Repository navigation
Expand file tree
/
Copy pathRecordedRequestAssert.java
More file actions
153 lines (137 loc) · 4.91 KB
/
Copy pathRecordedRequestAssert.java
File metadata and controls
153 lines (137 loc) · 4.91 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
/**
* Copyright 2012-2020 The Feign Authors
*
* 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 feign.assertj;
import java.util.Arrays;
import java.util.Collection;
import okhttp3.Headers;
import okhttp3.mockwebserver.RecordedRequest;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.data.MapEntry;
import org.assertj.core.internal.ByteArrays;
import org.assertj.core.internal.Failures;
import org.assertj.core.internal.Maps;
import org.assertj.core.internal.Objects;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
import feign.Util;
import static org.assertj.core.data.MapEntry.entry;
import static org.assertj.core.error.ShouldNotContain.shouldNotContain;
public final class RecordedRequestAssert
extends AbstractAssert<RecordedRequestAssert, RecordedRequest> {
ByteArrays arrays = ByteArrays.instance();
Objects objects = Objects.instance();
Maps maps = Maps.instance();
Failures failures = Failures.instance();
public RecordedRequestAssert(RecordedRequest actual) {
super(actual, RecordedRequestAssert.class);
}
public RecordedRequestAssert hasMethod(String expected) {
isNotNull();
objects.assertEqual(info, actual.getMethod(), expected);
return this;
}
public RecordedRequestAssert hasPath(String expected) {
isNotNull();
objects.assertEqual(info, actual.getPath(), expected);
return this;
}
public RecordedRequestAssert hasQueryParams(String... expectedParams) {
return hasQueryParams(Arrays.asList(expectedParams));
}
public RecordedRequestAssert hasQueryParams(Collection<String> expectedParams) {
isNotNull();
Collection<String> actualQueryParams = getQueryParams();
objects.assertEqual(info, expectedParams.size(), actualQueryParams.size());
for (String expectedParam : expectedParams) {
objects.assertIsIn(info, expectedParam, actualQueryParams);
}
return this;
}
private Collection<String> getQueryParams() {
String path = actual.getPath();
int queryStart = path.indexOf("?") + 1;
String[] queryParams = actual.getPath()
.substring(queryStart)
.split("&");
return Arrays.asList(queryParams);
}
public RecordedRequestAssert hasOneOfPath(String... expected) {
isNotNull();
objects.assertIsIn(info, actual.getPath(), expected);
return this;
}
public RecordedRequestAssert hasBody(String utf8Expected) {
isNotNull();
objects.assertEqual(info, actual.getBody().readUtf8(), utf8Expected);
return this;
}
public RecordedRequestAssert hasGzippedBody(byte[] expectedUncompressed) {
isNotNull();
byte[] compressedBody = actual.getBody().readByteArray();
byte[] uncompressedBody;
try {
uncompressedBody =
Util.toByteArray(new GZIPInputStream(new ByteArrayInputStream(compressedBody)));
} catch (IOException e) {
throw new RuntimeException(e);
}
arrays.assertContains(info, uncompressedBody, expectedUncompressed);
return this;
}
public RecordedRequestAssert hasDeflatedBody(byte[] expectedUncompressed) {
isNotNull();
byte[] compressedBody = actual.getBody().readByteArray();
byte[] uncompressedBody;
try {
uncompressedBody =
Util.toByteArray(new InflaterInputStream(new ByteArrayInputStream(compressedBody)));
} catch (IOException e) {
throw new RuntimeException(e);
}
arrays.assertContains(info, uncompressedBody, expectedUncompressed);
return this;
}
public RecordedRequestAssert hasBody(byte[] expected) {
isNotNull();
arrays.assertContains(info, actual.getBody().readByteArray(), expected);
return this;
}
public RecordedRequestAssert hasHeaders(MapEntry... expected) {
isNotNull();
maps.assertContains(info, actual.getHeaders().toMultimap(), expected);
return this;
}
public RecordedRequestAssert hasNoHeaderNamed(final String... names) {
isNotNull();
Set<String> found = new LinkedHashSet<String>();
for (String header : actual.getHeaders().names()) {
for (String name : names) {
if (header.equalsIgnoreCase(name)) {
found.add(header);
}
}
}
if (found.isEmpty()) {
return this;
}
throw failures.failure(info, shouldNotContain(actual.getHeaders(), names, found));
}
}