|
| 1 | +/* |
| 2 | + * Copyright 2016-2024 Qameta Software Inc |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.qameta.allure.openfeign; |
| 17 | + |
| 18 | +import feign.Request; |
| 19 | +import feign.Response; |
| 20 | +import feign.codec.DecodeException; |
| 21 | +import feign.codec.Decoder; |
| 22 | +import io.qameta.allure.attachment.DefaultAttachmentProcessor; |
| 23 | +import io.qameta.allure.attachment.FreemarkerAttachmentRenderer; |
| 24 | +import io.qameta.allure.attachment.http.HttpRequestAttachment; |
| 25 | +import io.qameta.allure.attachment.http.HttpResponseAttachment; |
| 26 | + |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.lang.reflect.Type; |
| 31 | +import java.nio.charset.Charset; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Objects; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
| 39 | +/** |
| 40 | + * @author sbushmelev (Sergei Bushmelev) |
| 41 | + */ |
| 42 | +public class AllureResponseDecoder implements Decoder { |
| 43 | + |
| 44 | + private final Decoder decoder; |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a new AllureResponseDecoder wrapping the specified decoder. |
| 48 | + * |
| 49 | + * @param decoder the underlying decoder to delegate actual decoding to |
| 50 | + */ |
| 51 | + public AllureResponseDecoder(final Decoder decoder) { |
| 52 | + this.decoder = decoder; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Object decode(final Response response, final Type type) throws IOException { |
| 57 | + final Request request = response.request(); |
| 58 | + |
| 59 | + final HttpRequestAttachment.Builder requestAttachmentBuilder = HttpRequestAttachment |
| 60 | + .Builder.create("Request", request.url()) |
| 61 | + .setMethod(request.httpMethod().name()) |
| 62 | + .setHeaders(headers(request.headers())); |
| 63 | + |
| 64 | + if (Objects.nonNull(request.body())) { |
| 65 | + final Charset charset = request.charset() == null ? StandardCharsets.UTF_8 : request.charset(); |
| 66 | + requestAttachmentBuilder.setBody(new String(request.body(), charset)); |
| 67 | + } |
| 68 | + |
| 69 | + new DefaultAttachmentProcessor().addAttachment( |
| 70 | + requestAttachmentBuilder.build(), |
| 71 | + new FreemarkerAttachmentRenderer("http-request.ftl") |
| 72 | + ); |
| 73 | + |
| 74 | + final HttpResponseAttachment.Builder responseAttachmentBuilder = HttpResponseAttachment |
| 75 | + .Builder.create("Response") |
| 76 | + .setResponseCode(response.status()) |
| 77 | + .setHeaders(headers(response.headers())); |
| 78 | + |
| 79 | + final Response.Builder builder = response.toBuilder(); |
| 80 | + |
| 81 | + if (Objects.nonNull(response.body())) { |
| 82 | + try (InputStream bodyStream = response.body().asInputStream()) { |
| 83 | + final byte[] body = readAllBytes(bodyStream); |
| 84 | + final Charset charset = response.charset() == null ? StandardCharsets.UTF_8 : response.charset(); |
| 85 | + responseAttachmentBuilder.setBody(new String(body, charset)); |
| 86 | + builder.body(body); |
| 87 | + } catch (IOException e) { |
| 88 | + throw new DecodeException(response.status(), "Failed to read response body", request, e); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + new DefaultAttachmentProcessor().addAttachment( |
| 93 | + responseAttachmentBuilder.build(), |
| 94 | + new FreemarkerAttachmentRenderer("http-response.ftl") |
| 95 | + ); |
| 96 | + |
| 97 | + return decoder.decode(builder.build(), type); |
| 98 | + } |
| 99 | + |
| 100 | + private byte[] readAllBytes(final InputStream inputStream) throws IOException { |
| 101 | + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 102 | + int byteRead; |
| 103 | + while ((byteRead = inputStream.read()) != -1) { |
| 104 | + buffer.write(byteRead); |
| 105 | + } |
| 106 | + return buffer.toByteArray(); |
| 107 | + } |
| 108 | + |
| 109 | + private Map<String, String> headers(final Map<String, Collection<String>> headers) { |
| 110 | + if (headers == null) { |
| 111 | + return new HashMap<>(); |
| 112 | + } else { |
| 113 | + return headers.entrySet().stream() |
| 114 | + .collect(Collectors.toMap( |
| 115 | + Map.Entry::getKey, |
| 116 | + entry -> "Set-Cookie".equalsIgnoreCase(entry.getKey()) |
| 117 | + ? String.join("\n", entry.getValue()) |
| 118 | + : String.join(", ", entry.getValue()) |
| 119 | + )); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments