RequestLogger { public static void logRequest(EventBus eventBus, final HttpServerRequest request, final int status, Buffer data) { logRequest(eventBus, request, status, data, request.response().headers()); } private  RequestLogger(); static void logRequest(EventBus eventBus, final HttpServerRequest request, final int status, Buffer data); static void logRequest(EventBus eventBus, final HttpServerRequest request, final int status, Buffer data, final MultiMap responseHeaders); static final String OK; static final String ERROR; static final String MESSAGE; static final String STATUS; static final String REQUEST_URI; static final String REQUEST_METHOD; static final String REQUEST_HEADERS; static final String RESPONSE_HEADERS; static final String REQUEST_STATUS; static final String BODY; }
@Test public void testLogRequest(TestContext context){ EventBus eventBus = Mockito.mock(EventBus .class); CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("Content-Type", "application/json"); headers.add("x-rp-unique-id", "123456"); MockedResponse response = new MockedResponse(new CaseInsensitiveHeaders()); MockedRequest request = new MockedRequest("/uri/to/a/resource", HttpMethod.PUT, headers, response); JsonObject body = new JsonObject().put("key_1", "value_2").put("key_2", 99); RequestLogger.logRequest(eventBus, request, StatusCode.OK.getStatusCode(), Buffer.buffer(body.encode())); JsonObject expected = new JsonObject(); expected.put(REQUEST_URI, "/uri/to/a/resource"); expected.put(REQUEST_METHOD, "PUT"); JsonObject requestHeaders = new JsonObject().put("Content-Type", "application/json").put("x-rp-unique-id", "123456"); expected.put(REQUEST_HEADERS, requestHeaders); expected.put(RESPONSE_HEADERS, new JsonObject()); expected.put(REQUEST_STATUS, StatusCode.OK.getStatusCode()); expected.put(BODY, Buffer.buffer(body.encode()).toString()); Mockito.verify(eventBus, Mockito.times(1)) .send(eq(Address.requestLoggingConsumerAddress()), eq(expected), any(Handler.class)); }
@Test public void testLogRequestResponseHeaders(TestContext context){ EventBus eventBus = Mockito.mock(EventBus .class); CaseInsensitiveHeaders headers = new CaseInsensitiveHeaders(); headers.add("Content-Type", "application/json"); headers.add("x-rp-unique-id", "123456"); MockedRequest request = new MockedRequest("/uri/to/a/resource", HttpMethod.PUT, headers, new MockedResponse(new CaseInsensitiveHeaders())); JsonObject body = new JsonObject().put("key_1", "value_2").put("key_2", 99); CaseInsensitiveHeaders responseHeaders = new CaseInsensitiveHeaders(); responseHeaders.add("header_1", "value_1"); responseHeaders.add("header_2", "value_2"); RequestLogger.logRequest(eventBus, request, StatusCode.OK.getStatusCode(), Buffer.buffer(body.encode()), responseHeaders); JsonObject expected = new JsonObject(); expected.put(REQUEST_URI, "/uri/to/a/resource"); expected.put(REQUEST_METHOD, "PUT"); JsonObject requestHeaders = new JsonObject().put("Content-Type", "application/json").put("x-rp-unique-id", "123456"); expected.put(REQUEST_HEADERS, requestHeaders); JsonObject responseHeadersJsonObject = new JsonObject().put("header_1", "value_1").put("header_2", "value_2"); expected.put(RESPONSE_HEADERS, responseHeadersJsonObject); expected.put(REQUEST_STATUS, StatusCode.OK.getStatusCode()); expected.put(BODY, Buffer.buffer(body.encode()).toString()); Mockito.verify(eventBus, Mockito.times(1)) .send(eq(Address.requestLoggingConsumerAddress()), eq(expected), any(Handler.class)); }