Skip to content

Commit c008ac8

Browse files
pboutesvelo
authored andcommitted
Mock: manage headers using RequestHeaders (#706)
* feat(feign-mock): add RequestHeaders class to manage headers * feat(feign-mock): use google code style formatting * feat(feign-mock): remove system.out * feat(RequestKey): add deprecated headers builder + format code * feat(feign-mock): format pom correctly * feat(feign-mock): format pom correctly * fix(feign-mock): undo some typo and no-op change
1 parent b12b57c commit c008ac8

File tree

8 files changed

+264
-42
lines changed

8 files changed

+264
-42
lines changed

mock/src/main/java/feign/mock/MockClient.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.net.HttpURLConnection;
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
22-
import java.util.Collection;
2322
import java.util.Collections;
2423
import java.util.HashMap;
2524
import java.util.Iterator;
@@ -45,8 +44,6 @@ public RequestResponse(RequestKey requestKey, Response.Builder responseBuilder)
4544

4645
}
4746

48-
public static final Map<String, Collection<String>> EMPTY_HEADERS = Collections.emptyMap();
49-
5047
private final List<RequestResponse> responses = new ArrayList<RequestResponse>();
5148

5249
private final Map<RequestKey, List<Request>> requests = new HashMap<RequestKey, List<Request>>();
@@ -196,7 +193,7 @@ public MockClient add(RequestKey requestKey, int status, String responseBody) {
196193

197194
public MockClient add(RequestKey requestKey, int status, byte[] responseBody) {
198195
return add(requestKey,
199-
Response.builder().status(status).reason("Mocked").headers(EMPTY_HEADERS)
196+
Response.builder().status(status).reason("Mocked").headers(RequestHeaders.EMPTY)
200197
.body(responseBody));
201198
}
202199

@@ -255,7 +252,7 @@ public void verifyNever(HttpMethod method, String url) {
255252

256253
/**
257254
* To be called in an &#64;After method:
258-
*
255+
*
259256
* <pre>
260257
* &#64;After
261258
* public void tearDown() {
@@ -276,4 +273,5 @@ public void resetRequests() {
276273
requests.clear();
277274
}
278275

276+
279277
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* Copyright 2012-2018 The Feign Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package feign.mock;
15+
16+
import java.util.ArrayList;
17+
import java.util.Arrays;
18+
import java.util.Collection;
19+
import java.util.Collections;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
public class RequestHeaders {
24+
25+
public static class Builder {
26+
27+
private Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>();
28+
29+
private Builder() {}
30+
31+
public Builder add(String key, Collection<String> values) {
32+
if (!headers.containsKey(key)) {
33+
headers.put(key, values);
34+
} else {
35+
Collection<String> previousValues = headers.get(key);
36+
previousValues.addAll(values);
37+
headers.put(key, previousValues);
38+
}
39+
return this;
40+
}
41+
42+
public Builder add(String key, String value) {
43+
if (!headers.containsKey(key)) {
44+
headers.put(key, new ArrayList<String>(Arrays.asList(value)));
45+
} else {
46+
final Collection<String> values = headers.get(key);
47+
values.add(value);
48+
headers.put(key, values);
49+
}
50+
return this;
51+
}
52+
53+
public RequestHeaders build() {
54+
return new RequestHeaders(this);
55+
}
56+
57+
}
58+
59+
public static final Map<String, Collection<String>> EMPTY = Collections.emptyMap();
60+
61+
public static Builder builder() {
62+
return new Builder();
63+
}
64+
65+
public static RequestHeaders of(Map<String, Collection<String>> headers) {
66+
return new RequestHeaders(headers);
67+
}
68+
69+
private Map<String, Collection<String>> headers;
70+
71+
private RequestHeaders(Builder builder) {
72+
this.headers = builder.headers;
73+
}
74+
75+
private RequestHeaders(Map<String, Collection<String>> headers) {
76+
this.headers = headers;
77+
}
78+
79+
public int size() {
80+
return headers.size();
81+
}
82+
83+
public int sizeOf(String key) {
84+
if (!headers.containsKey(key)) {
85+
return 0;
86+
}
87+
return headers.get(key).size();
88+
}
89+
90+
public Collection<String> fetch(String key) {
91+
return headers.get(key);
92+
}
93+
94+
@Override
95+
public boolean equals(Object obj) {
96+
if (this == obj) {
97+
return true;
98+
}
99+
if (obj == null) {
100+
return false;
101+
}
102+
if (getClass() != obj.getClass()) {
103+
return false;
104+
}
105+
final RequestHeaders other = (RequestHeaders) obj;
106+
return this.headers.equals(other.headers);
107+
}
108+
109+
@Override
110+
public String toString() {
111+
StringBuilder builder = new StringBuilder();
112+
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
113+
builder.append(entry).append(',').append(' ');
114+
}
115+
if (builder.length() > 0) {
116+
return builder.substring(0, builder.length() - 2);
117+
}
118+
return "no";
119+
}
120+
121+
}

mock/src/main/java/feign/mock/RequestKey.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static class Builder {
3131

3232
private final String url;
3333

34-
private Map<String, Collection<String>> headers;
34+
private RequestHeaders headers;
3535

3636
private Charset charset;
3737

@@ -42,7 +42,13 @@ private Builder(HttpMethod method, String url) {
4242
this.url = url;
4343
}
4444

45+
@Deprecated
4546
public Builder headers(Map<String, Collection<String>> headers) {
47+
this.headers = RequestHeaders.of(headers);
48+
return this;
49+
}
50+
51+
public Builder headers(RequestHeaders headers) {
4652
this.headers = headers;
4753
return this;
4854
}
@@ -87,7 +93,7 @@ private static String buildUrl(Request request) {
8793

8894
private final String url;
8995

90-
private final Map<String, Collection<String>> headers;
96+
private final RequestHeaders headers;
9197

9298
private final Charset charset;
9399

@@ -104,7 +110,7 @@ private RequestKey(Builder builder) {
104110
private RequestKey(Request request) {
105111
this.method = HttpMethod.valueOf(request.method());
106112
this.url = buildUrl(request);
107-
this.headers = request.headers();
113+
this.headers = RequestHeaders.of(request.headers());
108114
this.charset = request.charset();
109115
this.body = request.body();
110116
}
@@ -117,7 +123,7 @@ public String getUrl() {
117123
return url;
118124
}
119125

120-
public Map<String, Collection<String>> getHeaders() {
126+
public RequestHeaders getHeaders() {
121127
return headers;
122128
}
123129

@@ -140,21 +146,23 @@ public int hashCode() {
140146

141147
@Override
142148
public boolean equals(Object obj) {
143-
if (this == obj)
149+
if (this == obj) {
144150
return true;
145-
if (obj == null)
151+
}
152+
if (obj == null) {
146153
return false;
147-
if (getClass() != obj.getClass())
154+
}
155+
if (getClass() != obj.getClass()) {
148156
return false;
157+
}
149158
final RequestKey other = (RequestKey) obj;
150-
if (method != other.method)
159+
if (method != other.method) {
151160
return false;
161+
}
152162
if (url == null) {
153-
if (other.url != null)
154-
return false;
155-
} else if (!url.equals(other.url))
156-
return false;
157-
return true;
163+
return other.url == null;
164+
} else
165+
return url.equals(other.url);
158166
}
159167

160168
public boolean equalsExtended(Object obj) {
@@ -173,7 +181,7 @@ public boolean equalsExtended(Object obj) {
173181
@Override
174182
public String toString() {
175183
return String.format("Request [%s %s: %s headers and %s]", method, url,
176-
headers == null ? "without" : "with " + headers.size(),
184+
headers == null ? "without" : "with " + headers,
177185
charset == null ? "no charset" : "charset " + charset);
178186
}
179187

mock/src/test/java/feign/mock/MockClientSequentialTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import feign.Body;
3232
import feign.Feign;
3333
import feign.FeignException;
34+
import feign.Headers;
3435
import feign.Param;
3536
import feign.RequestLine;
3637
import feign.Response;
@@ -42,6 +43,7 @@ public class MockClientSequentialTest {
4243

4344
interface GitHub {
4445

46+
@Headers({"Name: {owner}"})
4547
@RequestLine("GET /repos/{owner}/{repo}/contributors")
4648
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
4749

@@ -89,26 +91,29 @@ public Object decode(Response response, Type type)
8991
}
9092

9193
private GitHub githubSequential;
92-
9394
private MockClient mockClientSequential;
9495

9596
@Before
9697
public void setup() throws IOException {
9798
try (InputStream input = getClass().getResourceAsStream("/fixtures/contributors.json")) {
9899
byte[] data = toByteArray(input);
99-
100+
RequestHeaders headers = RequestHeaders
101+
.builder()
102+
.add("Name", "netflix")
103+
.build();
100104
mockClientSequential = new MockClient(true);
101105
githubSequential = Feign.builder().decoder(new AssertionDecoder(new GsonDecoder()))
102106
.client(mockClientSequential
103-
.add(HttpMethod.GET, "/repos/netflix/feign/contributors", HttpsURLConnection.HTTP_OK,
104-
data)
107+
.add(RequestKey
108+
.builder(HttpMethod.GET, "/repos/netflix/feign/contributors")
109+
.headers(headers).build(), HttpsURLConnection.HTTP_OK, data)
105110
.add(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=55",
106111
HttpsURLConnection.HTTP_NOT_FOUND)
107112
.add(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=7 7",
108113
HttpsURLConnection.HTTP_INTERNAL_ERROR, new ByteArrayInputStream(data))
109114
.add(HttpMethod.GET, "/repos/netflix/feign/contributors",
110115
Response.builder().status(HttpsURLConnection.HTTP_OK)
111-
.headers(MockClient.EMPTY_HEADERS).body(data)))
116+
.headers(RequestHeaders.EMPTY).body(data)))
112117
.target(new MockTarget<>(GitHub.class));
113118
}
114119
}

mock/src/test/java/feign/mock/MockClientTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ public Object decode(Response response, Type type)
9191
}
9292

9393
private GitHub github;
94-
9594
private MockClient mockClient;
9695

9796
@Before

mock/src/test/java/feign/mock/MockTargetTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package feign.mock;
1515

1616
import static org.hamcrest.Matchers.equalTo;
17-
import static org.junit.Assert.*;
17+
import static org.junit.Assert.assertThat;
1818
import org.junit.Before;
1919
import org.junit.Test;
2020

0 commit comments

Comments
 (0)