Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [plugin-rest-api] Move response time value reporting from sub-steps to attachment #3829

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor Author

@EDbarvinsky EDbarvinsky Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 args to much ?
can be replaced with response

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you won't be able to use response in attachApiMessage since it's used to requests as well

}

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">
EDbarvinsky marked this conversation as resolved.
Show resolved Hide resolved
<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