-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mock: manage headers using RequestHeaders #706
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be68a4f
feat(feign-mock): add RequestHeaders class to manage headers
pboutes 67a5be5
feat(feign-mock): use google code style formatting
pboutes e78d88f
feat(feign-mock): remove system.out
pboutes 2f3b707
feat(RequestKey): add deprecated headers builder + format code
pboutes 6da613b
feat(feign-mock): format pom correctly
pboutes 00cf2b5
feat(feign-mock): format pom correctly
pboutes 8815321
fix(feign-mock): undo some typo and no-op change
pboutes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/** | ||
* Copyright 2012-2018 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.mock; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class RequestHeaders { | ||
|
||
public static class Builder { | ||
|
||
private Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>(); | ||
|
||
private Builder() {} | ||
|
||
public Builder add(String key, Collection<String> values) { | ||
if (!headers.containsKey(key)) { | ||
headers.put(key, values); | ||
} else { | ||
Collection<String> previousValues = headers.get(key); | ||
previousValues.addAll(values); | ||
headers.put(key, previousValues); | ||
} | ||
return this; | ||
} | ||
|
||
public Builder add(String key, String value) { | ||
if (!headers.containsKey(key)) { | ||
headers.put(key, new ArrayList<String>(Arrays.asList(value))); | ||
} else { | ||
final Collection<String> values = headers.get(key); | ||
values.add(value); | ||
headers.put(key, values); | ||
} | ||
return this; | ||
} | ||
|
||
public RequestHeaders build() { | ||
return new RequestHeaders(this); | ||
} | ||
|
||
} | ||
|
||
public static final Map<String, Collection<String>> EMPTY = Collections.emptyMap(); | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static RequestHeaders of(Map<String, Collection<String>> headers) { | ||
return new RequestHeaders(headers); | ||
} | ||
|
||
private Map<String, Collection<String>> headers; | ||
|
||
private RequestHeaders(Builder builder) { | ||
this.headers = builder.headers; | ||
} | ||
|
||
private RequestHeaders(Map<String, Collection<String>> headers) { | ||
this.headers = headers; | ||
} | ||
|
||
public int size() { | ||
return headers.size(); | ||
} | ||
|
||
public int sizeOf(String key) { | ||
if (!headers.containsKey(key)) { | ||
return 0; | ||
} | ||
return headers.get(key).size(); | ||
} | ||
|
||
public Collection<String> fetch(String key) { | ||
return headers.get(key); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
final RequestHeaders other = (RequestHeaders) obj; | ||
return this.headers.equals(other.headers); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder(); | ||
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) { | ||
builder.append(entry).append(',').append(' '); | ||
} | ||
if (builder.length() > 0) { | ||
return builder.substring(0, builder.length() - 2); | ||
} | ||
return "no"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ public static class Builder { | |
|
||
private final String url; | ||
|
||
private Map<String, Collection<String>> headers; | ||
private RequestHeaders headers; | ||
|
||
private Charset charset; | ||
|
||
|
@@ -42,7 +42,13 @@ private Builder(HttpMethod method, String url) { | |
this.url = url; | ||
} | ||
|
||
@Deprecated | ||
public Builder headers(Map<String, Collection<String>> headers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please keep the original headers with |
||
this.headers = RequestHeaders.of(headers); | ||
return this; | ||
} | ||
|
||
public Builder headers(RequestHeaders headers) { | ||
this.headers = headers; | ||
return this; | ||
} | ||
|
@@ -87,7 +93,7 @@ private static String buildUrl(Request request) { | |
|
||
private final String url; | ||
|
||
private final Map<String, Collection<String>> headers; | ||
private final RequestHeaders headers; | ||
|
||
private final Charset charset; | ||
|
||
|
@@ -104,7 +110,7 @@ private RequestKey(Builder builder) { | |
private RequestKey(Request request) { | ||
this.method = HttpMethod.valueOf(request.method()); | ||
this.url = buildUrl(request); | ||
this.headers = request.headers(); | ||
this.headers = RequestHeaders.of(request.headers()); | ||
this.charset = request.charset(); | ||
this.body = request.body(); | ||
} | ||
|
@@ -117,7 +123,7 @@ public String getUrl() { | |
return url; | ||
} | ||
|
||
public Map<String, Collection<String>> getHeaders() { | ||
public RequestHeaders getHeaders() { | ||
return headers; | ||
} | ||
|
||
|
@@ -140,21 +146,23 @@ public int hashCode() { | |
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
if (this == obj) { | ||
return true; | ||
if (obj == null) | ||
} | ||
if (obj == null) { | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
final RequestKey other = (RequestKey) obj; | ||
if (method != other.method) | ||
if (method != other.method) { | ||
return false; | ||
} | ||
if (url == null) { | ||
if (other.url != null) | ||
return false; | ||
} else if (!url.equals(other.url)) | ||
return false; | ||
return true; | ||
return other.url == null; | ||
} else | ||
return url.equals(other.url); | ||
} | ||
|
||
public boolean equalsExtended(Object obj) { | ||
|
@@ -173,7 +181,7 @@ public boolean equalsExtended(Object obj) { | |
@Override | ||
public String toString() { | ||
return String.format("Request [%s %s: %s headers and %s]", method, url, | ||
headers == null ? "without" : "with " + headers.size(), | ||
headers == null ? "without" : "with " + headers, | ||
charset == null ? "no charset" : "charset " + charset); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,12 +91,12 @@ public Object decode(Response response, Type type) | |
} | ||
|
||
private GitHub github; | ||
|
||
private MockClient mockClient; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
try (InputStream input = getClass().getResourceAsStream("/fixtures/contributors.json")) { | ||
InputStream input = getClass().getResourceAsStream("/fixtures/contributors.json"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to undo it. |
||
try { | ||
byte[] data = toByteArray(input); | ||
mockClient = new MockClient(); | ||
github = Feign.builder().decoder(new AssertionDecoder(new GsonDecoder())) | ||
|
@@ -115,7 +115,9 @@ HttpsURLConnection.HTTP_INTERNAL_ERROR, new ByteArrayInputStream(data)) | |
HttpsURLConnection.HTTP_INTERNAL_ERROR, "") | ||
.add(HttpMethod.GET, "/repos/netflix/feign/contributors?client_id=123456789", | ||
HttpsURLConnection.HTTP_INTERNAL_ERROR, data)) | ||
.target(new MockTarget<>(GitHub.class)); | ||
.target(new MockTarget<GitHub>(GitHub.class)); | ||
} finally { | ||
input.close(); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still see a bunch of
No-Op
changes...Would be nice to reduce the number of thoose