diff --git a/CHANGELOG.md b/CHANGELOG.md index baabf3a73..d0cb3f00e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.11.09 +* Expected body param values for Mock expects need to be url encoded +* Support ANY expectation on methods for MockClient. (e.g. ```expect(HttpMethod.GET)``) + ## 3.11.08 * Adds new body matchers to the Mock client for asserting multipart forms. diff --git a/unirest-mocks/src/main/java/kong/unirest/FieldMatcher.java b/unirest-mocks/src/main/java/kong/unirest/FieldMatcher.java index 462f0ccb1..3b0c30c43 100644 --- a/unirest-mocks/src/main/java/kong/unirest/FieldMatcher.java +++ b/unirest-mocks/src/main/java/kong/unirest/FieldMatcher.java @@ -25,6 +25,7 @@ package kong.unirest; +import java.net.URLEncoder; import java.util.*; /** @@ -56,7 +57,7 @@ public MatchStatus matches(List body) throws AssertionError { List missing = new ArrayList<>(); boolean pass = true; for (Map.Entry r : formParams.entrySet()) { - String expectedParam = r.getKey() + "=" + r.getValue(); + String expectedParam = r.getKey() + "=" + URLEncoder.encode(r.getValue()); if (body.stream().noneMatch(p -> Objects.equals(expectedParam, p))) { missing.add(expectedParam); pass = false; @@ -70,7 +71,7 @@ private String description(boolean pass, List missing) { if(pass){ return ""; } - StringJoiner joiner = new StringJoiner(System.lineSeparator() + "", "Missing Expected Fields. Expected: " +System.lineSeparator(),""); + StringJoiner joiner = new StringJoiner(System.lineSeparator()); missing.forEach(m -> joiner.add(m)); return joiner.toString(); diff --git a/unirest-mocks/src/main/java/kong/unirest/Matchers.java b/unirest-mocks/src/main/java/kong/unirest/Matchers.java new file mode 100644 index 000000000..1ae5ed089 --- /dev/null +++ b/unirest-mocks/src/main/java/kong/unirest/Matchers.java @@ -0,0 +1,42 @@ +/** + * The MIT License + * + * Copyright for portions of unirest-java are held by Kong Inc (c) 2013. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package kong.unirest; + +/** + * Static set of Matchers + */ +public class Matchers { + + /** + * Creates a FieldMatcher expecting a map of keys and values + * use like: FieldMatcher.of("fruit", "orange", "quantity" "42") + * @param keyValuePairs + * @return a new FieldMatcher + */ + public BodyMatcher bodyFields(String keyValuePairs){ + return FieldMatcher.of(keyValuePairs); + } +} diff --git a/unirest-mocks/src/main/java/kong/unirest/MockClient.java b/unirest-mocks/src/main/java/kong/unirest/MockClient.java index 1d1fe3b75..a8d9f6ca5 100644 --- a/unirest-mocks/src/main/java/kong/unirest/MockClient.java +++ b/unirest-mocks/src/main/java/kong/unirest/MockClient.java @@ -145,6 +145,15 @@ public Expectation expect(HttpMethod method, String path) { return exp.newExpectation(); } + /** + * Expect ANY call to a path with this method + * @param method the Http Method + * @return this expectation builder + */ + public Expectation expect(HttpMethod method) { + return expect(method, null); + } + /** * Assert a specific method and path were invoked * @param method the Http method diff --git a/unirest-mocks/src/main/java/kong/unirest/Routes.java b/unirest-mocks/src/main/java/kong/unirest/Routes.java index 036141bcb..f1f0588e7 100644 --- a/unirest-mocks/src/main/java/kong/unirest/Routes.java +++ b/unirest-mocks/src/main/java/kong/unirest/Routes.java @@ -54,7 +54,7 @@ Expectation newExpectation() { boolean matches(HttpRequest request) { Path p = new Path(request.getUrl()); return this.method.equals(request.getHttpMethod()) - && this.path.equalsIgnoreCase(p.baseUrl()); + && this.path == null || this.path.equalsIgnoreCase(p.baseUrl()); } RawResponse exchange(HttpRequest request, Config config) { diff --git a/unirest-mocks/src/test/java/kong/tests/AssertTest.java b/unirest-mocks/src/test/java/kong/tests/AssertTest.java index 8d418a048..364dae0bc 100644 --- a/unirest-mocks/src/test/java/kong/tests/AssertTest.java +++ b/unirest-mocks/src/test/java/kong/tests/AssertTest.java @@ -99,7 +99,15 @@ void canSetHeaderExpectationOnExpects() { client.verifyAll(); } + @Test + public void expectAnyPath(){ + client.expect(HttpMethod.GET) + .thenReturn("woh"); + + Unirest.get(path).asEmpty(); + client.verifyAll(); + } @Test void canExpectQueryParams() { diff --git a/unirest-mocks/src/test/java/kong/tests/BodyMatchingTest.java b/unirest-mocks/src/test/java/kong/tests/BodyMatchingTest.java index 8b5227511..554d298b5 100644 --- a/unirest-mocks/src/test/java/kong/tests/BodyMatchingTest.java +++ b/unirest-mocks/src/test/java/kong/tests/BodyMatchingTest.java @@ -30,12 +30,15 @@ import kong.unirest.Unirest; import org.junit.jupiter.api.Test; +import static kong.unirest.FieldMatcher.of; +import static org.junit.jupiter.api.Assertions.assertEquals; + class BodyMatchingTest extends Base { @Test void canTestForForms() { client.expect(HttpMethod.POST, path) - .body(FieldMatcher.of("beetle", "ringo", "number", "42")) + .body(of("beetle", "ringo", "number", "42")) .thenReturn() .withStatus(200); @@ -50,7 +53,7 @@ void canTestForForms() { @Test void missingAField() { client.expect(HttpMethod.POST, path) - .body(FieldMatcher.of("beetle", "ringo", "number", "42")) + .body(of("beetle", "ringo", "number", "42")) .thenReturn() .withStatus(200); @@ -61,7 +64,25 @@ void missingAField() { assertException(() -> client.verifyAll(), "A expectation was never invoked! POST http://basic\n" + "Body:\n" + - "\tMissing Expected Fields. Expected: \n" + - "number=42"); + "\tnumber=42"); + } + + @Test + void forFieldsShouldBeEncoded() { + client.expect(HttpMethod.POST, path) + .body(of("beetles", "ringo george", + "url", "http://somewhere")) + .thenReturn("cool") + .withStatus(200); + + String result = Unirest.post(path) + .field("beetles", "ringo george") + .field("url", "http://somewhere") + .asString() + .getBody(); + + client.verifyAll(); + + assertEquals("cool", result); } } diff --git a/unirest/src/main/java/kong/unirest/Path.java b/unirest/src/main/java/kong/unirest/Path.java index cf8ff8fe7..bd9010cbd 100644 --- a/unirest/src/main/java/kong/unirest/Path.java +++ b/unirest/src/main/java/kong/unirest/Path.java @@ -117,7 +117,7 @@ public String rawPath() { } public String baseUrl() { - if(url.contains("?")){ + if(url != null && url.contains("?")){ return url.substring(0, url.indexOf("?")); } return url;