1
2
3
4
5 package com.jcabi.http.request;
6
7 import jakarta.ws.rs.HttpMethod;
8 import java.io.IOException;
9 import java.net.MalformedURLException;
10 import java.net.URI;
11 import org.apache.commons.lang3.StringUtils;
12 import org.hamcrest.MatcherAssert;
13 import org.hamcrest.Matchers;
14 import org.junit.jupiter.api.Assertions;
15 import org.junit.jupiter.api.Test;
16
17
18
19
20
21 final class JdkRequestITCase {
22
23
24
25
26 private static final String MESSAGE = "message";
27
28
29
30
31
32 @Test
33 void throwsDescriptiveException() {
34 final String uri = "http://localhost:6789";
35 final String method = HttpMethod.POST;
36 MatcherAssert.assertThat(
37 "should be error with a descriptive message",
38 Assertions.assertThrows(
39 IOException.class,
40 () -> new JdkRequest(new URI(uri)).method(method).fetch()
41 ),
42 Matchers.hasProperty(
43 JdkRequestITCase.MESSAGE,
44 Matchers.allOf(
45 Matchers.containsString(uri),
46 Matchers.containsString(method)
47 )
48 )
49 );
50 }
51
52
53
54
55
56 @Test
57 void failsNoProtocolNoPort() {
58 final String uri = "localhost";
59 MatcherAssert.assertThat(
60 "should be error with a descriptive message",
61 Assertions.assertThrows(
62 IOException.class,
63 () -> new JdkRequest(uri).fetch()
64 ),
65 Matchers.hasProperty(
66 JdkRequestITCase.MESSAGE,
67 Matchers.allOf(
68 Matchers.containsString("is incorrect"),
69 Matchers.containsString(uri)
70 )
71 )
72 );
73 }
74
75
76
77
78
79 @Test
80 void failsWithPortButNoProtocol() {
81 final String url = "test.com";
82 final String colon = ":";
83 MatcherAssert.assertThat(
84 "should be error with a descriptive message",
85 Assertions.assertThrows(
86 MalformedURLException.class,
87 () -> new JdkRequest(
88 StringUtils.join(
89 url,
90 colon,
91 "80"
92 )
93 ).fetch()
94 ),
95 Matchers.hasProperty(
96 JdkRequestITCase.MESSAGE,
97 Matchers.allOf(
98 Matchers.containsString("unknown protocol: "),
99 Matchers.containsString(url)
100 )
101 )
102 );
103 }
104
105
106
107
108
109 @Test
110 void failsMalformedEntirely() {
111 final String uri = "bla bla url";
112 MatcherAssert.assertThat(
113 "should be error with a descriptive message",
114 Assertions.assertThrows(
115 IllegalArgumentException.class,
116 () -> new JdkRequest(uri).fetch()
117 ),
118 Matchers.hasProperty(
119 JdkRequestITCase.MESSAGE,
120 Matchers.allOf(
121 Matchers.containsString("Illegal character in path"),
122 Matchers.containsString(uri)
123 )
124 )
125 );
126 }
127 }