if (originalRequest.getPath().equals("/a/b")) {
final HttpRequestImpl copyOriginal = new HttpRequestImpl(originalRequest);
removeAllHeaderParams(copyOriginal);
final HttpRequestImpl copyOther = new HttpRequestImpl(otherRequest);
removeAllHeaderParams(copyOther);
final HttpResponse newResponse = new HttpResponseImpl(201, "application/json", null);
return new HttpRequestMatchingContextImpl(copyOriginal, copyOther, newResponse);
}
return context;
}
@Override
public void setNext(final HttpRequestMatchingFilter filter) {
throw new UnsupportedOperationException();
}
@Override
public HttpRequestMatchingFilter next() {
return null;
}
private void removeAllHeaderParams(final HttpRequestImpl request) {
final Set<HttpMessageHeader> httpMessageHeaders = request.getHttpMessageHeaders();
for (final HttpMessageHeader header : httpMessageHeaders) {
request.removeHttpMessageHeader(header.getName(), header.getValue());
}
}
};
responseProvider.addHttpRequestMatchingFilter(filter);
final HttpRequestImpl request1 = new HttpRequestImpl();
request1.method(Method.GET);
request1.path("/a/b");
request1.queryParameter("a", "1");
request1.httpMessageHeader("custom", "value");
final HttpResponse response1 = responseProvider.getResponse(request1);
assertNotNull("We expect matcher to be kicked in and provided response.", response1);
assertEquals("We expect custom response.", 201, response1.getHttpCode());
final HttpRequestImpl request2 = new HttpRequestImpl();
request2.path("/a/c");
request2.httpMessageHeader("custom", "value");
assertNull("for /a/c we need exact match.", responseProvider.getResponse(request2));
final HttpRequestImpl request3 = new HttpRequestImpl();
request3.method(Method.GET);
request3.path("/a/c");
request3.httpMessageHeader("Content-Type", "application/json");
request3.queryParameter("a", "1");
final HttpResponse response3 = responseProvider.getResponse(request3);
assertNotNull("We expected exact match to have worked.", response3);
assertEquals(200, response3.getHttpCode());
}