View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.http.wire;
6   
7   import com.jcabi.http.mock.MkAnswer;
8   import com.jcabi.http.mock.MkContainer;
9   import com.jcabi.http.mock.MkGrizzlyContainer;
10  import com.jcabi.http.request.JdkRequest;
11  import com.jcabi.http.response.RestResponse;
12  import jakarta.ws.rs.core.HttpHeaders;
13  import java.net.HttpURLConnection;
14  import org.hamcrest.MatcherAssert;
15  import org.hamcrest.Matchers;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * Test case for {@link UserAgentWire}.
20   * @since 1.2
21   */
22  final class UserAgentWireTest {
23  
24      @Test
25      void addsDefaultUserAgentHeader() throws Exception {
26          final MkContainer container = new MkGrizzlyContainer().next(
27              new MkAnswer.Simple("")
28          ).start();
29          new JdkRequest(container.home())
30              .through(UserAgentWire.class)
31              .fetch()
32              .as(RestResponse.class)
33              .assertStatus(HttpURLConnection.HTTP_OK);
34          container.stop();
35          MatcherAssert.assertThat(
36              "must add default User-Agent HTTP header",
37              container.take().headers(),
38              Matchers.hasEntry(
39                  Matchers.is(HttpHeaders.USER_AGENT),
40                  Matchers.contains(
41                      Matchers.startsWith("jcabi-")
42                  )
43              )
44          );
45      }
46  
47      @Test
48      void addsCustomUserAgentHeader() throws Exception {
49          final MkContainer container = new MkGrizzlyContainer().next(
50              new MkAnswer.Simple("")
51          ).start();
52          final String agent = "Mozilla/5.0";
53          new JdkRequest(container.home())
54              .through(UserAgentWire.class, agent)
55              .fetch()
56              .as(RestResponse.class)
57              .assertStatus(HttpURLConnection.HTTP_OK);
58          container.stop();
59          MatcherAssert.assertThat(
60              "must add custom User-Agent HTTP header",
61              container.take().headers(),
62              Matchers.hasEntry(
63                  Matchers.is(HttpHeaders.USER_AGENT),
64                  Matchers.contains(agent)
65              )
66          );
67      }
68  
69  }