Skip to content

Commit

Permalink
jcabi#182 - Added "Request through(Wire wire)" method
Browse files Browse the repository at this point in the history
For jcabi#182
- added `Request through(Wire wire)` method
  • Loading branch information
pinaf committed Jul 29, 2019
1 parent 47c8d2c commit 9248fce
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/jcabi/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
* @see com.jcabi.http.request.ApacheRequest
*/
@Immutable
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
public interface Request {

/**
Expand Down Expand Up @@ -195,4 +195,11 @@ public interface Request {
*/
<T extends Wire> Request through(Class<T> 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);
}
4 changes: 4 additions & 0 deletions src/main/java/com/jcabi/http/request/ApacheRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,8 @@ public <T extends Wire> Request through(final Class<T> type,
return this.base.through(type, args);
}

@Override
public Request through(final Wire wire) {
return this.base.through(wire);
}
}
7 changes: 6 additions & 1 deletion src/main/java/com/jcabi/http/request/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,13 @@ public <T extends Wire> Request through(final Class<T> 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,
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/jcabi/http/request/FakeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ public <T extends Wire> Request through(final Class<T> 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
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/jcabi/http/request/JdkRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,9 @@ public <T extends Wire> Request through(final Class<T> type,
return this.base.through(type, args);
}

@Override
public Request through(final Wire wire) {
return this.base.through(wire);
}

}
52 changes: 52 additions & 0 deletions src/test/java/com/jcabi/http/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Collection<Map.Entry<String, String>>> hdrs =
new Supplier<Collection<Map.Entry<String, String>>>() {
@Override
public Collection<Map.Entry<String, String>> 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
* <code>Request.timeout</code> is called.
Expand Down

0 comments on commit 9248fce

Please sign in to comment.