|
| 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 | +} |
0 commit comments