1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
46
47
48 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
49 final class RestResponseTest {
50
51
52
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
73
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
96
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
120
121
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
149
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 }