-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathAllureRestAssured.java
More file actions
123 lines (103 loc) · 4.58 KB
/
AllureRestAssured.java
File metadata and controls
123 lines (103 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2019 Qameta Software OÜ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.restassured;
import io.qameta.allure.attachment.DefaultAttachmentProcessor;
import io.qameta.allure.attachment.FreemarkerAttachmentRenderer;
import io.qameta.allure.attachment.http.HttpRequestAttachment;
import io.qameta.allure.attachment.http.HttpResponseAttachment;
import io.restassured.filter.FilterContext;
import io.restassured.filter.OrderedFilter;
import io.restassured.internal.NameAndValue;
import io.restassured.internal.support.Prettifier;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static io.qameta.allure.attachment.http.HttpRequestAttachment.Builder.create;
import static io.qameta.allure.attachment.http.HttpResponseAttachment.Builder.create;
/**
* Allure logger filter for Rest-assured.
*/
public class AllureRestAssured implements OrderedFilter {
private String requestTemplatePath = "http-request.ftl";
private String responseTemplatePath = "http-response.ftl";
public AllureRestAssured setRequestTemplate(final String templatePath) {
this.requestTemplatePath = templatePath;
return this;
}
public AllureRestAssured setResponseTemplate(final String templatePath) {
this.responseTemplatePath = templatePath;
return this;
}
/**
* @deprecated use {@link #setRequestTemplate(String)} instead.
* Scheduled for removal in 3.0 release.
*/
@Deprecated
public AllureRestAssured withRequestTemplate(final String templatePath) {
return setRequestTemplate(templatePath);
}
/**
* @deprecated use {@link #setResponseTemplate(String)} instead.
* Scheduled for removal in 3.0 release.
*/
@Deprecated
public AllureRestAssured withResponseTemplate(final String templatePath) {
return setResponseTemplate(templatePath);
}
@Override
public Response filter(final FilterableRequestSpecification requestSpec,
final FilterableResponseSpecification responseSpec,
final FilterContext filterContext) {
final Prettifier prettifier = new Prettifier();
final HttpRequestAttachment.Builder requestAttachmentBuilder = create("Request", requestSpec.getURI())
.setRequestTime()
.setMethod(requestSpec.getMethod())
.setHeaders(toMapConverter(requestSpec.getHeaders()))
.setCookies(toMapConverter(requestSpec.getCookies()));
if (Objects.nonNull(requestSpec.getBody())) {
requestAttachmentBuilder.setBody(prettifier.getPrettifiedBodyIfPossible(requestSpec));
}
final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();
new DefaultAttachmentProcessor().addAttachment(
requestAttachment,
new FreemarkerAttachmentRenderer(requestTemplatePath)
);
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()))
.build();
new DefaultAttachmentProcessor().addAttachment(
responseAttachment,
new FreemarkerAttachmentRenderer(responseTemplatePath)
);
return response;
}
private static Map<String, String> toMapConverter(final Iterable<? extends NameAndValue> items) {
final Map<String, String> result = new HashMap<>();
items.forEach(h -> result.put(h.getName(), h.getValue()));
return result;
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}