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.request;
31
32 import jakarta.ws.rs.HttpMethod;
33 import java.io.IOException;
34 import java.net.MalformedURLException;
35 import java.net.URI;
36 import org.apache.commons.lang3.StringUtils;
37 import org.hamcrest.MatcherAssert;
38 import org.hamcrest.Matchers;
39 import org.junit.jupiter.api.Assertions;
40 import org.junit.jupiter.api.Test;
41 import org.junit.jupiter.api.function.Executable;
42
43 /**
44 * Integration case for {@link JdkRequest}.
45 * @since 1.4.1
46 */
47 final class JdkRequestITCase {
48
49 /**
50 * Property name of Exception.
51 */
52 private static final String MESSAGE = "message";
53
54 /**
55 * BaseRequest throws an exception with a descriptive message showing the
56 * URI and method when an error occurs.
57 */
58 @Test
59 void throwsDescriptiveException() {
60 final String uri = "http://localhost:6789";
61 final String method = HttpMethod.POST;
62 MatcherAssert.assertThat(
63 Assertions.assertThrows(
64 IOException.class,
65 new Executable() {
66 @Override
67 public void execute() throws Throwable {
68 new JdkRequest(new URI(uri)).method(method).fetch();
69 }
70 }),
71 Matchers.hasProperty(
72 JdkRequestITCase.MESSAGE,
73 Matchers.allOf(
74 Matchers.containsString(uri),
75 Matchers.containsString(method)
76 )
77 )
78 );
79 }
80
81 /**
82 * BaseRequest throws an exception with a descriptive message if there is no
83 * port and no protocol mentioned in the uri.
84 */
85 @Test
86 void failsNoProtocolNoPort() {
87 final String uri = "localhost";
88 MatcherAssert.assertThat(
89 Assertions.assertThrows(
90 IOException.class,
91 new Executable() {
92 @Override
93 public void execute() throws Throwable {
94 new JdkRequest(uri).fetch();
95 }
96 }),
97 Matchers.hasProperty(
98 JdkRequestITCase.MESSAGE,
99 Matchers.allOf(
100 Matchers.containsString("is incorrect"),
101 Matchers.containsString(uri)
102 )
103 )
104 );
105 }
106
107 /**
108 * BaseRequest throws an exception with a descriptive message if there is no
109 * protocol mentioned in the uri.
110 */
111 @Test
112 void failsWithPortButNoProtocol() {
113 final String url = "test.com";
114 final String colon = ":";
115 MatcherAssert.assertThat(
116 Assertions.assertThrows(
117 MalformedURLException.class,
118 new Executable() {
119
120 @Override
121 public void execute() throws Throwable {
122 new JdkRequest(
123 StringUtils.join(
124 url,
125 colon,
126 "80"
127 )
128 ).fetch();
129 }
130 }
131 ),
132 Matchers.hasProperty(
133 JdkRequestITCase.MESSAGE,
134 Matchers.allOf(
135 Matchers.containsString("unknown protocol: "),
136 Matchers.containsString(url)
137 )
138 )
139 );
140 }
141
142 /**
143 * BaseRequest throws an exception with a descriptive message
144 * if the uri is completely wrong (e.g. bla bla1)
145 */
146 @Test
147 void failsMalformedEntirely() {
148 final String uri = "bla bla url";
149 MatcherAssert.assertThat(
150 Assertions.assertThrows(
151 IllegalArgumentException.class,
152 new Executable() {
153
154 @Override
155 public void execute() throws Throwable {
156 new JdkRequest(uri).fetch();
157 }
158 }),
159 Matchers.hasProperty(
160 JdkRequestITCase.MESSAGE,
161 Matchers.allOf(
162 Matchers.containsString("Illegal character in path"),
163 Matchers.containsString(uri)
164 )
165 )
166 );
167 }
168 }