1
2
3
4 package com.azure.common.http;
5
6 import com.azure.common.implementation.serializer.SerializerAdapter;
7 import com.azure.common.implementation.serializer.SerializerEncoding;
8 import com.azure.common.implementation.serializer.jackson.JacksonAdapter;
9 import io.netty.buffer.ByteBuf;
10 import io.netty.buffer.Unpooled;
11 import reactor.core.publisher.Flux;
12 import reactor.core.publisher.Mono;
13
14 import java.io.IOException;
15 import java.nio.charset.Charset;
16 import java.nio.charset.StandardCharsets;
17
18 public class MockHttpResponse extends HttpResponse {
19 private static final SerializerAdapter SERIALIZER = new JacksonAdapter();
20
21 private final int statusCode;
22
23 private final HttpHeaders headers;
24
25 private final byte[] bodyBytes;
26
27 public MockHttpResponse(HttpRequest request, int statusCode, HttpHeaders headers, byte[] bodyBytes) {
28 this.statusCode = statusCode;
29 this.headers = headers;
30 this.bodyBytes = bodyBytes;
31 this.withRequest(request);
32 }
33
34 public MockHttpResponse(HttpRequest request, int statusCode) {
35 this(request, statusCode, new HttpHeaders(), new byte[0]);
36 }
37
38 public MockHttpResponse(HttpRequest request, int statusCode, HttpHeaders headers) {
39 this(request, statusCode, headers, new byte[0]);
40 }
41
42 public MockHttpResponse(HttpRequest request, int statusCode, HttpHeaders headers, Object serializable) {
43 this(request, statusCode, headers, serialize(serializable));
44 }
45
46 public MockHttpResponse(HttpRequest request, int statusCode, Object serializable) {
47 this(request, statusCode, new HttpHeaders(), serialize(serializable));
48 }
49
50 private static byte[] serialize(Object serializable) {
51 byte[] result = null;
52 try {
53 final String serializedString = SERIALIZER.serialize(serializable, SerializerEncoding.JSON);
54 result = serializedString == null ? null : serializedString.getBytes();
55 } catch (IOException e) {
56 e.printStackTrace();
57 }
58 return result;
59 }
60
61 @Override
62 public int statusCode() {
63 return statusCode;
64 }
65
66 @Override
67 public String headerValue(String name) {
68 return headers.value(name);
69 }
70
71 @Override
72 public HttpHeaders headers() {
73 return new HttpHeaders(headers);
74 }
75
76 @Override
77 public Mono<byte[]> bodyAsByteArray() {
78 if (bodyBytes == null) {
79 return Mono.empty();
80 } else {
81 return Mono.just(bodyBytes);
82 }
83 }
84
85 @Override
86 public Flux<ByteBuf> body() {
87 if (bodyBytes == null) {
88 return Flux.empty();
89 } else {
90 return Flux.just(Unpooled.wrappedBuffer(bodyBytes));
91 }
92 }
93
94 @Override
95 public Mono<String> bodyAsString() {
96 if (bodyBytes == null) {
97 return Mono.empty();
98 } else {
99 return Mono.just(new String(bodyBytes, StandardCharsets.UTF_8));
100 }
101 }
102
103 @Override
104 public Mono<String> bodyAsString(Charset charset) {
105 if (bodyBytes == null) {
106 return Mono.empty();
107 } else {
108 return Mono.just(new String(bodyBytes, charset));
109 }
110 }
111 }