Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions unirest-modules-mocks/src/main/java/kong/unirest/core/Routes.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,38 @@ private Optional<Invocation> getBestMatch(HttpRequest request) {
}

private Optional<Invocation> getBestMatch(HttpRequest request, boolean expected) {
Map<Integer, Invocation> map = new TreeMap<>();
invokes.stream()
.forEach(i -> {
Integer score = i.scoreMatch(request);
if(score >= 0) {
map.put(score, i);
}
});
if (map.size() == 0) {
Invocation best = null;
int bestScore = -1;

for (Invocation invocation : invokes) {
if (!invocation.isExpected().equals(expected)) {
continue;
}

int score = invocation.scoreMatch(request);

if (score < 0) {
continue;
}

if (score > bestScore) {
best = invocation;
bestScore = score;
} else if (score == bestScore
&& expected
&& best != null
&& best.requestSize() > 0
&& invocation.requestSize() == 0) {
best = invocation;
}
}

if (best == null) {
return Optional.empty();
}
Invocation value = map.get(Collections.max(map.keySet()));
value.log(request);
return Optional.of(value);

best.log(request);
return Optional.of(best);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void willPickTheBestMatchingQueryExpectation() {
}

@Test
void whenDuplicateExpectsExistUseTheLastOne() {
void whenDuplicateExpectsExistUseThemInOrder() {
client.expect(GET, path)
.queryString("monster", "grover")
.thenReturn("one");
Expand All @@ -103,17 +103,19 @@ void whenDuplicateExpectsExistUseTheLastOne() {
.queryString("monster", "grover")
.thenReturn("two");

String result = Unirest.get(path)
String first = Unirest.get(path)
.queryString("monster", "grover")
.asString()
.getBody();

assertEquals("two", result);
String second = Unirest.get(path)
.queryString("monster", "grover")
.asString()
.getBody();

assertException(() -> client.verifyAll(),
"Expected at least 1 invocations but got 0\n" +
"GET http://basic\n" +
"Params:\n" +
"monster: grover");
assertEquals("one", first);
assertEquals("two", second);

client.verifyAll();
}
}
Loading