Skip to content

Commit

Permalink
feat(RequestKey): add deprecated headers builder + format code
Browse files Browse the repository at this point in the history
  • Loading branch information
pboutes committed May 17, 2018
1 parent e78d88f commit 2f3b707
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 194 deletions.
90 changes: 45 additions & 45 deletions mock/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,50 @@
the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.openfeign</groupId>
<artifactId>parent</artifactId>
<version>9.8.0-SNAPSHOT</version>
</parent>

<artifactId>feign-mock</artifactId>
<name>Feign Mock</name>
<description>Feign Mock</description>

<properties>
<main.basedir>${project.basedir}/..</main.basedir>

<hamcrest.version>1.3</hamcrest.version>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>feign-core</artifactId>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>feign-gson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.github.openfeign</groupId>
<artifactId>parent</artifactId>
<version>9.8.0-SNAPSHOT</version>
</parent>

<artifactId>feign-mock</artifactId>
<name>Feign Mock</name>
<description>Feign Mock</description>

<properties>
<main.basedir>${project.basedir}/..</main.basedir>

<hamcrest.version>1.3</hamcrest.version>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>feign-core</artifactId>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>feign-gson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
50 changes: 25 additions & 25 deletions mock/src/main/java/feign/mock/MockClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
package feign.mock;

import static feign.Util.UTF_8;

import feign.Client;
import feign.Request;
import feign.Response;
import feign.Util;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
Expand All @@ -29,6 +24,10 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import feign.Client;
import feign.Request;
import feign.Response;
import feign.Util;

public class MockClient implements Client {

Expand All @@ -53,16 +52,15 @@ public RequestResponse(RequestKey requestKey, Response.Builder responseBuilder)

private Iterator<RequestResponse> responseIterator;

public MockClient() {
}
public MockClient() {}

public MockClient(boolean sequential) {
this.sequential = sequential;
}

@Override
public synchronized Response execute(Request request, Request.Options options)
throws IOException {
throws IOException {
RequestKey requestKey = RequestKey.create(request);
Response.Builder responseBuilder;
if (sequential) {
Expand All @@ -86,8 +84,8 @@ private Response.Builder executeSequential(RequestKey requestKey) {
RequestResponse expectedRequestResponse = responseIterator.next();
if (!expectedRequestResponse.requestKey.equalsExtended(requestKey)) {
throw new VerificationAssertionError("Expected %s, but was %s",
expectedRequestResponse.requestKey,
requestKey);
expectedRequestResponse.requestKey,
requestKey);
}

responseBuilder = expectedRequestResponse.responseBuilder;
Expand Down Expand Up @@ -118,14 +116,14 @@ private Response.Builder getResponseBuilder(Request request, RequestKey requestK
}
if (responseBuilder == null) {
responseBuilder =
Response.builder().status(HttpURLConnection.HTTP_NOT_FOUND).reason("Not mocker")
.headers(request.headers());
Response.builder().status(HttpURLConnection.HTTP_NOT_FOUND).reason("Not mocker")
.headers(request.headers());
}
return responseBuilder;
}

public MockClient ok(HttpMethod method, String url, InputStream responseBody)
throws IOException {
throws IOException {
return ok(RequestKey.builder(method, url).build(), responseBody);
}

Expand Down Expand Up @@ -158,7 +156,7 @@ public MockClient ok(RequestKey requestKey) {
}

public MockClient add(HttpMethod method, String url, int status, InputStream responseBody)
throws IOException {
throws IOException {
return add(RequestKey.builder(method, url).build(), status, responseBody);
}

Expand All @@ -175,17 +173,18 @@ public MockClient add(HttpMethod method, String url, int status) {
}

/**
* @param response <ul>
* <li>the status defaults to 0, not 200!</li>
* <li>the internal feign-code requires the headers to be set</li>
* </ul>
* @param response
* <ul>
* <li>the status defaults to 0, not 200!</li>
* <li>the internal feign-code requires the headers to be set</li>
* </ul>
*/
public MockClient add(HttpMethod method, String url, Response.Builder response) {
return add(RequestKey.builder(method, url).build(), response);
}

public MockClient add(RequestKey requestKey, int status, InputStream responseBody)
throws IOException {
throws IOException {
return add(requestKey, status, Util.toByteArray(responseBody));
}

Expand All @@ -195,8 +194,8 @@ public MockClient add(RequestKey requestKey, int status, String responseBody) {

public MockClient add(RequestKey requestKey, int status, byte[] responseBody) {
return add(requestKey,
Response.builder().status(status).reason("Mocked").headers(RequestHeaders.EMPTY)
.body(responseBody));
Response.builder().status(status).reason("Mocked").headers(RequestHeaders.EMPTY)
.body(responseBody));
}

public MockClient add(RequestKey requestKey, int status) {
Expand Down Expand Up @@ -238,9 +237,9 @@ public List<Request> verifyTimes(final HttpMethod method, final String url, fina
List<Request> result = requests.get(requestKey);
if (result.size() != times) {
throw new VerificationAssertionError(
"Wanted: '%s' to be invoked: '%s' times but got: '%s'!",
requestKey,
times, result.size());
"Wanted: '%s' to be invoked: '%s' times but got: '%s'!",
requestKey,
times, result.size());
}

return result;
Expand All @@ -250,7 +249,7 @@ public void verifyNever(HttpMethod method, String url) {
RequestKey requestKey = RequestKey.builder(method, url).build();
if (requests.containsKey(requestKey)) {
throw new VerificationAssertionError("Do not wanted: '%s' but was invoked!",
requestKey);
requestKey);
}
}

Expand All @@ -277,4 +276,5 @@ public void resetRequests() {
requests.clear();
}


}
9 changes: 4 additions & 5 deletions mock/src/main/java/feign/mock/RequestHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public static class Builder {

private Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>();

private Builder() {
}
private Builder() {}

public Builder add(String key, Collection<String> values) {
if (!headers.containsKey(key)) {
Expand Down Expand Up @@ -57,12 +56,12 @@ public RequestHeaders build() {

}

public static final Map<String, Collection<String>> EMPTY = Collections.emptyMap();

public static Builder builder() {
return new Builder();
}

public static final Map<String, Collection<String>> EMPTY = Collections.emptyMap();

public static RequestHeaders of(Map<String, Collection<String>> headers) {
return new RequestHeaders(headers);
}
Expand Down Expand Up @@ -92,7 +91,6 @@ public Collection<String> fetch(String key) {
return headers.get(key);
}


@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand All @@ -119,4 +117,5 @@ public String toString() {
}
return "no";
}

}
31 changes: 17 additions & 14 deletions mock/src/main/java/feign/mock/RequestKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
package feign.mock;

import static feign.Util.UTF_8;

import feign.Request;
import feign.Util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import feign.Request;
import feign.Util;

public class RequestKey {

Expand All @@ -41,6 +42,12 @@ private Builder(HttpMethod method, String url) {
this.url = url;
}

@Deprecated
public Builder headers(Map<String, Collection<String>> headers) {
this.headers = RequestHeaders.of(headers);
return this;
}

public Builder headers(RequestHeaders headers) {
this.headers = headers;
return this;
Expand Down Expand Up @@ -153,22 +160,18 @@ public boolean equals(Object obj) {
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) {
if (equals(obj)) {
RequestKey other = (RequestKey) obj;
boolean headersEqual =
other.headers == null || headers == null || headers.equals(other.headers);
other.headers == null || headers == null || headers.equals(other.headers);
boolean charsetEqual =
other.charset == null || charset == null || charset.equals(other.charset);
other.charset == null || charset == null || charset.equals(other.charset);
boolean bodyEqual = other.body == null || body == null || Arrays.equals(other.body, body);
return headersEqual && charsetEqual && bodyEqual;
}
Expand All @@ -178,8 +181,8 @@ 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,
charset == null ? "no charset" : "charset " + charset);
headers == null ? "without" : "with " + headers,
charset == null ? "no charset" : "charset " + charset);
}

}
Loading

0 comments on commit 2f3b707

Please sign in to comment.