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.Request;
8   import com.jcabi.http.mock.MkAnswer;
9   import com.jcabi.http.mock.MkContainer;
10  import com.jcabi.http.mock.MkGrizzlyContainer;
11  import com.jcabi.http.mock.MkQuery;
12  import com.jcabi.http.request.JdkRequest;
13  import com.jcabi.http.response.RestResponse;
14  import jakarta.ws.rs.core.HttpHeaders;
15  import java.net.HttpURLConnection;
16  import org.hamcrest.MatcherAssert;
17  import org.hamcrest.Matchers;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * Test case for {@link VerboseWire}.
22   * @since 1.0
23   */
24  final class VerboseWireTest {
25  
26      /**
27       * VerboseWire can log requests.
28       * @throws Exception If something goes wrong inside
29       */
30      @Test
31      void logsRequest() throws Exception {
32          final MkContainer container = new MkGrizzlyContainer().next(
33              new MkAnswer.Simple("")
34          ).start();
35          new JdkRequest(container.home())
36              .through(VerboseWire.class)
37              .header(HttpHeaders.USER_AGENT, "it's me")
38              .fetch()
39              .as(RestResponse.class)
40              .assertStatus(HttpURLConnection.HTTP_OK);
41          container.stop();
42      }
43  
44      /**
45       * VerboseWire can log request body.
46       * @throws Exception If something goes wrong inside
47       */
48      @Test
49      void logsRequestBody() throws Exception {
50          final MkContainer container = new MkGrizzlyContainer().next(
51              new MkAnswer.Simple("")
52          ).start();
53          try {
54              new JdkRequest(container.home())
55                  .through(VerboseWire.class)
56                  .method(Request.POST)
57                  .body().set("hello, world!").back()
58                  .fetch()
59                  .as(RestResponse.class)
60                  .assertStatus(HttpURLConnection.HTTP_OK);
61              final MkQuery query = container.take();
62              MatcherAssert.assertThat(
63                  "should starts with 'hello,'",
64                  query.body(),
65                  Matchers.startsWith("hello,")
66              );
67          } finally {
68              container.stop();
69          }
70      }
71  
72  }