Skip to content

Commit

Permalink
[plugin-rest-api] Move response time value reporting from sub-steps t…
Browse files Browse the repository at this point in the history
…o attachment
  • Loading branch information
EDbarvinsky committed Apr 17, 2023
1 parent 4662674 commit 1478cf8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
</div>
</div>
</#if>
<#if responseTime != -1>
<div class="panel panel-info">
<div class="panel-heading">
<h4 class="panel-title">Response time: ${responseTime} ms</h4>
</div>
</div>
</#if>

<div class="panel panel-info">
<div class="panel-heading">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,22 @@ void testHttpRequestBodyAttachingIsFailed() throws IOException
}

@Test
void testHttpResponseIsAttachedSuccessfully() throws IOException
void testHttpResponseIsAttachedSuccessfully()
{
var httpResponse = mock(HttpResponse.class);
when(httpResponse.getResponseBody()).thenReturn(DATA);
when(httpResponse.getStatusCode()).thenReturn(HttpStatus.SC_OK);
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);
Expand Down

0 comments on commit 1478cf8

Please sign in to comment.