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.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
45
46
47 final class JdkRequestITCase {
48
49
50
51
52 private static final String MESSAGE = "message";
53
54
55
56
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
83
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
109
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
144
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 }