Skip to content

Commit

Permalink
fix HTTP module according latest SF changes
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Sep 12, 2022
1 parent df1b8b4 commit 6261980
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* Copyright 2015-2022 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.
Expand All @@ -17,8 +17,7 @@
package org.springframework.integration.http.inbound;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -30,8 +29,6 @@
import jakarta.servlet.http.HttpServletResponse;

import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
Expand Down Expand Up @@ -63,20 +60,7 @@ public void testMultiPass() throws Exception {

HttpServletRequest request = mock(HttpServletRequest.class);
ServletInputStream sis = mock(ServletInputStream.class);
doAnswer(new Answer<Integer>() {

int done;

@Override
public Integer answer(InvocationOnMock invocation) {
byte[] buff = invocation.getArgument(0);
buff[0] = 'f';
buff[1] = 'o';
buff[2] = 'o';
return done++ > 0 ? -1 : 3;
}

}).when(sis).read(any(byte[].class));
doReturn("test data".getBytes()).when(sis).readAllBytes();
when(request.getInputStream()).thenReturn(sis);
when(request.getMethod()).thenReturn("POST");
when(request.getHeaderNames()).thenReturn(mock(Enumeration.class));
Expand All @@ -87,7 +71,7 @@ public Integer answer(InvocationOnMock invocation) {
Message<?> received = requestChannel.receive(10000);
assertThat(received).isNotNull();
assertThat(received.getPayload()).isInstanceOf(byte[].class);
assertThat(new String((byte[]) received.getPayload())).isEqualTo("foo");
assertThat(new String((byte[]) received.getPayload())).isEqualTo("test data");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void simpleStringKeyStringValueFormData() {
MessageBuilder.withPayload(form)
.setHeader(MessageHeaders.CONTENT_TYPE,
MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8")
.build();
.build();
QueueChannel replyChannel = new QueueChannel();
handler.setOutputChannel(replyChannel);

Expand Down Expand Up @@ -204,9 +204,9 @@ public void stringKeyStringArrayValueFormData() {
setBeanFactory(handler);
handler.afterPropertiesSet();
Map<String, Object> form = new LinkedHashMap<>();
form.put("a", new String[] { "1", "2", "3" });
form.put("a", new String[]{ "1", "2", "3" });
form.put("b", "4");
form.put("c", new String[] { "5" });
form.put("c", new String[]{ "5" });
form.put("d", "6");
Message<?> message = MessageBuilder.withPayload(form).build();

Expand Down Expand Up @@ -249,9 +249,9 @@ public void stringKeyPrimitiveArrayValueMixedFormData() {
setBeanFactory(handler);
handler.afterPropertiesSet();
Map<String, Object> form = new LinkedHashMap<>();
form.put("a", new int[] { 1, 2, 3 });
form.put("a", new int[]{ 1, 2, 3 });
form.put("b", "4");
form.put("c", new String[] { "5" });
form.put("c", new String[]{ "5" });
form.put("d", "6");
Message<?> message = MessageBuilder.withPayload(form).build();

Expand Down Expand Up @@ -297,7 +297,7 @@ public void stringKeyNullArrayValueMixedFormData() {
setBeanFactory(handler);
handler.afterPropertiesSet();
Map<String, Object> form = new LinkedHashMap<>();
form.put("a", new Object[] { null, 4, null });
form.put("a", new Object[]{ null, 4, null });
form.put("b", "4");
Message<?> message = MessageBuilder.withPayload(form).build();

Expand Down Expand Up @@ -833,9 +833,11 @@ public void acceptHeaderForSerializableResponseMessageExchange() throws IOExcept
public void testNoContentTypeAndSmartConverter() {
Sinks.One<HttpHeaders> httpHeadersSink = Sinks.one();
RestTemplate testRestTemplate = new RestTemplate() {

@Nullable
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback,
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
protected <T> T doExecute(URI url, @Nullable String uriTemplate, @Nullable HttpMethod method,
@Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor)
throws RestClientException {

try {
ClientHttpRequest request = createRequest(url, method);
Expand All @@ -847,11 +849,12 @@ protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable Reques
}
throw new RuntimeException("intentional");
}

};

HttpRequestExecutingMessageHandler handler =
new HttpRequestExecutingMessageHandler("https://www.springsource.org/spring-integration",
testRestTemplate);
testRestTemplate);
setBeanFactory(handler);
handler.afterPropertiesSet();

Expand Down Expand Up @@ -911,8 +914,10 @@ private static class MockRestTemplate extends RestTemplate {
private final AtomicReference<String> actualUrl = new AtomicReference<>();

@Nullable
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback,
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
protected <T> T doExecute(URI url, @Nullable String uriTemplate, @Nullable HttpMethod method,
@Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor)
throws RestClientException {

this.actualUrl.set(url.toString());
this.lastRequestEntity.set(TestUtils.getPropertyValue(requestCallback, "requestEntity", HttpEntity.class));
throw new RuntimeException("intentional");
Expand All @@ -932,8 +937,10 @@ private static class MockRestTemplate2 extends RestTemplate {
}

@Nullable
protected <T> T doExecute(URI url, @Nullable HttpMethod method, @Nullable RequestCallback requestCallback,
@Nullable ResponseExtractor<T> responseExtractor) throws RestClientException {
protected <T> T doExecute(URI url, @Nullable String uriTemplate, @Nullable HttpMethod method,
@Nullable RequestCallback requestCallback, @Nullable ResponseExtractor<T> responseExtractor)
throws RestClientException {

this.actualUrl.set(url.toString());
try {
return responseExtractor.extractData(new MockClientHttpResponse(new byte[0], HttpStatus.OK));
Expand Down

0 comments on commit 6261980

Please sign in to comment.