View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.http;
6   
7   import com.jcabi.http.mock.MkAnswer;
8   import com.jcabi.http.mock.MkContainer;
9   import com.jcabi.http.mock.MkGrizzlyContainer;
10  import com.jcabi.http.mock.MkQuery;
11  import com.jcabi.http.request.ApacheRequest;
12  import com.jcabi.http.response.JsonResponse;
13  import com.jcabi.http.response.RestResponse;
14  import com.jcabi.http.response.XmlResponse;
15  import jakarta.json.Json;
16  import java.io.IOException;
17  import java.net.HttpURLConnection;
18  import java.net.URI;
19  import org.hamcrest.MatcherAssert;
20  import org.hamcrest.Matchers;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.api.Timeout;
24  import org.junit.jupiter.api.condition.DisabledOnOs;
25  import org.junit.jupiter.api.condition.OS;
26  import org.junit.jupiter.params.ParameterizedTest;
27  
28  /**
29   * Integration case for {@link com.jcabi.http.request.ApacheRequest}.
30   * @since 1.1
31   */
32  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
33  final class RequestITCase extends RequestTestTemplate {
34  
35      /**
36       * BaseRequest can fetch HTTP request and process HTTP response.
37       * @param type Request type
38       * @throws Exception If something goes wrong inside
39       */
40      @Values
41      @ParameterizedTest
42      @Timeout(10)
43      @DisabledOnOs(OS.WINDOWS)
44      void sendsHttpRequestAndProcessesHttpResponse(
45          final Class<? extends Request> type
46      ) throws Exception {
47          RequestTestTemplate.request(new URI("https://www.rt.com/rss/"), type)
48              .fetch().as(RestResponse.class)
49              .assertStatus(HttpURLConnection.HTTP_OK)
50              .as(XmlResponse.class)
51              .assertXPath("/rss/channel");
52      }
53  
54      /**
55       * BaseRequest can process not-OK response.
56       * @param type Request type
57       * @throws Exception If something goes wrong inside
58       */
59      @Values
60      @ParameterizedTest
61      @Timeout(10)
62      void processesNotOkHttpResponse(
63          final Class<? extends Request> type
64      ) throws Exception {
65          RequestTestTemplate.request(new URI("https://badssl.com/404"), type)
66              .fetch().as(RestResponse.class)
67              .assertStatus(HttpURLConnection.HTTP_NOT_FOUND);
68      }
69  
70      /**
71       * BaseRequest can throw a correct exception on connection error.
72       * @param type Request type
73       */
74      @Values
75      @ParameterizedTest
76      void continuesOnConnectionError(final Class<? extends Request> type) {
77          Assertions.assertThrows(
78              IOException.class,
79              () -> RequestTestTemplate.request(
80                  new URI("http://localhost:6868/"),
81                  type
82              ).method(Request.GET).fetch()
83          );
84      }
85  
86      @Test
87      void handlesGet() throws Exception {
88          try (MkContainer container = new MkGrizzlyContainer()
89              .next(
90                  new MkAnswer.Simple(
91                      HttpURLConnection.HTTP_OK,
92                      Json.createObjectBuilder().toString()
93                  )
94              ).start()) {
95              new ApacheRequest(container.home())
96                  .method(Request.GET)
97                  .fetch()
98                  .as(RestResponse.class)
99                  .assertStatus(HttpURLConnection.HTTP_OK);
100             final MkQuery query = container.take();
101             MatcherAssert.assertThat(
102                 "should be 'GET'",
103                 query.method(),
104                 Matchers.is("GET")
105             );
106         }
107     }
108 
109     @Test
110     void handlesDelete() throws Exception {
111         try (MkContainer container = new MkGrizzlyContainer()
112             .next(
113                 new MkAnswer.Simple(
114                     HttpURLConnection.HTTP_OK,
115                     Json.createObjectBuilder().toString()
116                 )
117             ).start()) {
118             new ApacheRequest(container.home())
119                 .method(Request.DELETE)
120                 .fetch()
121                 .as(RestResponse.class)
122                 .assertStatus(HttpURLConnection.HTTP_OK);
123             MatcherAssert.assertThat(
124                 "should be 'DELETE'",
125                 container.take().method(),
126                 Matchers.is("DELETE")
127             );
128         }
129     }
130 
131     @Test
132     void handlesDeleteWithBody() throws Exception {
133         try (MkContainer container = new MkGrizzlyContainer()
134             .next(
135                 new MkAnswer.Simple(
136                     HttpURLConnection.HTTP_OK,
137                     Json.createObjectBuilder().toString()
138                 )
139             ).start()) {
140             new ApacheRequest(container.home())
141                 .method(Request.DELETE)
142                 .body().set("{}").back()
143                 .fetch()
144                 .as(RestResponse.class)
145                 .assertStatus(HttpURLConnection.HTTP_OK)
146                 .as(JsonResponse.class)
147                 .json();
148             final MkQuery take = container.take();
149             MatcherAssert.assertThat(
150                 "should be 'DELETE'",
151                 take.method(),
152                 Matchers.is("DELETE")
153             );
154             MatcherAssert.assertThat(
155                 "should be '{}'",
156                 take.body(),
157                 Matchers.is("{}")
158             );
159         }
160     }
161 }