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;
31  
32  import com.jcabi.http.mock.MkAnswer;
33  import com.jcabi.http.mock.MkContainer;
34  import com.jcabi.http.mock.MkGrizzlyContainer;
35  import com.jcabi.http.mock.MkQuery;
36  import com.jcabi.http.request.ApacheRequest;
37  import com.jcabi.http.response.JsonResponse;
38  import com.jcabi.http.response.RestResponse;
39  import com.jcabi.http.response.XmlResponse;
40  import jakarta.json.Json;
41  import java.io.IOException;
42  import java.net.HttpURLConnection;
43  import java.net.URI;
44  import org.hamcrest.MatcherAssert;
45  import org.hamcrest.Matchers;
46  import org.junit.jupiter.api.Assertions;
47  import org.junit.jupiter.api.Test;
48  import org.junit.jupiter.api.Timeout;
49  import org.junit.jupiter.api.function.Executable;
50  import org.junit.jupiter.params.ParameterizedTest;
51  
52  
53  
54  
55  
56  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
57  final class RequestITCase extends RequestTestTemplate {
58  
59      
60  
61  
62  
63  
64      @Values
65      @ParameterizedTest
66      @Timeout(10)
67      void sendsHttpRequestAndProcessesHttpResponse(
68          final Class<? extends Request> type
69      ) throws Exception {
70          RequestTestTemplate.request(new URI("https://www.rt.com/rss/"), type)
71              .fetch().as(RestResponse.class)
72              .assertStatus(HttpURLConnection.HTTP_OK)
73              .as(XmlResponse.class)
74              .assertXPath("/rss/channel");
75      }
76  
77      
78  
79  
80  
81  
82      @Values
83      @ParameterizedTest
84      @Timeout(10)
85      void processesNotOkHttpResponse(
86          final Class<? extends Request> type
87      ) throws Exception {
88          RequestTestTemplate.request(new URI("https://example.com/file-not-found.txt"), type)
89              .fetch().as(RestResponse.class)
90              .assertStatus(HttpURLConnection.HTTP_NOT_FOUND);
91      }
92  
93      
94  
95  
96  
97      @Values
98      @ParameterizedTest
99      void continuesOnConnectionError(final Class<? extends Request> type) {
100         Assertions.assertThrows(
101             IOException.class,
102             new Executable() {
103                 @Override
104                 public void execute() throws Throwable {
105                     RequestTestTemplate.request(
106                         new URI("http://localhost:6868/"),
107                         type
108                     ).method(Request.GET).fetch();
109                 }
110             }
111         );
112     }
113 
114     @Test
115     void handlesGet() throws Exception {
116         try (MkContainer container = new MkGrizzlyContainer()
117             .next(
118                 new MkAnswer.Simple(
119                     HttpURLConnection.HTTP_OK,
120                     Json.createObjectBuilder().toString()
121                 )
122             ).start()) {
123             new ApacheRequest(container.home())
124                 .method(Request.GET)
125                 .fetch()
126                 .as(RestResponse.class)
127                 .assertStatus(HttpURLConnection.HTTP_OK);
128             final MkQuery query = container.take();
129             MatcherAssert.assertThat(
130                 query.method(),
131                 Matchers.is("GET")
132             );
133         }
134     }
135 
136     @Test
137     void handlesDelete() throws Exception {
138         try (MkContainer container = new MkGrizzlyContainer()
139             .next(
140                 new MkAnswer.Simple(
141                     HttpURLConnection.HTTP_OK,
142                     Json.createObjectBuilder().toString()
143                 )
144             ).start()) {
145             new ApacheRequest(container.home())
146                 .method(Request.DELETE)
147                 .fetch()
148                 .as(RestResponse.class)
149                 .assertStatus(HttpURLConnection.HTTP_OK);
150             MatcherAssert.assertThat(
151                 container.take().method(),
152                 Matchers.is("DELETE")
153             );
154         }
155     }
156 
157     @Test
158     void handlesDeleteWithBody() throws Exception {
159         try (MkContainer container = new MkGrizzlyContainer()
160             .next(
161                 new MkAnswer.Simple(
162                     HttpURLConnection.HTTP_OK,
163                     Json.createObjectBuilder().toString()
164                 )
165             ).start()) {
166             new ApacheRequest(container.home())
167                 .method(Request.DELETE)
168                 .body().set("{}").back()
169                 .fetch()
170                 .as(RestResponse.class)
171                 .assertStatus(HttpURLConnection.HTTP_OK)
172                 .as(JsonResponse.class)
173                 .json();
174             final MkQuery take = container.take();
175             MatcherAssert.assertThat(
176                 take.method(),
177                 Matchers.is("DELETE")
178             );
179             MatcherAssert.assertThat(
180                 take.body(),
181                 Matchers.is("{}")
182             );
183         }
184     }
185 }