1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.http;
18
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.springframework.mock.web.MockHttpServletRequest;
24 import org.springframework.mock.web.MockHttpServletResponse;
25 import org.springframework.ws.FaultAwareWebServiceMessage;
26 import org.springframework.ws.InvalidXmlException;
27 import org.springframework.ws.NoEndpointFoundException;
28 import org.springframework.ws.WebServiceMessageFactory;
29 import org.springframework.ws.context.MessageContext;
30 import org.springframework.ws.transport.WebServiceMessageReceiver;
31
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35
36 import static org.easymock.EasyMock.*;
37
38 public class WebServiceMessageReceiverHandlerAdapterTest {
39
40 private static final String REQUEST = " <SOAP-ENV:Envelope\n" +
41 " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
42 " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" + " <SOAP-ENV:Body>\n" +
43 " <m:GetLastTradePrice xmlns:m=\"Some-URI\">\n" + " <symbol>DIS</symbol>\n" +
44 " </m:GetLastTradePrice>\n" + " </SOAP-ENV:Body>\n" + "</SOAP-ENV:Envelope>";
45
46 private WebServiceMessageReceiverHandlerAdapter adapter;
47
48 private MockHttpServletRequest httpRequest;
49
50 private MockHttpServletResponse httpResponse;
51
52 private WebServiceMessageFactory factoryMock;
53
54 private FaultAwareWebServiceMessage responseMock;
55
56 private FaultAwareWebServiceMessage requestMock;
57
58 @Before
59 public void setUp() throws Exception {
60 adapter = new WebServiceMessageReceiverHandlerAdapter();
61 httpRequest = new MockHttpServletRequest();
62 httpResponse = new MockHttpServletResponse();
63 factoryMock = createMock(WebServiceMessageFactory.class);
64 adapter.setMessageFactory(factoryMock);
65 requestMock = createMock("request", FaultAwareWebServiceMessage.class);
66 responseMock = createMock("response", FaultAwareWebServiceMessage.class);
67 }
68
69 @Test
70 public void testHandleNonPost() throws Exception {
71 httpRequest.setMethod(HttpTransportConstants.METHOD_GET);
72 replayMockControls();
73 WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
74
75 public void receive(MessageContext messageContext) throws Exception {
76 }
77 };
78 adapter.handle(httpRequest, httpResponse, endpoint);
79 Assert.assertEquals("METHOD_NOT_ALLOWED expected", HttpServletResponse.SC_METHOD_NOT_ALLOWED,
80 httpResponse.getStatus());
81 verifyMockControls();
82 }
83
84 @Test
85 public void testHandlePostNoResponse() throws Exception {
86 httpRequest.setMethod(HttpTransportConstants.METHOD_POST);
87 httpRequest.setContent(REQUEST.getBytes("UTF-8"));
88 httpRequest.setContentType("text/xml; charset=\"utf-8\"");
89 httpRequest.setCharacterEncoding("UTF-8");
90 expect(factoryMock.createWebServiceMessage(isA(InputStream.class))).andReturn(responseMock);
91
92 replayMockControls();
93 WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
94
95 public void receive(MessageContext messageContext) throws Exception {
96 }
97 };
98
99 adapter.handle(httpRequest, httpResponse, endpoint);
100
101 Assert.assertEquals("Invalid status code on response", HttpServletResponse.SC_ACCEPTED,
102 httpResponse.getStatus());
103 Assert.assertEquals("Response written", 0, httpResponse.getContentAsString().length());
104 verifyMockControls();
105 }
106
107 @Test
108 public void testHandlePostResponse() throws Exception {
109 httpRequest.setMethod(HttpTransportConstants.METHOD_POST);
110 httpRequest.setContent(REQUEST.getBytes("UTF-8"));
111 httpRequest.setContentType("text/xml; charset=\"utf-8\"");
112 httpRequest.setCharacterEncoding("UTF-8");
113 expect(factoryMock.createWebServiceMessage(isA(InputStream.class))).andReturn(requestMock);
114 expect(factoryMock.createWebServiceMessage()).andReturn(responseMock);
115 expect(responseMock.hasFault()).andReturn(false);
116 responseMock.writeTo(isA(OutputStream.class));
117
118 replayMockControls();
119 WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
120
121 public void receive(MessageContext messageContext) throws Exception {
122 messageContext.getResponse();
123 }
124 };
125
126 adapter.handle(httpRequest, httpResponse, endpoint);
127
128 Assert.assertEquals("Invalid status code on response", HttpServletResponse.SC_OK, httpResponse.getStatus());
129 verifyMockControls();
130 }
131
132 @Test
133 public void testHandlePostFault() throws Exception {
134 httpRequest.setMethod(HttpTransportConstants.METHOD_POST);
135 httpRequest.setContent(REQUEST.getBytes("UTF-8"));
136 httpRequest.setContentType("text/xml; charset=\"utf-8\"");
137 httpRequest.setCharacterEncoding("UTF-8");
138 expect(factoryMock.createWebServiceMessage(isA(InputStream.class))).andReturn(requestMock);
139 expect(factoryMock.createWebServiceMessage()).andReturn(responseMock);
140 expect(responseMock.hasFault()).andReturn(true);
141 responseMock.writeTo(isA(OutputStream.class));
142
143 replayMockControls();
144 WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
145
146 public void receive(MessageContext messageContext) throws Exception {
147 messageContext.getResponse();
148 }
149 };
150
151 adapter.handle(httpRequest, httpResponse, endpoint);
152
153 Assert.assertEquals("Invalid status code on response", HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
154 httpResponse.getStatus());
155 verifyMockControls();
156 }
157
158 @Test
159 public void testHandleNotFound() throws Exception {
160 httpRequest.setMethod(HttpTransportConstants.METHOD_POST);
161 httpRequest.setContent(REQUEST.getBytes("UTF-8"));
162 httpRequest.setContentType("text/xml; charset=\"utf-8\"");
163 httpRequest.setCharacterEncoding("UTF-8");
164 expect(factoryMock.createWebServiceMessage(isA(InputStream.class))).andReturn(requestMock);
165
166 replayMockControls();
167
168 WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
169
170 public void receive(MessageContext messageContext) throws Exception {
171 throw new NoEndpointFoundException(messageContext.getRequest());
172 }
173 };
174
175 adapter.handle(httpRequest, httpResponse, endpoint);
176 Assert.assertEquals("No 404 returned", HttpServletResponse.SC_NOT_FOUND, httpResponse.getStatus());
177
178 verifyMockControls();
179
180 }
181
182 @Test
183 public void testHandleInvalidXml() throws Exception {
184 httpRequest.setMethod(HttpTransportConstants.METHOD_POST);
185 httpRequest.setContent(REQUEST.getBytes("UTF-8"));
186 httpRequest.setContentType("text/xml; charset=\"utf-8\"");
187 httpRequest.setCharacterEncoding("UTF-8");
188 expect(factoryMock.createWebServiceMessage(isA(InputStream.class))).andThrow(new InvalidXmlException(null, null));
189
190 replayMockControls();
191
192 WebServiceMessageReceiver endpoint = new WebServiceMessageReceiver() {
193
194 public void receive(MessageContext messageContext) throws Exception {
195 }
196 };
197
198 adapter.handle(httpRequest, httpResponse, endpoint);
199 Assert.assertEquals("No 400 returned", HttpServletResponse.SC_BAD_REQUEST, httpResponse.getStatus());
200
201 verifyMockControls();
202 }
203
204 private void replayMockControls() {
205 replay(factoryMock, requestMock, responseMock);
206 }
207
208 private void verifyMockControls() {
209 verify(factoryMock, requestMock, responseMock);
210 }
211
212 }