View Javadoc
1   /*
2    * Copyright (c) 2011-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Integration case for {@link com.jcabi.http.request.ApacheRequest}.
54   * @since 1.1
55   */
56  @SuppressWarnings("PMD.AvoidDuplicateLiterals")
57  final class RequestITCase extends RequestTestTemplate {
58  
59      /**
60       * BaseRequest can fetch HTTP request and process HTTP response.
61       * @throws Exception If something goes wrong inside
62       * @param type Request type
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       * BaseRequest can process not-OK response.
79       * @throws Exception If something goes wrong inside
80       * @param type Request type
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       * BaseRequest can throw a correct exception on connection error.
95       * @param type Request type
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 }