Skip to content

Commit 771cf34

Browse files
committed
Make UserAgentWire configurable
1 parent 357ce43 commit 771cf34

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

src/main/java/com/jcabi/http/wire/UserAgentWire.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.Map;
4343
import javax.ws.rs.core.HttpHeaders;
4444
import lombok.EqualsAndHashCode;
45+
import lombok.RequiredArgsConstructor;
4546
import lombok.ToString;
4647

4748
/**
@@ -69,29 +70,33 @@
6970
@Immutable
7071
@ToString(of = "origin")
7172
@EqualsAndHashCode(of = "origin")
73+
@RequiredArgsConstructor
7274
public final class UserAgentWire implements Wire {
7375

7476
/**
75-
* Default user agent.
77+
* Original wire.
7678
*/
77-
private static final String AGENT = String.format(
78-
"jcabi-%s/%s Java/%s",
79-
Manifests.read("JCabi-Version"),
80-
Manifests.read("JCabi-Build"),
81-
System.getProperty("java.version")
82-
);
79+
private final Wire origin;
8380

8481
/**
85-
* Original wire.
82+
* Agent.
8683
*/
87-
private final transient Wire origin;
84+
private final String agent;
8885

8986
/**
9087
* Public ctor.
9188
* @param wire Original wire
9289
*/
9390
public UserAgentWire(final Wire wire) {
94-
this.origin = wire;
91+
this(
92+
wire,
93+
String.format(
94+
"jcabi-%s/%s Java/%s",
95+
Manifests.read("JCabi-Version"),
96+
Manifests.read("JCabi-Build"),
97+
System.getProperty("java.version")
98+
)
99+
);
95100
}
96101

97102
// @checkstyle ParameterNumber (7 lines)
@@ -115,7 +120,7 @@ public Response send(final Request req, final String home,
115120
hdrs.add(
116121
new ImmutableHeader(
117122
HttpHeaders.USER_AGENT,
118-
UserAgentWire.AGENT
123+
this.agent
119124
)
120125
);
121126
}

src/test/java/com/jcabi/http/wire/UserAgentWireTest.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,10 @@
4444
* Test case for {@link UserAgentWire}.
4545
* @since 1.2
4646
*/
47-
public final class UserAgentWireTest {
47+
final class UserAgentWireTest {
4848

49-
/**
50-
* UserAgentWire can add User-Agent HTTP header.
51-
* @throws Exception If something goes wrong inside
52-
*/
5349
@Test
54-
void addsUserAgentHeader() throws Exception {
50+
void addsDefaultUserAgentHeader() throws Exception {
5551
final MkContainer container = new MkGrizzlyContainer().next(
5652
new MkAnswer.Simple("")
5753
).start();
@@ -62,8 +58,36 @@ void addsUserAgentHeader() throws Exception {
6258
.assertStatus(HttpURLConnection.HTTP_OK);
6359
container.stop();
6460
MatcherAssert.assertThat(
61+
"must add default User-Agent HTTP header",
6562
container.take().headers(),
66-
Matchers.hasKey(HttpHeaders.USER_AGENT)
63+
Matchers.hasEntry(
64+
Matchers.is(HttpHeaders.USER_AGENT),
65+
Matchers.contains(
66+
Matchers.startsWith("jcabi-")
67+
)
68+
)
69+
);
70+
}
71+
72+
@Test
73+
void addsCustomUserAgentHeader() throws Exception {
74+
final MkContainer container = new MkGrizzlyContainer().next(
75+
new MkAnswer.Simple("")
76+
).start();
77+
final String agent = "Mozilla/5.0";
78+
new JdkRequest(container.home())
79+
.through(UserAgentWire.class, agent)
80+
.fetch()
81+
.as(RestResponse.class)
82+
.assertStatus(HttpURLConnection.HTTP_OK);
83+
container.stop();
84+
MatcherAssert.assertThat(
85+
"must add custom User-Agent HTTP header",
86+
container.take().headers(),
87+
Matchers.hasEntry(
88+
Matchers.is(HttpHeaders.USER_AGENT),
89+
Matchers.contains(agent)
90+
)
6791
);
6892
}
6993

0 commit comments

Comments
 (0)