-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathAllureRestAssured.java
More file actions
178 lines (148 loc) · 6.86 KB
/
AllureRestAssured.java
File metadata and controls
178 lines (148 loc) · 6.86 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright 2016-2026 Qameta Software Inc
*
* 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.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import static io.qameta.allure.attachment.http.HttpRequestAttachment.Builder.create;
import static java.util.Optional.ofNullable;
/**
* Allure logger filter for Rest-assured.
*/
public class AllureRestAssured implements OrderedFilter {
private static final String HIDDEN_PLACEHOLDER = "[ BLACKLISTED ]";
private static final String REST_ASSURED_NO_PARAMETER_VALUE_CLASS =
"io.restassured.internal.NoParameterValue";
private int maxAllowedPrettifyLength = 1_048_576;
private String requestTemplatePath = "http-request.ftl";
private String responseTemplatePath = "http-response.ftl";
private String requestAttachmentName = "Request";
private String responseAttachmentName;
public AllureRestAssured setMaxAllowedPrettifyLength(final int maxAllowedPrettifyLength) {
this.maxAllowedPrettifyLength = maxAllowedPrettifyLength;
return this;
}
public AllureRestAssured setRequestTemplate(final String templatePath) {
this.requestTemplatePath = templatePath;
return this;
}
public AllureRestAssured setResponseTemplate(final String templatePath) {
this.responseTemplatePath = templatePath;
return this;
}
public AllureRestAssured setRequestAttachmentName(final String requestAttachmentName) {
this.requestAttachmentName = requestAttachmentName;
return this;
}
public AllureRestAssured setResponseAttachmentName(final String responseAttachmentName) {
this.responseAttachmentName = responseAttachmentName;
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 String url = requestSpec.getURI();
final Set<String> hiddenHeaders = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
hiddenHeaders.addAll(Objects.requireNonNull(requestSpec.getConfig().getLogConfig().blacklistedHeaders()));
final HttpRequestAttachment.Builder requestAttachmentBuilder = create(requestAttachmentName, url)
.setMethod(requestSpec.getMethod())
.setHeaders(toMapConverter(requestSpec.getHeaders(), hiddenHeaders))
.setCookies(toMapConverter(requestSpec.getCookies(), new HashSet<>()));
if (Objects.nonNull(requestSpec.getBody())) {
requestAttachmentBuilder.setBody(prettifier.getPrettifiedBodyIfPossible(requestSpec));
}
if (Objects.nonNull(requestSpec.getFormParams())) {
requestAttachmentBuilder.setFormParams(normalizeFormParams(requestSpec.getFormParams()));
}
final HttpRequestAttachment requestAttachment = requestAttachmentBuilder.build();
new DefaultAttachmentProcessor().addAttachment(
requestAttachment,
new FreemarkerAttachmentRenderer(requestTemplatePath)
);
final Response response = filterContext.next(requestSpec, responseSpec);
final String attachmentName = ofNullable(responseAttachmentName)
.orElse(response.getStatusLine());
final String responseAsString = response.getBody().asString();
final String body = responseAsString.length() > maxAllowedPrettifyLength
? responseAsString
: prettifier.getPrettifiedBodyIfPossible(response, response.getBody());
final HttpResponseAttachment responseAttachment = HttpResponseAttachment.Builder.create(attachmentName)
.setResponseCode(response.getStatusCode())
.setHeaders(toMapConverter(response.getHeaders(), hiddenHeaders))
.setBody(body)
.build();
new DefaultAttachmentProcessor().addAttachment(
responseAttachment,
new FreemarkerAttachmentRenderer(responseTemplatePath)
);
return response;
}
private static Map<String, String> toMapConverter(final Iterable<? extends NameAndValue> items,
final Set<String> toHide) {
final Map<String, String> result = new HashMap<>();
items.forEach(h -> result.put(h.getName(), toHide.contains(h.getName()) ? HIDDEN_PLACEHOLDER : h.getValue()));
return result;
}
private static Map<String, String> normalizeFormParams(final Map<?, ?> formParams) {
final Map<String, String> result = new HashMap<>();
formParams.forEach((key, value) -> {
if (value == null) {
return;
}
if (REST_ASSURED_NO_PARAMETER_VALUE_CLASS.equals(value.getClass().getName())) {
return;
}
result.put(String.valueOf(key), String.valueOf(value));
});
return result;
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}