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 package org.apache.hc.client5.http.classic.methods;
29
30 import static org.junit.jupiter.api.Assertions.assertAll;
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertFalse;
33 import static org.junit.jupiter.api.Assertions.assertThrows;
34 import static org.junit.jupiter.api.Assertions.assertTrue;
35
36 import java.net.URI;
37 import java.util.HashSet;
38 import java.util.Set;
39 import java.util.stream.Collectors;
40 import java.util.stream.Stream;
41
42 import org.apache.hc.client5.http.entity.EntityBuilder;
43 import org.apache.hc.core5.http.HttpEntity;
44 import org.apache.hc.core5.http.HttpResponse;
45 import org.apache.hc.core5.http.HttpStatus;
46 import org.apache.hc.core5.http.message.BasicHttpResponse;
47 import org.junit.jupiter.api.Assertions;
48 import org.junit.jupiter.api.Test;
49
50 class TestHttpRequestBase {
51
52 private static final String HOT_URL = "http://host/path";
53
54 @Test
55 void testBasicGetMethodProperties() throws Exception {
56 final HttpGet httpget = new HttpGet(HOT_URL);
57 Assertions.assertEquals("GET", httpget.getMethod());
58 Assertions.assertEquals(new URI(HOT_URL), httpget.getUri());
59 }
60
61 @Test
62 void testBasicHttpPostMethodProperties() throws Exception {
63 final HttpPost HttpPost = new HttpPost(HOT_URL);
64 Assertions.assertEquals("POST", HttpPost.getMethod());
65 Assertions.assertEquals(new URI(HOT_URL), HttpPost.getUri());
66 }
67
68 @Test
69 void testBasicHttpHeadMethodProperties() throws Exception {
70 final HttpHead httpHead = new HttpHead(HOT_URL);
71 Assertions.assertEquals("HEAD", httpHead.getMethod());
72 Assertions.assertEquals(new URI(HOT_URL), httpHead.getUri());
73 }
74
75 @Test
76 void testBasicHttpOptionMethodProperties() throws Exception {
77 final HttpOptions httpOption = new HttpOptions(HOT_URL);
78 Assertions.assertEquals("OPTIONS", httpOption.getMethod());
79 Assertions.assertEquals(new URI(HOT_URL), httpOption.getUri());
80 }
81
82 @Test
83 void testBasicHttpPatchMethodProperties() throws Exception {
84 final HttpPatch httpPatch = new HttpPatch(HOT_URL);
85 Assertions.assertEquals("PATCH", httpPatch.getMethod());
86 Assertions.assertEquals(new URI(HOT_URL), httpPatch.getUri());
87 }
88
89 @Test
90 void testBasicHttpPutMethodProperties() throws Exception {
91 final HttpPut httpPut = new HttpPut(HOT_URL);
92 Assertions.assertEquals("PUT", httpPut.getMethod());
93 Assertions.assertEquals(new URI(HOT_URL), httpPut.getUri());
94 }
95
96 @Test
97 void testBasicHttpTraceMethodProperties() throws Exception {
98 final HttpTrace httpTrace = new HttpTrace(HOT_URL);
99 Assertions.assertEquals("TRACE", httpTrace.getMethod());
100 Assertions.assertEquals(new URI(HOT_URL), httpTrace.getUri());
101 }
102
103
104 @Test
105 void testBasicHttpDeleteMethodProperties() throws Exception {
106 final HttpDelete httpDelete = new HttpDelete(HOT_URL);
107 Assertions.assertEquals("DELETE", httpDelete.getMethod());
108 Assertions.assertEquals(new URI(HOT_URL), httpDelete.getUri());
109 }
110
111
112 @Test
113 void testGetMethodEmptyURI() throws Exception {
114 final HttpGet httpget = new HttpGet("");
115 Assertions.assertEquals(new URI("/"), httpget.getUri());
116 }
117
118 @Test
119 void testPostMethodEmptyURI() throws Exception {
120 final HttpPost HttpPost = new HttpPost("");
121 Assertions.assertEquals(new URI("/"), HttpPost.getUri());
122 }
123
124 @Test
125 void testHeadMethodEmptyURI() throws Exception {
126 final HttpHead httpHead = new HttpHead("");
127 Assertions.assertEquals(new URI("/"), httpHead.getUri());
128 }
129
130 @Test
131 void testOptionMethodEmptyURI() throws Exception {
132 final HttpOptions httpOption = new HttpOptions("");
133 Assertions.assertEquals(new URI("/"), httpOption.getUri());
134 }
135
136 @Test
137 void testPatchMethodEmptyURI() throws Exception {
138 final HttpPatch httpPatch = new HttpPatch("");
139 Assertions.assertEquals(new URI("/"), httpPatch.getUri());
140 }
141
142 @Test
143 void testPutMethodEmptyURI() throws Exception {
144 final HttpPut httpPut = new HttpPut("");
145 Assertions.assertEquals(new URI("/"), httpPut.getUri());
146 }
147
148 @Test
149 void testTraceMethodEmptyURI() throws Exception {
150 final HttpTrace httpTrace = new HttpTrace("");
151 Assertions.assertEquals(new URI("/"), httpTrace.getUri());
152 }
153
154
155 @Test
156 void testDeleteMethodEmptyURI() throws Exception {
157 final HttpDelete httpDelete = new HttpDelete("");
158 Assertions.assertEquals(new URI("/"), httpDelete.getUri());
159 }
160
161
162 @Test
163 void testTraceMethodSetEntity() {
164 final HttpTrace httpTrace = new HttpTrace(HOT_URL);
165 final HttpEntity entity = EntityBuilder.create().setText("stuff").build();
166 assertThrows(IllegalStateException.class, () -> httpTrace.setEntity(entity));
167 }
168
169 @Test
170 void testOptionMethodGetAllowedMethods() {
171 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK, "OK");
172 response.addHeader("Allow", "GET, HEAD");
173 response.addHeader("Allow", "DELETE");
174 response.addHeader("Content-Length", "128");
175 final HttpOptions httpOptions = new HttpOptions("");
176 final Set<String> methods = httpOptions.getAllowedMethods(response);
177 assertAll("Must all pass",
178 () -> assertFalse(methods.isEmpty()),
179 () -> assertEquals(3, methods.size()),
180 () -> assertTrue(methods.containsAll(Stream.of("HEAD", "DELETE", "GET")
181 .collect(Collectors.toCollection(HashSet::new))))
182 );
183 }
184
185 }