diff --git a/src/main/java/com/jcabi/http/Request.java b/src/main/java/com/jcabi/http/Request.java index 6cb16339..090be822 100644 --- a/src/main/java/com/jcabi/http/Request.java +++ b/src/main/java/com/jcabi/http/Request.java @@ -78,7 +78,7 @@ * @see com.jcabi.http.request.ApacheRequest */ @Immutable -@SuppressWarnings("PMD.AvoidDuplicateLiterals") +@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"}) public interface Request { /** @@ -195,4 +195,11 @@ public interface Request { */ Request through(Class type, Object... args); + /** + * Send it through a decorating {@link Wire}. + * @param wire Wire to use + * @return New request with a wire decorated + * @since 0.10 + */ + Request through(Wire wire); } diff --git a/src/main/java/com/jcabi/http/request/ApacheRequest.java b/src/main/java/com/jcabi/http/request/ApacheRequest.java index 936dcda4..c417c4c3 100644 --- a/src/main/java/com/jcabi/http/request/ApacheRequest.java +++ b/src/main/java/com/jcabi/http/request/ApacheRequest.java @@ -266,4 +266,8 @@ public Request through(final Class type, return this.base.through(type, args); } + @Override + public Request through(final Wire wire) { + return this.base.through(wire); + } } diff --git a/src/main/java/com/jcabi/http/request/BaseRequest.java b/src/main/java/com/jcabi/http/request/BaseRequest.java index f849cc50..b711fa25 100644 --- a/src/main/java/com/jcabi/http/request/BaseRequest.java +++ b/src/main/java/com/jcabi/http/request/BaseRequest.java @@ -308,8 +308,13 @@ public Request through(final Class type, | IllegalAccessException | InvocationTargetException ex) { throw new IllegalStateException(ex); } + return this.through(decorated); + } + + @Override + public Request through(final Wire wre) { return new BaseRequest( - decorated, + wre, this.home, this.hdrs, this.mtd, diff --git a/src/main/java/com/jcabi/http/request/FakeRequest.java b/src/main/java/com/jcabi/http/request/FakeRequest.java index a1c230f9..ba9e8418 100644 --- a/src/main/java/com/jcabi/http/request/FakeRequest.java +++ b/src/main/java/com/jcabi/http/request/FakeRequest.java @@ -212,6 +212,11 @@ public Request through(final Class type, return this.base.through(type, args); } + @Override + public Request through(final Wire wire) { + return this.base.through(wire); + } + /** * Make a similar request, with the provided status code. * @param status The code diff --git a/src/main/java/com/jcabi/http/request/JdkRequest.java b/src/main/java/com/jcabi/http/request/JdkRequest.java index 4f121de7..8d503bbc 100644 --- a/src/main/java/com/jcabi/http/request/JdkRequest.java +++ b/src/main/java/com/jcabi/http/request/JdkRequest.java @@ -284,4 +284,9 @@ public Request through(final Class type, return this.base.through(type, args); } + @Override + public Request through(final Wire wire) { + return this.base.through(wire); + } + } diff --git a/src/test/java/com/jcabi/http/RequestTest.java b/src/test/java/com/jcabi/http/RequestTest.java index 7128904d..f6d96cc1 100644 --- a/src/test/java/com/jcabi/http/RequestTest.java +++ b/src/test/java/com/jcabi/http/RequestTest.java @@ -29,17 +29,20 @@ */ package com.jcabi.http; +import com.google.common.base.Supplier; import com.jcabi.http.mock.MkAnswer; import com.jcabi.http.mock.MkContainer; import com.jcabi.http.mock.MkGrizzlyContainer; import com.jcabi.http.mock.MkQuery; import com.jcabi.http.request.ApacheRequest; +import com.jcabi.http.request.BaseRequest; import com.jcabi.http.request.JdkRequest; import com.jcabi.http.response.RestResponse; import com.jcabi.http.response.XmlResponse; import com.jcabi.http.wire.BasicAuthWire; import com.jcabi.http.wire.UserAgentWire; import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; @@ -718,6 +721,55 @@ public void run() { this.testTimeoutOrderDoesntMatter(requestExecution); } + /** + * The wire passed to method "through" is used. + * @throws IOException On error + */ + @Test + public void passesThroughWire() throws IOException { + final Wire original = Mockito.mock(Wire.class); + final Wire wire = Mockito.mock(Wire.class); + final Response response = Mockito.mock(Response.class); + final Supplier>> hdrs = + new Supplier>>() { + @Override + public Collection> get() { + return org.mockito.Matchers.anyCollectionOf(null); + } + }; + final String url = "fake-url"; + Mockito.when( + wire.send( + org.mockito.Matchers.any(Request.class), + org.mockito.Matchers.eq(url), + org.mockito.Matchers.anyString(), + hdrs.get(), + org.mockito.Matchers.any(InputStream.class), + org.mockito.Matchers.anyInt(), + org.mockito.Matchers.anyInt() + ) + ).thenReturn(response); + new BaseRequest(original, url).through(wire).fetch(); + Mockito.verify(original, Mockito.never()).send( + org.mockito.Matchers.any(Request.class), + org.mockito.Matchers.anyString(), + org.mockito.Matchers.anyString(), + hdrs.get(), + org.mockito.Matchers.any(InputStream.class), + org.mockito.Matchers.anyInt(), + org.mockito.Matchers.anyInt() + ); + Mockito.verify(wire).send( + org.mockito.Matchers.any(Request.class), + org.mockito.Matchers.anyString(), + org.mockito.Matchers.anyString(), + hdrs.get(), + org.mockito.Matchers.any(InputStream.class), + org.mockito.Matchers.anyInt(), + org.mockito.Matchers.anyInt() + ); + } + /** * The connect and read timeouts are properly set no matter in which order * Request.timeout is called.