View Javadoc
1   /*
2    * Copyright (c) 2011-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.http.wire;
31  
32  import com.jcabi.http.Request;
33  import com.jcabi.http.mock.MkAnswer;
34  import com.jcabi.http.mock.MkContainer;
35  import com.jcabi.http.mock.MkGrizzlyContainer;
36  import com.jcabi.http.mock.MkQuery;
37  import com.jcabi.http.request.JdkRequest;
38  import com.jcabi.http.response.RestResponse;
39  import jakarta.ws.rs.core.HttpHeaders;
40  import java.net.HttpURLConnection;
41  import org.hamcrest.MatcherAssert;
42  import org.hamcrest.Matchers;
43  import org.junit.jupiter.api.Test;
44  
45  /**
46   * Test case for {@link VerboseWire}.
47   * @since 1.0
48   */
49  final class VerboseWireTest {
50  
51      /**
52       * VerboseWire can log requests.
53       * @throws Exception If something goes wrong inside
54       */
55      @Test
56      void logsRequest() throws Exception {
57          final MkContainer container = new MkGrizzlyContainer().next(
58              new MkAnswer.Simple("")
59          ).start();
60          new JdkRequest(container.home())
61              .through(VerboseWire.class)
62              .header(HttpHeaders.USER_AGENT, "it's me")
63              .fetch()
64              .as(RestResponse.class)
65              .assertStatus(HttpURLConnection.HTTP_OK);
66          container.stop();
67      }
68  
69      /**
70       * VerboseWire can log request body.
71       * @throws Exception If something goes wrong inside
72       */
73      @Test
74      void logsRequestBody() throws Exception {
75          final MkContainer container = new MkGrizzlyContainer().next(
76              new MkAnswer.Simple("")
77          ).start();
78          try {
79              new JdkRequest(container.home())
80                  .through(VerboseWire.class)
81                  .method(Request.POST)
82                  .body().set("hello, world!").back()
83                  .fetch()
84                  .as(RestResponse.class)
85                  .assertStatus(HttpURLConnection.HTTP_OK);
86              final MkQuery query = container.take();
87              MatcherAssert.assertThat(
88                  query.body(),
89                  Matchers.startsWith("hello,")
90              );
91          } finally {
92              container.stop();
93          }
94      }
95  
96  }