Custom Request Matcher NPE when unmatched
See original GitHub issuewiremock-standalone v2.2.1
When using a Custom RequestMatcher
and an unmatched Request
is encountered I am seeing the below Exception.
java.lang.NullPointerException
at com.github.tomakehurst.wiremock.verification.Diff.toString(Diff.java:60)
at com.github.tomakehurst.wiremock.client.VerificationException.<init>(VerificationException.java:64)
at com.github.tomakehurst.wiremock.client.VerificationException.forSingleUnmatchedRequest(VerificationException.java:43)
at com.github.tomakehurst.wiremock.client.VerificationException.forUnmatchedRequests(VerificationException.java:48)
at com.github.tomakehurst.wiremock.junit.WireMockRule.checkForUnmatchedRequests(WireMockRule.java:87)
at com.github.tomakehurst.wiremock.junit.WireMockRule.access$000(WireMockRule.java:33)
at com.github.tomakehurst.wiremock.junit.WireMockRule$1.evaluate(WireMockRule.java:73)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
Looks like the documentation may be a bit out of date regarding custom matchers, so this could be an issue with the way I have set them up.
I’ve created a sample test that demonstrates the issue.
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.MatchResult;
import com.github.tomakehurst.wiremock.matching.RequestMatcher;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Rule;
import org.junit.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.requestMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
public class WmBugTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
@Test
public void wmBug() throws Exception {
wireMockRule.stubFor(requestMatching(new CustomRequestMatcher())
.willReturn(aResponse()
.withHeader("Content-Type", "text/plain")
.withBody("Hi")));
unmatchedGet();
}
private void unmatchedGet() throws Exception {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("http://localhost:" + wireMockRule.port() + "/unmatched");
request.addHeader("User-Agent", "foobar");
HttpResponse response = client.execute(request);
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
}
public static class CustomRequestMatcher extends RequestMatcher {
@Override
public String getExpected() {
return "Some custom expectation";
}
@Override
public MatchResult match(Request request) {
if (request.getUrl().equals("foobar")) {
return MatchResult.exactMatch();
} else {
return MatchResult.noMatch();
}
}
@Override
public String getName() {
return "custom-request-matcher";
}
}
}
If this is not user-error, can anyone suggest a workaround just to have the WireMockRule
not fail during (with this internal Exception) and instead fail with the unmatched call Exception?
I spent 45mins or so debugging through StubMapping, RequestPattern, NearMiss and Diff and didn’t really come to any meaningful conclusion about what is at fault (eg should the Diff class be null safe or is the contract that urlMatcher should always be non-null). Happy to work on a pull request if someone can push me in the right direction.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5
Actually, don’t worry. I’m about to push a fix.
Fix looks good on the demo test, thanks for that.