Skip to content

Commit

Permalink
encode form params and support anymatch on URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Dec 23, 2020
1 parent 1326505 commit 83046b5
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
5 changes: 3 additions & 2 deletions unirest-mocks/src/main/java/kong/unirest/FieldMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package kong.unirest;

import java.net.URLEncoder;
import java.util.*;

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ public MatchStatus matches(List<String> body) throws AssertionError {
List<String> missing = new ArrayList<>();
boolean pass = true;
for (Map.Entry<String, String> 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;
Expand All @@ -70,7 +71,7 @@ private String description(boolean pass, List<String> 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();
Expand Down
42 changes: 42 additions & 0 deletions unirest-mocks/src/main/java/kong/unirest/Matchers.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
9 changes: 9 additions & 0 deletions unirest-mocks/src/main/java/kong/unirest/MockClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion unirest-mocks/src/main/java/kong/unirest/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions unirest-mocks/src/test/java/kong/tests/AssertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
29 changes: 25 additions & 4 deletions unirest-mocks/src/test/java/kong/tests/BodyMatchingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion unirest/src/main/java/kong/unirest/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 83046b5

Please sign in to comment.