From 1478cf8e6e8bcbfd8b8749a88b8954c679047b91 Mon Sep 17 00:00:00 2001 From: Yauheni Barvinski Date: Thu, 13 Apr 2023 17:06:24 +0200 Subject: [PATCH] [plugin-rest-api] Move response time value reporting from sub-steps to attachment --- .../vividus/http/PublishingAttachmentInterceptor.java | 11 +++++++---- .../org/vividus/http/attachment/api-message.ftl | 7 +++++++ .../http/PublishingAttachmentInterceptorTests.java | 5 +++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/vividus-plugin-rest-api/src/main/java/org/vividus/http/PublishingAttachmentInterceptor.java b/vividus-plugin-rest-api/src/main/java/org/vividus/http/PublishingAttachmentInterceptor.java index 65152da267..5a30bf6d84 100644 --- a/vividus-plugin-rest-api/src/main/java/org/vividus/http/PublishingAttachmentInterceptor.java +++ b/vividus-plugin-rest-api/src/main/java/org/vividus/http/PublishingAttachmentInterceptor.java @@ -80,25 +80,28 @@ public void process(HttpRequest request, EntityDetails entityDetails, HttpContex } } } - attachApiMessage("Request: " + request, request.getHeaders(), body, mimeType, -1); + attachApiMessage("Request: " + request, request.getHeaders(), body, mimeType, -1, -1); } @Override - public void handle(HttpResponse response) throws IOException + public void handle(HttpResponse response) { Header[] headers = response.getResponseHeaders(); String attachmentTitle = String.format("Response: %s %s", response.getMethod(), response.getFrom()); String mimeType = getMimeType(headers).orElseGet(ContentType.DEFAULT_TEXT::getMimeType); - attachApiMessage(attachmentTitle, headers, response.getResponseBody(), mimeType, response.getStatusCode()); + attachApiMessage(attachmentTitle, headers, response.getResponseBody(), mimeType, response.getStatusCode(), + response.getResponseTimeInMs()); } - private void attachApiMessage(String title, Header[] headers, byte[] body, String mimeType, int statusCode) + private void attachApiMessage(String title, Header[] headers, byte[] body, String mimeType, int statusCode, + long responseTime) { Map dataMap = new HashMap<>(); dataMap.put("headers", headers); dataMap.put("body", body != null ? new String(body, StandardCharsets.UTF_8) : null); dataMap.put("bodyContentType", mimeType); dataMap.put("statusCode", statusCode); + dataMap.put("responseTime", responseTime); attachmentPublisher.publishAttachment("/org/vividus/http/attachment/api-message.ftl", dataMap, title); } diff --git a/vividus-plugin-rest-api/src/main/resources/org/vividus/http/attachment/api-message.ftl b/vividus-plugin-rest-api/src/main/resources/org/vividus/http/attachment/api-message.ftl index 2012b02ec7..414c793f46 100644 --- a/vividus-plugin-rest-api/src/main/resources/org/vividus/http/attachment/api-message.ftl +++ b/vividus-plugin-rest-api/src/main/resources/org/vividus/http/attachment/api-message.ftl @@ -43,6 +43,13 @@ + <#if responseTime != -1> +
+
+

Response time: ${responseTime} ms

+
+
+
diff --git a/vividus-plugin-rest-api/src/test/java/org/vividus/http/PublishingAttachmentInterceptorTests.java b/vividus-plugin-rest-api/src/test/java/org/vividus/http/PublishingAttachmentInterceptorTests.java index 570b2f9cf1..1380293685 100644 --- a/vividus-plugin-rest-api/src/test/java/org/vividus/http/PublishingAttachmentInterceptorTests.java +++ b/vividus-plugin-rest-api/src/test/java/org/vividus/http/PublishingAttachmentInterceptorTests.java @@ -152,7 +152,7 @@ void testHttpRequestBodyAttachingIsFailed() throws IOException } @Test - void testHttpResponseIsAttachedSuccessfully() throws IOException + void testHttpResponseIsAttachedSuccessfully() { var httpResponse = mock(HttpResponse.class); when(httpResponse.getResponseBody()).thenReturn(DATA); @@ -160,13 +160,14 @@ void testHttpResponseIsAttachedSuccessfully() throws IOException when(httpResponse.getMethod()).thenReturn(METHOD); when(httpResponse.getFrom()).thenReturn(URI.create(ENDPOINT)); when(httpResponse.getResponseHeaders()).thenReturn(new Header[] { mock(Header.class) }); + when(httpResponse.getResponseTimeInMs()).thenReturn(1L); interceptor.handle(httpResponse); var argumentCaptor = verifyPublishAttachment(RESPONSE); assertEquals(HttpStatus.SC_OK, argumentCaptor.getValue().get("statusCode").intValue()); } @Test - void testNoHttpResponseBodyIsAttached() throws IOException + void testNoHttpResponseBodyIsAttached() { var httpResponse = mock(HttpResponse.class); when(httpResponse.getResponseBody()).thenReturn(null);