io.qameta.allure.attachment.http.HttpRequestAttachment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of allure-attachments Show documentation
Show all versions of allure-attachments Show documentation
Module allure-attachments of Allure Framework.
/*
* 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.attachment.http;
import io.qameta.allure.attachment.AttachmentData;
import io.qameta.allure.util.ObjectUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* @author charlie (Dmitry Baev).
*/
public class HttpRequestAttachment implements AttachmentData {
private final String name;
private final String url;
private final String method;
private final String body;
private final String curl;
private final Map headers;
private final Map cookies;
public HttpRequestAttachment(final String name, final String url, final String method,
final String body, final String curl, final Map headers,
final Map cookies) {
this.name = name;
this.url = url;
this.method = method;
this.body = body;
this.curl = curl;
this.headers = headers;
this.cookies = cookies;
}
public String getUrl() {
return url;
}
public String getMethod() {
return method;
}
public String getBody() {
return body;
}
public Map getHeaders() {
return headers;
}
public Map getCookies() {
return cookies;
}
public String getCurl() {
return curl;
}
@Override
public String getName() {
return name;
}
@Override
public String toString() {
return "HttpRequestAttachment("
+ "\n\tname=" + this.name
+ ",\n\turl=" + this.url
+ ",\n\tbody=" + this.body
+ ",\n\theaders=" + ObjectUtils.mapToString(this.headers)
+ ",\n\tcookies=" + ObjectUtils.mapToString(this.cookies)
+ "\n)";
}
/**
* Builder for HttpRequestAttachment.
*/
@SuppressWarnings("PMD.AvoidFieldNameMatchingMethodName")
public static final class Builder {
private final String name;
private final String url;
private String method;
private String body;
private final Map headers = new HashMap<>();
private final Map cookies = new HashMap<>();
private Builder(final String name, final String url) {
Objects.requireNonNull(name, "Name must not be null value");
Objects.requireNonNull(url, "Url must not be null value");
this.name = name;
this.url = url;
}
public static Builder create(final String attachmentName, final String url) {
return new Builder(attachmentName, url);
}
public Builder setMethod(final String method) {
Objects.requireNonNull(method, "Method must not be null value");
this.method = method;
return this;
}
public Builder setHeader(final String name, final String value) {
Objects.requireNonNull(name, "Header name must not be null value");
Objects.requireNonNull(value, "Header value must not be null value");
this.headers.put(name, value);
return this;
}
public Builder setHeaders(final Map headers) {
Objects.requireNonNull(headers, "Headers must not be null value");
this.headers.putAll(headers);
return this;
}
public Builder setCookie(final String name, final String value) {
Objects.requireNonNull(name, "Cookie name must not be null value");
Objects.requireNonNull(value, "Cookie value must not be null value");
this.cookies.put(name, value);
return this;
}
public Builder setCookies(final Map cookies) {
Objects.requireNonNull(cookies, "Cookies must not be null value");
this.cookies.putAll(cookies);
return this;
}
public Builder setBody(final String body) {
Objects.requireNonNull(body, "Body should not be null value");
this.body = body;
return this;
}
/**
* Use setter method instead.
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
public Builder withMethod(final String method) {
return setMethod(method);
}
/**
* Use setter method instead.
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
public Builder withHeader(final String name, final String value) {
return setHeader(name, value);
}
/**
* Use setter method instead.
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
public Builder withHeaders(final Map headers) {
return setHeaders(headers);
}
/**
* Use setter method instead.
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
public Builder withCookie(final String name, final String value) {
return setCookie(name, value);
}
/**
* Use setter method instead.
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
public Builder withCookies(final Map cookies) {
return setCookies(cookies);
}
/**
* Use setter method instead.
* @deprecated scheduled for removal in 3.0 release
*/
@Deprecated
public Builder withBody(final String body) {
return setBody(body);
}
public HttpRequestAttachment build() {
return new HttpRequestAttachment(name, url, method, body, getCurl(), headers, cookies);
}
private String getCurl() {
final StringBuilder builder = new StringBuilder("curl -v");
if (Objects.nonNull(method)) {
builder.append(" -X ").append(method);
}
builder.append(" '").append(url).append('\'');
headers.forEach((key, value) -> appendHeader(builder, key, value));
cookies.forEach((key, value) -> appendCookie(builder, key, value));
if (Objects.nonNull(body)) {
builder.append(" -d '").append(body).append('\'');
}
return builder.toString();
}
private static void appendHeader(final StringBuilder builder, final String key, final String value) {
builder.append(" -H '")
.append(key)
.append(": ")
.append(value)
.append('\'');
}
private static void appendCookie(final StringBuilder builder, final String key, final String value) {
builder.append(" -b '")
.append(key)
.append('=')
.append(value)
.append('\'');
}
}
}