Skip to content

Commit

Permalink
Make UserAgentWire configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoss committed Jun 25, 2021
1 parent 357ce43 commit 771cf34
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
27 changes: 16 additions & 11 deletions src/main/java/com/jcabi/http/wire/UserAgentWire.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Map;
import javax.ws.rs.core.HttpHeaders;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

/**
Expand Down Expand Up @@ -69,29 +70,33 @@
@Immutable
@ToString(of = "origin")
@EqualsAndHashCode(of = "origin")
@RequiredArgsConstructor
public final class UserAgentWire implements Wire {

/**
* Default user agent.
* Original wire.
*/
private static final String AGENT = String.format(
"jcabi-%s/%s Java/%s",
Manifests.read("JCabi-Version"),
Manifests.read("JCabi-Build"),
System.getProperty("java.version")
);
private final Wire origin;

/**
* Original wire.
* Agent.
*/
private final transient Wire origin;
private final String agent;

/**
* Public ctor.
* @param wire Original wire
*/
public UserAgentWire(final Wire wire) {
this.origin = wire;
this(
wire,
String.format(
"jcabi-%s/%s Java/%s",
Manifests.read("JCabi-Version"),
Manifests.read("JCabi-Build"),
System.getProperty("java.version")
)
);
}

// @checkstyle ParameterNumber (7 lines)
Expand All @@ -115,7 +120,7 @@ public Response send(final Request req, final String home,
hdrs.add(
new ImmutableHeader(
HttpHeaders.USER_AGENT,
UserAgentWire.AGENT
this.agent
)
);
}
Expand Down
38 changes: 31 additions & 7 deletions src/test/java/com/jcabi/http/wire/UserAgentWireTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@
* Test case for {@link UserAgentWire}.
* @since 1.2
*/
public final class UserAgentWireTest {
final class UserAgentWireTest {

/**
* UserAgentWire can add User-Agent HTTP header.
* @throws Exception If something goes wrong inside
*/
@Test
void addsUserAgentHeader() throws Exception {
void addsDefaultUserAgentHeader() throws Exception {
final MkContainer container = new MkGrizzlyContainer().next(
new MkAnswer.Simple("")
).start();
Expand All @@ -62,8 +58,36 @@ void addsUserAgentHeader() throws Exception {
.assertStatus(HttpURLConnection.HTTP_OK);
container.stop();
MatcherAssert.assertThat(
"must add default User-Agent HTTP header",
container.take().headers(),
Matchers.hasKey(HttpHeaders.USER_AGENT)
Matchers.hasEntry(
Matchers.is(HttpHeaders.USER_AGENT),
Matchers.contains(
Matchers.startsWith("jcabi-")
)
)
);
}

@Test
void addsCustomUserAgentHeader() throws Exception {
final MkContainer container = new MkGrizzlyContainer().next(
new MkAnswer.Simple("")
).start();
final String agent = "Mozilla/5.0";
new JdkRequest(container.home())
.through(UserAgentWire.class, agent)
.fetch()
.as(RestResponse.class)
.assertStatus(HttpURLConnection.HTTP_OK);
container.stop();
MatcherAssert.assertThat(
"must add custom User-Agent HTTP header",
container.take().headers(),
Matchers.hasEntry(
Matchers.is(HttpHeaders.USER_AGENT),
Matchers.contains(agent)
)
);
}

Expand Down

0 comments on commit 771cf34

Please sign in to comment.