@Before public void setup() throws RpcCallException { handlerDictionary = new MethodHandlerDictionary(); handlerDictionary.put("a", null); ServiceMethodHandlerUnderTest mockHandlerThrowsRpcCallEx = new ServiceMethodHandlerUnderTest(); handlerDictionary.put("jsonRpcWithException", mockHandlerThrowsRpcCallEx); metricRegistry = mock(MetricRegistry.class); when(metricRegistry.counter(anyString())).thenReturn(mock(Counter.class)); when(metricRegistry.timer(anyString())).thenReturn(mock(Timer.class)); handlerMetrics = mock(RpcHandlerMetrics.class); when(handlerMetrics.getMethodTimer(any(), any(), any())).thenReturn(mock(GoTimer.class)); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, new ServiceProperties(), null); }
@Test public void doPost_NotValidJson_ErrorMessageBadRequestWithHttpExposed() throws IOException { // given final String json = "xxxx"; HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(MockHttpServletResponse.class); BufferedReader readerFromRequest = new BufferedReader(new StringReader(json)); when(request.getReader()).thenReturn(readerFromRequest); CharArrayWriter charArryWriter = new CharArrayWriter(512); PrintWriter writer = new PrintWriter(charArryWriter); when(response.getWriter()).thenReturn(writer); doCallRealMethod().when(response).setStatus(anyInt()); doCallRealMethod().when(response).getStatus(); // when ServiceProperties props = new ServiceProperties(); props.addProperty(FeatureFlags.FLAG_EXPOSE_ERRORS_HTTP, "true"); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, props, null); servlet.doPost(request, response); // then final String responseAsString = charArryWriter.toString(); LOGGER.debug(responseAsString); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_BAD_REQUEST); }
@Test public void testRpcCallExceptionJsonRpcWithHttpExposed() throws IOException { String input = "{\"service\":\"x\",\"method\":\"jsonRpcWithException\",\"params\":[{}],\"id\":\"dead-beef\"}"; HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(MockHttpServletResponse.class); BufferedReader readerFromRequest = new BufferedReader(new StringReader(input)); when(request.getReader()).thenReturn(readerFromRequest); CharArrayWriter charArryWriter = new CharArrayWriter(512); PrintWriter writer = new PrintWriter(charArryWriter); when(response.getWriter()).thenReturn(writer); doCallRealMethod().when(response).setStatus(anyInt()); doCallRealMethod().when(response).getStatus(); ServiceProperties props = new ServiceProperties(); props.addProperty(FeatureFlags.FLAG_EXPOSE_ERRORS_HTTP, "true"); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, props, null); servlet.doPost(request, response); String responseAsString = charArryWriter.toString(); LOGGER.debug(responseAsString); assertThat(responseAsString).contains("no p4s5!"); assertThat(responseAsString).matches(Pattern.compile(".*retriable.+true.*")); assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); }