Skip to content

Commit

Permalink
Handling path matching request using last mock in priority (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrepa authored Jan 29, 2024
1 parent 708b9a5 commit b6cdeba
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,24 @@
import org.mockserver.closurecallback.websocketregistry.LocalCallbackRegistry;
import org.mockserver.collections.CircularPriorityQueue;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.matchers.*;
import org.mockserver.matchers.HttpRequestMatcher;
import org.mockserver.matchers.HttpRequestPropertiesMatcher;
import org.mockserver.matchers.JsonStringMatcher;
import org.mockserver.matchers.MatchType;
import org.mockserver.matchers.TimeToLive;
import org.mockserver.matchers.Times;
import org.mockserver.mock.Expectation;
import org.mockserver.mock.HttpState;
import org.mockserver.mock.SortableExpectationId;
import org.mockserver.mock.action.ExpectationResponseCallback;
import org.mockserver.mock.listeners.MockServerMatcherNotifier;
import org.mockserver.model.*;
import org.mockserver.model.Body;
import org.mockserver.model.Header;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.LogEventRequestAndResponse;
import org.mockserver.model.NottableSchemaString;
import org.mockserver.model.Parameter;
import org.mockserver.netty.MockServerUnificationInitializer;
import org.mockserver.verify.VerificationTimes;

Expand All @@ -40,7 +51,10 @@
import static com.decathlon.tzatziki.utils.Fields.getValue;
import static com.decathlon.tzatziki.utils.Unchecked.unchecked;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -178,16 +192,21 @@ public static Integer localPort() {
private static final Set<Pattern> PATH_PATTERNS = new LinkedHashSet<>();

public static List<LogEventRequestAndResponse> retrieveRequestResponses(HttpRequest httpRequest) {
PATH_PATTERNS.stream()
.map(pathPattern -> pathPattern.matcher(httpRequest.getPath().getValue()))
.filter(Matcher::matches)
.filter(matcher -> matcher.groupCount() > 0)
.findFirst()
.ifPresent(matcher -> {
List<Pattern> patternArrayList = new ArrayList<>(PATH_PATTERNS);
Collections.reverse(patternArrayList);
for (Pattern pattern : patternArrayList) {
Matcher matcher = pattern.matcher(httpRequest.getPath().getValue());
if (matcher.matches()) {
if (matcher.groupCount() > 0) {
for (int i = 1; i <= matcher.groupCount(); i++) {
httpRequest.withPathParameter("param" + i, matcher.group(i));

}
});
}
break;
}
}

List<LogEventRequestAndResponse> requestResponses = new ArrayList<>();
CompletableFuture<Void> waiter = new CompletableFuture<>();
unchecked(HTTP_STATE::get).getMockServerLog().retrieveRequestResponses(httpRequest, logEventRequestAndResponses -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1461,4 +1461,14 @@ Feature: to interact with an http service and setup mocks

Scenario: Http status codes are extended and not limited to MockServer ones
Given that getting on "http://backend/tooManyRequest" will return a status TOO_MANY_REQUESTS_429
Then getting on "http://backend/tooManyRequest" returns a status TOO_MANY_REQUESTS_429
Then getting on "http://backend/tooManyRequest" returns a status TOO_MANY_REQUESTS_429


Scenario: Conflicting pattern are properly handled and last mock is prioritized
Given that getting on "http://backend/test/S(\d)/path/C(\d)" will return a status TOO_MANY_REQUESTS_429

And that getting on "http://backend/test/S1/path/C2" will return a status OK_200

Then getting on "http://backend/test/S1/path/C2" returns a status OK_200

And "http://backend/test/S1/path/C2" has received a GET

0 comments on commit b6cdeba

Please sign in to comment.