1
2
3
4
5 package com.jcabi.http.request;
6
7 import com.jcabi.http.Request;
8 import com.jcabi.http.response.RestResponse;
9 import jakarta.ws.rs.core.HttpHeaders;
10 import jakarta.ws.rs.core.MediaType;
11 import java.io.ByteArrayInputStream;
12 import java.net.HttpURLConnection;
13 import java.net.URI;
14 import java.nio.charset.StandardCharsets;
15 import org.hamcrest.MatcherAssert;
16 import org.hamcrest.Matchers;
17 import org.junit.jupiter.api.Assertions;
18 import org.junit.jupiter.api.Test;
19
20
21
22
23
24 final class FakeRequestTest {
25
26
27
28
29
30 @Test
31 void sendsHttpRequestAndProcessesHttpResponse() throws Exception {
32 this.generateMainRequest()
33 .withBody("how are you?")
34 .uri().path("/helloall").back()
35 .method(Request.POST)
36 .fetch().as(RestResponse.class)
37 .assertStatus(HttpURLConnection.HTTP_OK)
38 .assertHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
39 .assertBody(Matchers.containsString("are you?"));
40 }
41
42
43
44
45
46 @Test
47 void sendsHttpRequestAndProcessesHttpBinaryResponse()
48 throws Exception {
49 final byte[] content = "Binary body content".getBytes();
50 this.generateMainRequest()
51 .withBody(content)
52 .uri().path("/binContent").back()
53 .method(Request.POST)
54 .fetch().as(RestResponse.class)
55 .assertStatus(HttpURLConnection.HTTP_OK)
56 .assertHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
57 .assertBinary(Matchers.equalTo(content));
58 }
59
60
61
62
63
64 @Test
65 void changesUri() throws Exception {
66 MatcherAssert.assertThat(
67 "should ends with 'facebook.com/'",
68 new FakeRequest()
69 .uri().set(new URI("http://facebook.com")).back()
70 .uri().get().toString(),
71 Matchers.endsWith("facebook.com/")
72 );
73 }
74
75
76
77
78
79 @Test
80 void changesUriInResponse() throws Exception {
81 MatcherAssert.assertThat(
82 "should contains 'google.com'",
83 new FakeRequest()
84 .uri().set(new URI("http://google.com")).back()
85 .fetch().back()
86 .uri().get().toString(),
87 Matchers.containsString("google.com")
88 );
89 }
90
91
92
93
94
95 @Test
96 void fetchThrowsExceptionWhenBodyIsNotEmpty() {
97 Assertions.assertThrows(
98 IllegalStateException.class,
99 () -> new FakeRequest()
100 .withStatus(HttpURLConnection.HTTP_OK)
101 .withBody("blah")
102 .fetch(
103 new ByteArrayInputStream(
104 "foo".getBytes(StandardCharsets.UTF_8)
105 )
106 )
107 );
108 }
109
110
111
112
113
114
115 @Test
116 void fakeRequestReturnsResponseBody() throws Exception {
117 final String response = "the response body";
118 final String request = "the request body";
119 new FakeRequest().withBody(response)
120 .body().set(request).back()
121 .fetch()
122 .as(RestResponse.class)
123 .assertBody(
124 Matchers.allOf(
125 Matchers.is(response),
126 Matchers.not(Matchers.is(request))
127 )
128 );
129 }
130
131
132
133
134 @Test
135 void identifiesUniquely() {
136 MatcherAssert.assertThat(
137 "should not equals",
138 new FakeRequest().header("header-1", "value-1"),
139 Matchers.not(
140 Matchers.equalTo(
141 new FakeRequest().header("header-2", "value-2")
142 )
143 )
144 );
145 MatcherAssert.assertThat(
146 "should equals",
147 new FakeRequest(),
148 Matchers.equalTo(new FakeRequest())
149 );
150 }
151
152
153
154
155
156 private FakeRequest generateMainRequest() {
157 return new FakeRequest()
158 .withStatus(HttpURLConnection.HTTP_OK)
159 .withReason("OK")
160 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
161 }
162
163 }