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

Add request and response time to HttpResponseAttachment/HttpRequestAttachment #397

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -17,6 +17,8 @@

import io.qameta.allure.attachment.AttachmentData;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -36,18 +38,23 @@ public class HttpRequestAttachment implements AttachmentData {

private final String curl;

private final ZonedDateTime requestTime;

private final Map<String, String> headers;

private final Map<String, String> cookies;

@SuppressWarnings("checkstyle:ParameterNumber")
public HttpRequestAttachment(final String name, final String url, final String method,
final String body, final String curl, final Map<String, String> headers,
final String body, final String curl, final ZonedDateTime requestTime,
final Map<String, String> headers,
final Map<String, String> cookies) {
this.name = name;
this.url = url;
this.method = method;
this.body = body;
this.curl = curl;
this.requestTime = requestTime;
this.headers = headers;
this.cookies = cookies;
}
Expand Down Expand Up @@ -76,6 +83,10 @@ public String getCurl() {
return curl;
}

public ZonedDateTime getRequestTime() {
return requestTime;
}

@Override
public String getName() {
return name;
Expand All @@ -95,6 +106,8 @@ public static final class Builder {

private String body;

private ZonedDateTime requestTime;

private final Map<String, String> headers = new HashMap<>();

private final Map<String, String> cookies = new HashMap<>();
Expand All @@ -110,6 +123,11 @@ public static Builder create(final String attachmentName, final String url) {
return new Builder(attachmentName, url);
}

public Builder setRequestTime() {
this.requestTime = ZonedDateTime.now(ZoneId.systemDefault());
return this;
}

public Builder setMethod(final String method) {
Objects.requireNonNull(method, "Method must not be null value");
this.method = method;
Expand Down Expand Up @@ -150,6 +168,7 @@ public Builder setBody(final String body) {

/**
* Use setter method instead.
*
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
Expand All @@ -159,6 +178,7 @@ public Builder withMethod(final String method) {

/**
* Use setter method instead.
*
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
Expand All @@ -168,6 +188,7 @@ public Builder withHeader(final String name, final String value) {

/**
* Use setter method instead.
*
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
Expand All @@ -177,6 +198,7 @@ public Builder withHeaders(final Map<String, String> headers) {

/**
* Use setter method instead.
*
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
Expand All @@ -186,6 +208,7 @@ public Builder withCookie(final String name, final String value) {

/**
* Use setter method instead.
*
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
Expand All @@ -195,6 +218,7 @@ public Builder withCookies(final Map<String, String> cookies) {

/**
* Use setter method instead.
*
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
Expand All @@ -203,7 +227,7 @@ public Builder withBody(final String body) {
}

public HttpRequestAttachment build() {
return new HttpRequestAttachment(name, url, method, body, getCurl(), headers, cookies);
return new HttpRequestAttachment(name, url, method, body, getCurl(), requestTime, headers, cookies);
}

private String getCurl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import io.qameta.allure.attachment.AttachmentData;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand All @@ -34,17 +36,20 @@ public class HttpResponseAttachment implements AttachmentData {

private final int responseCode;

private final ZonedDateTime responseTime;

private final Map<String, String> headers;

private final Map<String, String> cookies;

public HttpResponseAttachment(final String name, final String url,
final String body, final int responseCode,
final String body, final int responseCode, final ZonedDateTime responseTime,
final Map<String, String> headers, final Map<String, String> cookies) {
this.name = name;
this.url = url;
this.body = body;
this.responseCode = responseCode;
this.responseTime = responseTime;
this.headers = headers;
this.cookies = cookies;
}
Expand All @@ -66,6 +71,10 @@ public int getResponseCode() {
return responseCode;
}

public ZonedDateTime getResponseTime() {
return responseTime;
}

public Map<String, String> getHeaders() {
return headers;
}
Expand All @@ -86,6 +95,8 @@ public static final class Builder {

private int responseCode;

private ZonedDateTime responseTime;

private String body;

private final Map<String, String> headers = new HashMap<>();
Expand All @@ -101,6 +112,11 @@ public static Builder create(final String attachmentName) {
return new Builder(attachmentName);
}

public Builder setResponseTime() {
this.responseTime = ZonedDateTime.now(ZoneId.systemDefault());
return this;
}

public Builder setUrl(final String url) {
Objects.requireNonNull(url, "Url must not be null value");
this.url = url;
Expand Down Expand Up @@ -209,7 +225,7 @@ public Builder withBody(final String body) {
}

public HttpResponseAttachment build() {
return new HttpResponseAttachment(name, url, body, responseCode, headers, cookies);
return new HttpResponseAttachment(name, url, body, responseCode, responseTime, headers, cookies);
}
}
}
7 changes: 7 additions & 0 deletions allure-attachments/src/main/resources/tpl/http-request.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<#-- @ftlvariable name="data" type="io.qameta.allure.attachment.http.HttpRequestAttachment" -->
<div><#if data.method??>${data.method}<#else>GET</#if> to <#if data.url??>${data.url}<#else>Unknown</#if></div>

<#if data.requestTime??>
<h4>Request time</h4>
<div>
${data.requestTime}
</div>
</#if>

<#if data.body??>
<h4>Body</h4>
<div>
Expand Down
7 changes: 7 additions & 0 deletions allure-attachments/src/main/resources/tpl/http-response.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
<div>Status code <#if data.responseCode??>${data.responseCode} <#else>Unknown</#if></div>
<#if data.url??><div>${data.url}</div></#if>

<#if data.responseTime??>
<h4>Response time</h4>
<div>
${data.responseTime}
</div>
</#if>

<#if data.body??>
<h4>Body</h4>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package io.qameta.allure.attachment;

import io.qameta.allure.attachment.http.HttpRequestAttachment;
import io.qameta.allure.attachment.http.HttpResponseAttachment;
import io.qameta.allure.test.AllureFeatures;
import org.junit.jupiter.api.Test;

import static io.qameta.allure.attachment.testdata.TestData.randomHttpRequestAttachment;
import static io.qameta.allure.attachment.testdata.TestData.randomHttpResponseAttachment;
import static org.assertj.core.api.Assertions.assertThat;

/**
Expand All @@ -40,6 +42,26 @@ void shouldRenderRequestAttachment() {
.hasFieldOrProperty("content");
}

@AllureFeatures.Attachments
@Test
void shouldRenderRequestTimeAttachment() {
final HttpRequestAttachment data = randomHttpRequestAttachment();
final DefaultAttachmentContent content = new FreemarkerAttachmentRenderer("http-request.ftl")
.render(data);

assertThat(content.getContent()).contains("Request time");
}

@AllureFeatures.Attachments
@Test
void shouldRenderResponseTimeAttachment() {
final HttpResponseAttachment data = randomHttpResponseAttachment();
final DefaultAttachmentContent content = new FreemarkerAttachmentRenderer("http-response.ftl")
.render(data);

assertThat(content.getContent()).contains("Response time");
}

@AllureFeatures.Attachments
@Test
void shouldRenderResponseAttachment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.qameta.allure.attachment.http.HttpResponseAttachment;
import org.apache.commons.lang3.RandomStringUtils;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -45,6 +47,7 @@ public static HttpRequestAttachment randomHttpRequestAttachment() {
randomString(),
randomString(),
randomString(),
ZonedDateTime.now(ZoneId.systemDefault()),
randomMap(),
randomMap()
);
Expand All @@ -56,6 +59,7 @@ public static HttpResponseAttachment randomHttpResponseAttachment() {
randomString(),
randomString(),
ThreadLocalRandom.current().nextInt(),
ZonedDateTime.now(ZoneId.systemDefault()),
randomMap(),
randomMap()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public Response filter(final FilterableRequestSpecification requestSpec,


final HttpRequestAttachment.Builder requestAttachmentBuilder = create("Request", requestSpec.getURI())
.setRequestTime()
.setMethod(requestSpec.getMethod())
.setHeaders(toMapConverter(requestSpec.getHeaders()))
.setCookies(toMapConverter(requestSpec.getCookies()));
Expand All @@ -95,6 +96,7 @@ public Response filter(final FilterableRequestSpecification requestSpec,

final Response response = filterContext.next(requestSpec, responseSpec);
final HttpResponseAttachment responseAttachment = create(response.getStatusLine())
.setResponseTime()
.setResponseCode(response.getStatusCode())
.setHeaders(toMapConverter(response.getHeaders()))
.setBody(prettifier.getPrettifiedBodyIfPossible(response, response.getBody()))
Expand Down