PawelAdamski / HttpClientMock

Library for mocking Apache HttpClient.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NPE when the scheme is not set for version 1.10.0

emaccaull opened this issue · comments

I am seeing an NPE with the following test with version 1.10.0. This issue is not reproducible with version 2.2.1. However, I am currently stuck on 1.10.0 for my application.

    @Test
    public void npeWhenSchemeIsNotSet() throws Exception {
        final URI uri = new URIBuilder().setHost("foo").setPath("/example").build();
        final HttpPost post = new HttpPost(uri);

        try (HttpClientMock client = new HttpClientMock()) {
            client.onPost("http://foo/example").doReturn("1");
            client.execute(post);
        }
    }

uri does not specify a scheme.

The above code outputs:

java.lang.NullPointerException
	at com.github.paweladamski.httpclientmock.Request.getUri(Request.java:39)
	at com.github.paweladamski.httpclientmock.Rule.matches(Rule.java:32)
	at com.github.paweladamski.httpclientmock.Rule.matches(Rule.java:38)
	at com.github.paweladamski.httpclientmock.HttpClientMock.lambda$getHttpResponse$0(HttpClientMock.java:267)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:176)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.reduce(ReferencePipeline.java:558)
	at com.github.paweladamski.httpclientmock.HttpClientMock.getHttpResponse(HttpClientMock.java:268)
	at com.github.paweladamski.httpclientmock.HttpClientMock.doExecute(HttpClientMock.java:248)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
	at io.github.emaccaull.httpclient.ClientMockTest.npeWhenSchemeIsNotSet(ClientMockTest.java:19)

When a more helpful error message would be nice and save debugging time.

Thank you for this project. It's been very helpful.

Hi @emaccaull
I will have a look on it.