View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.http.response;
6   
7   import com.jcabi.http.Response;
8   import com.jcabi.http.request.FakeRequest;
9   import jakarta.ws.rs.core.HttpHeaders;
10  import java.net.HttpURLConnection;
11  import java.net.URI;
12  import org.hamcrest.Matcher;
13  import org.hamcrest.MatcherAssert;
14  import org.hamcrest.Matchers;
15  import org.junit.jupiter.api.Assertions;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * Test case for {@link RestResponse}.
20   * @since 1.1
21   */
22  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
23  final class RestResponseTest {
24  
25      /**
26       * RestResponse can assert HTTP status.
27       */
28      @Test
29      void assertsHttpStatusCode() {
30          Assertions.assertThrows(
31              AssertionError.class,
32              () -> new RestResponse(
33                  new FakeRequest()
34                      .withStatus(HttpURLConnection.HTTP_OK)
35                      .fetch()
36              ).assertStatus(HttpURLConnection.HTTP_NOT_FOUND)
37          );
38      }
39  
40      /**
41       * RestResponse can assert HTTP header.
42       * @throws Exception If something goes wrong inside
43       */
44      @Test
45      @SuppressWarnings("unchecked")
46      void assertsHttpHeaders() throws Exception {
47          final String name = "Abc";
48          final String value = "t66";
49          final Response rsp = new FakeRequest().withHeader(name, value).fetch();
50          new RestResponse(rsp).assertHeader(
51              name,
52              Matchers.allOf(
53                  Matchers.hasItems(value),
54                  Matcher.class.cast(Matchers.hasSize(1))
55              )
56          );
57          new RestResponse(rsp).assertHeader(
58              "Something-Else-Which-Is-Absent",
59              Matcher.class.cast(Matchers.empty())
60          );
61      }
62  
63      /**
64       * RestResponse can retrieve a cookie by name.
65       * @throws Exception If something goes wrong inside
66       */
67      @Test
68      void retrievesCookieByName() throws Exception {
69          final RestResponse response = new RestResponse(
70              new FakeRequest()
71                  .withBody("<hello/>")
72                  .withHeader(
73                      HttpHeaders.SET_COOKIE,
74                      "cookie1=foo1;Path=/;Comment=\"\", bar=1;"
75                  )
76                  .fetch()
77          );
78          MatcherAssert.assertThat(
79              "should contains value & path",
80              response.cookie("cookie1"),
81              Matchers.allOf(
82                  Matchers.hasProperty("value", Matchers.equalTo("foo1")),
83                  Matchers.hasProperty("path", Matchers.equalTo("/"))
84              )
85          );
86      }
87  
88      /**
89       * RestResponse can retrieve a cookie by name if header occurs several
90       * times.
91       * @throws Exception If something goes wrong inside
92       */
93      @Test
94      void retrievesCookieByNameSeveralValues() throws Exception {
95          final RestResponse response = new RestResponse(
96              new FakeRequest()
97                  .withHeader(HttpHeaders.SET_COOKIE, "foo=bar; path=/i;")
98                  .withHeader(HttpHeaders.SET_COOKIE, "baz=goo; path=/l;")
99                  .fetch()
100         );
101         MatcherAssert.assertThat(
102             "should contains value & path",
103             response.cookie("baz"),
104             Matchers.allOf(
105                 Matchers.hasProperty("value", Matchers.equalTo("goo")),
106                 Matchers.hasProperty("path", Matchers.equalTo("/l"))
107             )
108         );
109         MatcherAssert.assertThat(
110             "should contains value & path",
111             response.cookie("foo"),
112             Matchers.allOf(
113                 Matchers.hasProperty("value", Matchers.equalTo("bar")),
114                 Matchers.hasProperty("path", Matchers.equalTo("/i"))
115             )
116         );
117     }
118 
119     /**
120      * RestResponse can jump to a relative URL.
121      * @throws Exception If something goes wrong inside
122      */
123     @Test
124     void jumpsToRelativeUrls() throws Exception {
125         MatcherAssert.assertThat(
126             "should contains value & path",
127             new RestResponse(
128                 new FakeRequest()
129                     .uri().set(new URI("http://locahost:888/tt")).back()
130                     .fetch()
131             ).jump(new URI("/foo/bar?hey")).uri().get(),
132             Matchers.hasToString("http://locahost:888/foo/bar?hey")
133         );
134     }
135 
136 }