-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
OutboundResponseTypeTests.java
235 lines (195 loc) · 7.4 KB
/
OutboundResponseTypeTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* Copyright 2002-2015 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.test.util.SocketUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Oleg Zhurakousky
* @author Artem Bilan
* @author Gary Russell
* @since 2.2
*
* <p>
* see https://jira.springsource.org/browse/INT-2397
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class OutboundResponseTypeTests {
private static HttpServer server;
private static MyHandler httpHandler;
@Autowired
private QueueChannel replyChannel;
@Autowired
private MessageChannel requestChannel;
@Autowired
private MessageChannel resTypeSetChannel;
@Autowired
private MessageChannel resPrimitiveStringPresentationChannel;
@Autowired
private MessageChannel resTypeExpressionSetChannel;
@Autowired
private MessageChannel resTypeExpressionSetSerializationChannel;
@Autowired
private MessageChannel invalidResponseTypeChannel;
@Autowired
private MessageChannel contentTypePropagationChannel;
private static int port = SocketUtils.findAvailableServerSocket();
@BeforeClass
public static void createServer() throws Exception {
httpHandler = new MyHandler();
server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/testApps/outboundResponse", httpHandler);
server.start();
}
@AfterClass
public static void stopServer() throws Exception {
server.stop(0);
}
@Test
public void testDefaultResponseType() throws Exception {
this.requestChannel.send(new GenericMessage<String>("Hello"));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof ResponseEntity);
}
@Test
public void testWithResponseTypeSet() throws Exception {
this.resTypeSetChannel.send(new GenericMessage<String>("Hello"));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
}
@Test
public void testWithResponseTypeExpressionSet() throws Exception {
this.resTypeExpressionSetChannel.send(new GenericMessage<String>("java.lang.String"));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
}
@Test
public void testWithResponseTypeExpressionSetAsClass() throws Exception {
this.resTypeExpressionSetSerializationChannel.send(new GenericMessage<Class<?>>(String.class));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof String);
}
@Test
public void testInt2706ResponseTypeExpressionAsPrimitive() throws Exception {
this.resTypeExpressionSetChannel.send(new GenericMessage<String>("byte[]"));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof byte[]);
}
@Test
public void testInt2706ResponseTypePrimitiveArrayClassAsString() throws Exception {
this.resPrimitiveStringPresentationChannel.send(new GenericMessage<byte[]>("hello".getBytes()));
Message<?> message = this.replyChannel.receive(5000);
assertNotNull(message);
assertTrue(message.getPayload() instanceof byte[]);
}
@Test
public void testInt3052InvalidResponseType() throws Exception {
try {
this.invalidResponseTypeChannel.send(new GenericMessage<byte[]>("hello".getBytes()));
fail("IllegalStateException expected.");
}
catch (Exception e) {
assertThat(e, Matchers.instanceOf(MessageHandlingException.class));
Throwable t = e.getCause();
assertThat(t, Matchers.instanceOf(IllegalStateException.class));
assertThat(t.getMessage(), Matchers.containsString("'expectedResponseType' can be an instance of " +
"'Class<?>', 'String' or 'ParameterizedTypeReference<?>'"));
}
}
@Test
public void testMutuallyExclusivityInMethodAndMethodExpression() throws Exception {
try {
new ClassPathXmlApplicationContext("OutboundResponseTypeTests-context-fail.xml", this.getClass());
fail("Expected BeansException");
}
catch (BeansException e) {
assertTrue(e instanceof BeanDefinitionParsingException);
assertTrue(e.getMessage().contains("The 'expected-response-type' " +
"and 'expected-response-type-expression' are mutually exclusive"));
}
}
@Test
public void testContentTypePropagation() throws Exception {
this.contentTypePropagationChannel
.send(new GenericMessage<Map<String, String>>(Collections.singletonMap("foo", "bar")));
assertEquals(MediaType.APPLICATION_JSON.toString(), httpHandler.requestHeaders.getFirst("Content-Type"));
}
static class MyHandler implements HttpHandler {
private String httpMethod = "POST";
private volatile Headers requestHeaders;
public void setHttpMethod(String httpMethod) {
this.httpMethod = httpMethod;
}
public void handle(HttpExchange t) throws IOException {
String requestMethod = t.getRequestMethod();
requestHeaders = t.getRequestHeaders();
String response = null;
if (requestMethod.equalsIgnoreCase(this.httpMethod)) {
response = httpMethod;
t.getResponseHeaders().add("Content-Type", MediaType.TEXT_PLAIN.toString()); //Required for Spring 3.0.x
t.sendResponseHeaders(200, response.length());
}
else {
response = "Request is NOT valid";
t.sendResponseHeaders(404, 0);
}
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
public static class Port {
public String getPort() {
return Integer.toString(port);
}
}
}