@Test public void interceptorsNotJsonRpcTest() throws IOException { String requestNotRpc = "{\"test\": 1}"; String responseError = "{\"jsonrpc\":\"2.0\",\"id\":\"null\",\"error\":{\"code\":-32601,\"message\":\"method not found\"}}"; //bad call mockInterceptor.preHandleJson(mapper.readTree(requestNotRpc)); expectLastCall().times(1); mockInterceptor.postHandleJson(anyObject(JsonNode.class)); // mockInterceptor.postHandleJson(Util.mapper.readTree(responseError)); this place cause problem // json nodes are same but EasyMock don't understand it expectLastCall().times(1); replay(mockInterceptor); jsonRpcServer.handleRequest(new ByteArrayInputStream(requestNotRpc.getBytes(StandardCharsets.UTF_8)), byteArrayOutputStream); verify(mockInterceptor); }
mockInterceptor.preHandleJson(mapper.readTree(requestGood)); expectLastCall().times(1); mockInterceptor.preHandle( anyObject(), anyObject(Method.class), expectLastCall().times(1); expect(mockService.overloadedMethod("test.cool", "test.ru")).andReturn("test.ru"); mockInterceptor.postHandle( anyObject(), anyObject(Method.class), ); expectLastCall().times(1); mockInterceptor.postHandleJson(mapper.readTree(responseGood)); expectLastCall().times(1);
interceptor.preHandle(target, methodArgs.method, methodArgs.arguments); interceptor.postHandle(target, methodArgs.method, methodArgs.arguments, result);
/** * Handles a single request from the given {@link InputStream}, * that is to say that a single {@link JsonNode} is read from * the stream and treated as a JSON-RPC request. All responses * are written to the given {@link OutputStream}. * * @param input the {@link InputStream} * @param output the {@link OutputStream} * @return the error code, or {@code 0} if none * @throws IOException on error */ public int handleRequest(final InputStream input, final OutputStream output) throws IOException { final ReadContext readContext = ReadContext.getReadContext(input, mapper); try { readContext.assertReadable(); final JsonNode jsonNode = readContext.nextValue(); for (JsonRpcInterceptor interceptor : interceptorList) { interceptor.preHandleJson(jsonNode); } return handleJsonNodeRequest(jsonNode, output).code; } catch (JsonParseException | JsonMappingException e) { return writeAndFlushValueError(output, createResponseError(VERSION, NULL, JsonError.PARSE_ERROR)).code; } }
/** * Writes and flushes a value to the given {@link OutputStream} * and prevents Jackson from closing it. Also writes newline. * * @param output the {@link OutputStream} * @param value the value to write * @throws IOException on error */ private void writeAndFlushValue(OutputStream output, ObjectNode value) throws IOException { logger.debug("Response: {}", value); for (JsonRpcInterceptor interceptor : interceptorList) { interceptor.postHandleJson(value); } mapper.writeValue(new NoCloseOutputStream(output), value); output.write('\n'); }
@Test public void interceptorsPreHandleExceptionTest() throws IOException { final String requestGood = "{\n" + " \"id\": 0,\n" + " \"jsonrpc\": \"2.0\",\n" + " \"method\": \"testMethod\",\n" + " \"params\": [\"test.cool\"]\n" + " }\n" + "}"; String exceptionMessage = "123"; String responseError = "{\"jsonrpc\":\"2.0\",\"id\":0,\"error\":{\"code\":-32001,\"message\":\"" + exceptionMessage + "\",\"data\":{\"exceptionTypeName\":\"java.lang.RuntimeException\",\"message\":\"" + exceptionMessage + "\"}}}"; // mockInterceptor.preHandle( anyObject(), anyObject(Method.class), eq(new ArrayList<JsonNode>() {{ add(mapper.readTree(requestGood).at("/params/0")); }}) ); expectLastCall().andThrow(new RuntimeException(exceptionMessage)); replay(mockInterceptor); jsonRpcServer.handleRequest(new ByteArrayInputStream(requestGood.getBytes(StandardCharsets.UTF_8)), byteArrayOutputStream); assertEquals(responseError, byteArrayOutputStream.toString("UTF-8").trim()); verify(mockInterceptor); }
@Test public void interceptorsPostHandleExceptionTest() throws IOException { final String requestGood = "{\n" + " \"id\": 0,\n" + " \"jsonrpc\": \"2.0\",\n" + " \"method\": \"testMethod\",\n" + " \"params\": [\"test.cool\"]\n" + " }\n" + "}"; String exceptionMessage = "123"; String returnString = "test"; String responseError = "{\"jsonrpc\":\"2.0\",\"id\":0,\"error\":{\"code\":-32001,\"message\":\"" + exceptionMessage + "\",\"data\":{\"exceptionTypeName\":\"java.lang.RuntimeException\",\"message\":\"" + exceptionMessage + "\"}}}"; // expect(mockService.testMethod(mapper.readTree(requestGood).at("/params/0").asText())).andReturn(returnString); mockInterceptor.postHandle( anyObject(), anyObject(Method.class), eq(new ArrayList<JsonNode>() {{ add(mapper.readTree(requestGood).at("/params/0")); }}), eq(new TextNode(returnString)) ); expectLastCall().andThrow(new RuntimeException(exceptionMessage)); replay(mockService, mockInterceptor); jsonRpcServer.handleRequest(new ByteArrayInputStream(requestGood.getBytes(StandardCharsets.UTF_8)), byteArrayOutputStream); assertEquals(responseError, byteArrayOutputStream.toString("UTF-8").trim()); verify(mockService, mockInterceptor); }
@Test public void interceptorsPreHandleJsonExceptionTest() throws IOException { String requestNotRpc = "{\"test\": 1}"; String exceptionMessage = "123"; // mockInterceptor.preHandleJson(mapper.readTree(requestNotRpc)); expectLastCall().andThrow(new RuntimeException(exceptionMessage)); replay(mockInterceptor); MockHttpServletRequest request = new MockHttpServletRequest("POST", "/v1/zone"); request.setContent(requestNotRpc.getBytes(StandardCharsets.UTF_8)); MockHttpServletResponse response = new MockHttpServletResponse(); jsonRpcServer.handle(request, response); assertEquals(400, response.getStatus()); assertEquals("", response.getContentAsString()); verify(mockInterceptor); }
@Test public void interceptorsPostHandleJsonExceptionTest() throws IOException { final String requestGood = "{\n" + " \"id\": 0,\n" + " \"jsonrpc\": \"2.0\",\n" + " \"method\": \"testMethod\",\n" + " \"params\": [\"test.cool\"]\n" + " }\n" + "}"; String exceptionMessage = "123"; String returnString = "test"; String responseError = "{\"jsonrpc\":\"2.0\",\"id\":0,\"error\":{\"code\":-32001,\"message\":\"" + exceptionMessage + "\",\"data\":{\"exceptionTypeName\":\"java.lang.RuntimeException\",\"message\":\"" + exceptionMessage + "\"}}}"; // expect(mockService.testMethod(mapper.readTree(requestGood).at("/params/0").asText())).andReturn(returnString); mockInterceptor.postHandleJson(anyObject(JsonNode.class)); expectLastCall().andThrow(new RuntimeException(exceptionMessage)); replay(mockService, mockInterceptor); jsonRpcServer.handleRequest(new ByteArrayInputStream(requestGood.getBytes(StandardCharsets.UTF_8)), byteArrayOutputStream); assertEquals(responseError, byteArrayOutputStream.toString("UTF-8").trim()); verify(mockService, mockInterceptor); }