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.response;
31  
32  import com.jcabi.http.Response;
33  import com.jcabi.http.request.FakeRequest;
34  import jakarta.ws.rs.core.HttpHeaders;
35  import java.net.HttpURLConnection;
36  import java.net.URI;
37  import org.hamcrest.Matcher;
38  import org.hamcrest.MatcherAssert;
39  import org.hamcrest.Matchers;
40  import org.junit.jupiter.api.Assertions;
41  import org.junit.jupiter.api.Test;
42  import org.junit.jupiter.api.function.Executable;
43  
44  /**
45   * Test case for {@link RestResponse}.
46   * @since 1.1
47   */
48  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
49  final class RestResponseTest {
50  
51      /**
52       * RestResponse can assert HTTP status.
53       */
54      @Test
55      void assertsHttpStatusCode() {
56          Assertions.assertThrows(
57              AssertionError.class,
58              new Executable() {
59                  @Override
60                  public void execute() throws Throwable {
61                      new RestResponse(
62                          new FakeRequest()
63                              .withStatus(HttpURLConnection.HTTP_OK)
64                              .fetch()
65                      ).assertStatus(HttpURLConnection.HTTP_NOT_FOUND);
66                  }
67              }
68          );
69      }
70  
71      /**
72       * RestResponse can assert HTTP header.
73       * @throws Exception If something goes wrong inside
74       */
75      @Test
76      @SuppressWarnings("unchecked")
77      void assertsHttpHeaders() throws Exception {
78          final String name = "Abc";
79          final String value = "t66";
80          final Response rsp = new FakeRequest().withHeader(name, value).fetch();
81          new RestResponse(rsp).assertHeader(
82              name,
83              Matchers.allOf(
84                  Matchers.hasItems(value),
85                  Matcher.class.cast(Matchers.hasSize(1))
86              )
87          );
88          new RestResponse(rsp).assertHeader(
89              "Something-Else-Which-Is-Absent",
90              Matcher.class.cast(Matchers.empty())
91          );
92      }
93  
94      /**
95       * RestResponse can retrieve a cookie by name.
96       * @throws Exception If something goes wrong inside
97       */
98      @Test
99      void retrievesCookieByName() throws Exception {
100         final RestResponse response = new RestResponse(
101             new FakeRequest()
102                 .withBody("<hello/>")
103                 .withHeader(
104                     HttpHeaders.SET_COOKIE,
105                     "cookie1=foo1;Path=/;Comment=\"\", bar=1;"
106                 )
107                 .fetch()
108         );
109         MatcherAssert.assertThat(
110             response.cookie("cookie1"),
111             Matchers.allOf(
112                 Matchers.hasProperty("value", Matchers.equalTo("foo1")),
113                 Matchers.hasProperty("path", Matchers.equalTo("/"))
114             )
115         );
116     }
117 
118     /**
119      * RestResponse can retrieve a cookie by name if header occurs several
120      * times.
121      * @throws Exception If something goes wrong inside
122      */
123     @Test
124     void retrievesCookieByNameSeveralValues() throws Exception {
125         final RestResponse response = new RestResponse(
126             new FakeRequest()
127                 .withHeader(HttpHeaders.SET_COOKIE, "foo=bar; path=/i;")
128                 .withHeader(HttpHeaders.SET_COOKIE, "baz=goo; path=/l;")
129                 .fetch()
130         );
131         MatcherAssert.assertThat(
132             response.cookie("baz"),
133             Matchers.allOf(
134                 Matchers.hasProperty("value", Matchers.equalTo("goo")),
135                 Matchers.hasProperty("path", Matchers.equalTo("/l"))
136             )
137         );
138         MatcherAssert.assertThat(
139             response.cookie("foo"),
140             Matchers.allOf(
141                 Matchers.hasProperty("value", Matchers.equalTo("bar")),
142                 Matchers.hasProperty("path", Matchers.equalTo("/i"))
143             )
144         );
145     }
146 
147     /**
148      * RestResponse can jump to a relative URL.
149      * @throws Exception If something goes wrong inside
150      */
151     @Test
152     void jumpsToRelativeUrls() throws Exception {
153         MatcherAssert.assertThat(
154             new RestResponse(
155                 new FakeRequest()
156                     .uri().set(new URI("http://locahost:888/tt")).back()
157                     .fetch()
158             ).jump(new URI("/foo/bar?hey")).uri().get(),
159             Matchers.hasToString("http://locahost:888/foo/bar?hey")
160         );
161     }
162 
163 }