Skip to content

Commit 948325b

Browse files
committed
add response http attachment
1 parent e1e7af4 commit 948325b

16 files changed

Lines changed: 548 additions & 38 deletions

File tree

allure-attachments/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ dependencies {
88
compile project(':allure-java-commons')
99

1010
compile 'org.freemarker:freemarker'
11-
compile 'commons-io:commons-io'
12-
compile 'org.apache.commons:commons-lang3'
1311

14-
testCompile 'org.assertj:assertj-core'
1512
testCompile 'junit:junit'
13+
testCompile 'org.slf4j:slf4j-simple'
14+
testCompile 'org.assertj:assertj-core'
15+
testCompile 'org.mockito:mockito-core'
16+
testCompile 'org.apache.commons:commons-lang3'
1617
}
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
package io.qameta.allure.attachment;
22

33
import io.qameta.allure.Allure;
4+
import io.qameta.allure.AllureLifecycle;
5+
6+
import java.nio.charset.StandardCharsets;
47

58
/**
69
* @author charlie (Dmitry Baev).
710
*/
811
public class DefaultAttachmentProcessor implements AttachmentProcessor<AttachmentData> {
912

13+
private final AllureLifecycle lifecycle;
14+
15+
public DefaultAttachmentProcessor() {
16+
this(Allure.getLifecycle());
17+
}
18+
19+
public DefaultAttachmentProcessor(final AllureLifecycle lifecycle) {
20+
this.lifecycle = lifecycle;
21+
}
22+
1023
@Override
1124
public void addAttachment(final AttachmentData attachmentData,
1225
final AttachmentRenderer<AttachmentData> renderer) {
1326
final AttachmentContent content = renderer.render(attachmentData);
14-
Allure.addAttachment(
27+
lifecycle.addAttachment(
1528
attachmentData.getName(),
1629
content.getContentType(),
17-
content.getContent(),
18-
content.getFileExtension()
30+
content.getFileExtension(),
31+
content.getContent().getBytes(StandardCharsets.UTF_8)
1932
);
2033
}
2134
}

allure-attachments/src/main/java/io.qameta.allure.attachment/http/HttpRequestAttachment.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public String getName() {
6767
}
6868

6969
/**
70-
* Builder for HttpRequest.
70+
* Builder for HttpRequestAttachment.
7171
*/
7272
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
7373
public static final class Builder {
@@ -95,39 +95,39 @@ public static Builder create(final String attachmentName, final String url) {
9595
return new Builder(attachmentName, url);
9696
}
9797

98-
public Builder method(final String method) {
98+
public Builder withMethod(final String method) {
9999
Objects.requireNonNull(method, "Method must not be null value");
100100
this.method = method;
101101
return this;
102102
}
103103

104-
public Builder header(final String name, final String value) {
104+
public Builder withHeader(final String name, final String value) {
105105
Objects.requireNonNull(name, "Header name must not be null value");
106106
Objects.requireNonNull(value, "Header value must not be null value");
107107
this.headers.put(name, value);
108108
return this;
109109
}
110110

111-
public Builder headers(final Map<String, String> headers) {
111+
public Builder withHeaders(final Map<String, String> headers) {
112112
Objects.requireNonNull(headers, "Headers must not be null value");
113113
this.headers.putAll(headers);
114114
return this;
115115
}
116116

117-
public Builder cookie(final String name, final String value) {
117+
public Builder withCookie(final String name, final String value) {
118118
Objects.requireNonNull(name, "Cookie name must not be null value");
119119
Objects.requireNonNull(value, "Cookie value must not be null value");
120120
this.cookies.put(name, value);
121121
return this;
122122
}
123123

124-
public Builder cookies(final Map<String, String> cookies) {
124+
public Builder withCookies(final Map<String, String> cookies) {
125125
Objects.requireNonNull(cookies, "Cookies must not be null value");
126126
this.cookies.putAll(cookies);
127127
return this;
128128
}
129129

130-
public Builder body(final String body) {
130+
public Builder withBody(final String body) {
131131
Objects.requireNonNull(body, "Body should not be null value");
132132
this.body = body;
133133
return this;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package io.qameta.allure.attachment.http;
2+
3+
import io.qameta.allure.attachment.AttachmentData;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Objects;
8+
9+
/**
10+
* @author charlie (Dmitry Baev).
11+
*/
12+
public class HttpResponseAttachment implements AttachmentData {
13+
14+
private final String name;
15+
16+
private final String url;
17+
18+
private final String body;
19+
20+
private final int responseCode;
21+
22+
private final Map<String, String> headers;
23+
24+
private final Map<String, String> cookies;
25+
26+
public HttpResponseAttachment(final String name, final String url,
27+
final String body, final int responseCode,
28+
final Map<String, String> headers, final Map<String, String> cookies) {
29+
this.name = name;
30+
this.url = url;
31+
this.body = body;
32+
this.responseCode = responseCode;
33+
this.headers = headers;
34+
this.cookies = cookies;
35+
}
36+
37+
@Override
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public String getUrl() {
43+
return url;
44+
}
45+
46+
public String getBody() {
47+
return body;
48+
}
49+
50+
public int getResponseCode() {
51+
return responseCode;
52+
}
53+
54+
public Map<String, String> getHeaders() {
55+
return headers;
56+
}
57+
58+
public Map<String, String> getCookies() {
59+
return cookies;
60+
}
61+
62+
/**
63+
* Builder for HttpRequestAttachment.
64+
*/
65+
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
66+
public static final class Builder {
67+
68+
private final String name;
69+
70+
private String url;
71+
72+
private int responseCode;
73+
74+
private String body;
75+
76+
private final Map<String, String> headers = new HashMap<>();
77+
78+
private final Map<String, String> cookies = new HashMap<>();
79+
80+
private Builder(final String name) {
81+
Objects.requireNonNull(name, "Name must not be null value");
82+
this.name = name;
83+
}
84+
85+
public static Builder create(final String attachmentName) {
86+
return new Builder(attachmentName);
87+
}
88+
89+
public Builder withUrl(final String url) {
90+
Objects.requireNonNull(url, "Url must not be null value");
91+
this.url = url;
92+
return this;
93+
}
94+
95+
public Builder withResponseCode(final int responseCode) {
96+
Objects.requireNonNull(responseCode, "Response code must not be null value");
97+
this.responseCode = responseCode;
98+
return this;
99+
}
100+
101+
public Builder withHeader(final String name, final String value) {
102+
Objects.requireNonNull(name, "Header name must not be null value");
103+
Objects.requireNonNull(value, "Header value must not be null value");
104+
this.headers.put(name, value);
105+
return this;
106+
}
107+
108+
public Builder withHeaders(final Map<String, String> headers) {
109+
Objects.requireNonNull(headers, "Headers must not be null value");
110+
this.headers.putAll(headers);
111+
return this;
112+
}
113+
114+
public Builder withCookie(final String name, final String value) {
115+
Objects.requireNonNull(name, "Cookie name must not be null value");
116+
Objects.requireNonNull(value, "Cookie value must not be null value");
117+
this.cookies.put(name, value);
118+
return this;
119+
}
120+
121+
public Builder withCookies(final Map<String, String> cookies) {
122+
Objects.requireNonNull(cookies, "Cookies must not be null value");
123+
this.cookies.putAll(cookies);
124+
return this;
125+
}
126+
127+
public Builder withBody(final String body) {
128+
Objects.requireNonNull(body, "Body should not be null value");
129+
this.body = body;
130+
return this;
131+
}
132+
133+
public HttpResponseAttachment build() {
134+
return new HttpResponseAttachment(name, url, body, responseCode, headers, cookies);
135+
}
136+
}
137+
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
<#-- @ftlvariable name="data" type="io.qameta.allure.attachment.http.HttpRequestAttachment" -->
2-
<h2>${data.name}</h2>
3-
4-
<h3>Request</h3>
5-
<div>${data.method} to ${data.url}</div>
2+
<div><#if data.method??>${data.method}<#else>GET</#if> to <#if data.url??>${data.url}<#else>Unknown</#if></div>
63

74
<#if data.body??>
8-
<h3>Body</h3>
5+
<h4>Body</h4>
96
<div>
10-
<pre>
7+
<pre class="preformated-text">
118
${data.body}
129
</pre>
1310
</div>
1411
</#if>
1512

16-
<#if data.headers??>
17-
<h3>Headers</h3>
13+
<#if (data.headers)?has_content>
14+
<h4>Headers</h4>
1815
<div>
1916
<#list data.headers as name, value>
2017
<div>${name}: ${value}</div>
@@ -23,16 +20,18 @@
2320
</#if>
2421

2522

26-
<#if data.cookies??>
27-
<h3>Cookies</h3>
23+
<#if (data.cookies)?has_content>
24+
<h4>Cookies</h4>
2825
<div>
2926
<#list data.cookies as name, value>
3027
<div>${name}: ${value}</div>
3128
</#list>
3229
</div>
3330
</#if>
3431

35-
<h3>Curl</h3>
32+
<#if data.curl??>
33+
<h4>Curl</h4>
3634
<div>
3735
${data.curl}
3836
</div>
37+
</#if>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<#-- @ftlvariable name="data" type="io.qameta.allure.attachment.http.HttpResponseAttachment" -->
2+
<div>Status code <#if data.responseCode??>${data.responseCode} <#else>Unknown</#if></div>
3+
<#if data.url??><div>${data.url}</div></#if>
4+
5+
<#if data.body??>
6+
<h4>Body</h4>
7+
<div>
8+
<pre class="preformated-text">
9+
${data.body}
10+
</pre>
11+
</div>
12+
</#if>
13+
14+
<#if (data.headers)?has_content>
15+
<h4>Headers</h4>
16+
<div>
17+
<#list data.headers as name, value>
18+
<div>${name}: ${value}</div>
19+
</#list>
20+
</div>
21+
</#if>
22+
23+
24+
<#if (data.cookies)?has_content>
25+
<h4>Cookies</h4>
26+
<div>
27+
<#list data.cookies as name, value>
28+
<div>${name}: ${value}</div>
29+
</#list>
30+
</div>
31+
</#if>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.qameta.allure.attachment;
2+
3+
import io.qameta.allure.AllureLifecycle;
4+
import io.qameta.allure.attachment.http.HttpRequestAttachment;
5+
import org.junit.Test;
6+
7+
import java.nio.charset.StandardCharsets;
8+
9+
import static io.qameta.allure.attachment.testdata.TestData.randomAttachmentContent;
10+
import static io.qameta.allure.attachment.testdata.TestData.randomHttpRequestAttachment;
11+
import static org.mockito.ArgumentMatchers.eq;
12+
import static org.mockito.Mockito.doReturn;
13+
import static org.mockito.Mockito.mock;
14+
import static org.mockito.Mockito.times;
15+
import static org.mockito.Mockito.verify;
16+
17+
/**
18+
* @author charlie (Dmitry Baev).
19+
*/
20+
public class DefaultAttachmentProcessorTest {
21+
22+
@SuppressWarnings("unchecked")
23+
@Test
24+
public void shouldProcessAttachments() throws Exception {
25+
final HttpRequestAttachment attachment = randomHttpRequestAttachment();
26+
final AllureLifecycle lifecycle = mock(AllureLifecycle.class);
27+
final AttachmentRenderer<AttachmentData> renderer = mock(AttachmentRenderer.class);
28+
final AttachmentContent content = randomAttachmentContent();
29+
doReturn(content)
30+
.when(renderer)
31+
.render(attachment);
32+
33+
new DefaultAttachmentProcessor(lifecycle)
34+
.addAttachment(attachment, renderer);
35+
36+
verify(renderer, times(1)).render(attachment);
37+
verify(lifecycle, times(1))
38+
.addAttachment(
39+
eq(attachment.getName()),
40+
eq(content.getContentType()),
41+
eq(content.getFileExtension()),
42+
eq(content.getContent().getBytes(StandardCharsets.UTF_8))
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)