|
22 | 22 | import java.io.ByteArrayOutputStream; |
23 | 23 | import java.io.PrintWriter; |
24 | 24 | import java.io.StringWriter; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
25 | 26 |
|
26 | 27 | import jakarta.servlet.ServletConfig; |
27 | 28 | import jakarta.servlet.ServletContext; |
|
34 | 35 |
|
35 | 36 | import org.apache.axis2.AxisFault; |
36 | 37 | import org.apache.axis2.Constants; |
| 38 | +import org.apache.axis2.builder.ApplicationXMLBuilder; |
| 39 | +import org.apache.axis2.builder.XFormURLEncodedBuilder; |
37 | 40 | import org.apache.axis2.context.ConfigurationContext; |
38 | 41 | import org.apache.axis2.context.ConfigurationContextFactory; |
39 | 42 | import org.apache.axis2.context.MessageContext; |
| 43 | +import org.apache.axis2.description.AxisOperation; |
40 | 44 | import org.apache.axis2.description.AxisService; |
41 | 45 | import org.apache.axis2.description.InOnlyAxisOperation; |
42 | 46 | import org.apache.axis2.description.Parameter; |
43 | 47 | import org.apache.axis2.engine.AxisConfiguration; |
| 48 | +import org.apache.axis2.engine.MessageReceiver; |
44 | 49 | import org.mockito.Mockito; |
45 | 50 |
|
46 | 51 | import static org.mockito.ArgumentMatchers.any; |
@@ -245,6 +250,47 @@ public void testEnableJSONOnlyRejectsNonJSONRequests() throws Exception { |
245 | 250 | responseContent.contains("application/json is mandatory")); |
246 | 251 | } |
247 | 252 |
|
| 253 | + /** |
| 254 | + * GET and PUT go through RESTUtil rather than processHTTPPostRequest, and must |
| 255 | + * refuse a request that is not application/json as POST does, rather than |
| 256 | + * binding the operation from the query string or an XML body. |
| 257 | + */ |
| 258 | + public void testEnableJSONOnlyRejectsNonJSONRestRequests() throws Exception { |
| 259 | + axisConfig.addParameter(new Parameter(Constants.Configuration.ENABLE_JSON_ONLY, "true")); |
| 260 | + axisConfig.addMessageBuilder("application/x-www-form-urlencoded", new XFormURLEncodedBuilder()); |
| 261 | + axisConfig.addMessageBuilder("application/xml", new ApplicationXMLBuilder()); |
| 262 | + |
| 263 | + final AtomicInteger invocations = new AtomicInteger(); |
| 264 | + AxisOperation operation = axisConfig.getService("TestService") |
| 265 | + .getOperation(new QName("testOperation")); |
| 266 | + operation.setMessageReceiver(new MessageReceiver() { |
| 267 | + public void receive(MessageContext messageContext) { |
| 268 | + invocations.incrementAndGet(); |
| 269 | + } |
| 270 | + }); |
| 271 | + |
| 272 | + String uri = "/axis2/services/TestService/testOperation"; |
| 273 | + when(mockRequest.getRequestURI()).thenReturn(uri); |
| 274 | + when(mockRequest.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080" + uri)); |
| 275 | + |
| 276 | + when(mockServletContext.getAttribute(AxisServlet.CONFIGURATION_CONTEXT)).thenReturn(configContext); |
| 277 | + servlet.init(mockServletConfig); |
| 278 | + |
| 279 | + when(mockRequest.getMethod()).thenReturn("GET"); |
| 280 | + when(mockRequest.getQueryString()).thenReturn("value=1"); |
| 281 | + servlet.doGet(mockRequest, mockResponse); |
| 282 | + |
| 283 | + when(mockRequest.getMethod()).thenReturn("PUT"); |
| 284 | + when(mockRequest.getQueryString()).thenReturn(null); |
| 285 | + when(mockRequest.getContentType()).thenReturn("application/xml"); |
| 286 | + when(mockRequest.getInputStream()).thenReturn( |
| 287 | + new MockServletInputStream("<testOperation><value>1</value></testOperation>")); |
| 288 | + servlet.doPut(mockRequest, mockResponse); |
| 289 | + |
| 290 | + assertEquals("non-JSON REST requests must not reach the operation", 0, invocations.get()); |
| 291 | + verify(mockResponse, times(2)).setStatus(HttpServletResponse.SC_BAD_REQUEST); |
| 292 | + } |
| 293 | + |
248 | 294 | // Helper classes for mocking |
249 | 295 | private static class MockServletInputStream extends jakarta.servlet.ServletInputStream { |
250 | 296 | private final ByteArrayInputStream inputStream; |
|
0 commit comments