Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Created August 6, 2013 22:52
Show Gist options
  • Save garyrussell/6169540 to your computer and use it in GitHub Desktop.
Save garyrussell/6169540 to your computer and use it in GitHub Desktop.
Multipart Response
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.http.outbound;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.util.Collections;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.integration.Message;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.web.client.RestTemplate;
/**
* @author Gary Russell
* @since 3.0
*
*/
public class MultiPartResponseTests {
private static final String RESPONSE =
"--foo\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"\r\n" +
"foo" +
"--foo\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"\r\n" +
"bar" +
"--foo--\r\n";
@Test
public void testMPR() throws Exception {
ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
RestTemplate template = new RestTemplate(requestFactory);
ClientHttpRequest request = mock(ClientHttpRequest.class);
ClientHttpResponse response = mock(ClientHttpResponse.class);
when(requestFactory.createRequest(any(URI.class), any(HttpMethod.class))).thenReturn(request);
when(request.getHeaders()).thenReturn(new HttpHeaders());
when(request.getBody()).thenReturn(new ByteArrayOutputStream());
when(request.execute()).thenReturn(response);
when(response.getStatusCode()).thenReturn(HttpStatus.OK);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.put("Content-Type", Collections.singletonList("Multipart/mixed; boundary=\"foo\";"));
when(response.getHeaders()).thenReturn(responseHeaders);
when(response.getBody()).thenReturn(new ByteArrayInputStream(RESPONSE.getBytes()));
HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("http://foo", template);
PollableChannel outputChannel = new QueueChannel();
handler.setOutputChannel(outputChannel);
handler.setExpectedResponseType(String.class);
handler.afterPropertiesSet();
handler.handleMessage(new GenericMessage<String>("foo"));
Message<?> received = outputChannel.receive(0);
assertNotNull(received);
System.out.println(received);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment