View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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  public class TestHttpRequestBase {
51  
52      private static final String HOT_URL = "http://host/path";
53  
54      @Test
55      public 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      public 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      public 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      public 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      public 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      public 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      public 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     public 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     public void testGetMethodEmptyURI() throws Exception {
114         final HttpGet httpget = new HttpGet("");
115         Assertions.assertEquals(new URI("/"), httpget.getUri());
116     }
117 
118     @Test
119     public void testPostMethodEmptyURI() throws Exception {
120         final HttpPost HttpPost = new HttpPost("");
121         Assertions.assertEquals(new URI("/"), HttpPost.getUri());
122     }
123 
124     @Test
125     public void testHeadMethodEmptyURI() throws Exception {
126         final HttpHead httpHead = new HttpHead("");
127         Assertions.assertEquals(new URI("/"), httpHead.getUri());
128     }
129 
130     @Test
131     public void testOptionMethodEmptyURI() throws Exception {
132         final HttpOptions httpOption = new HttpOptions("");
133         Assertions.assertEquals(new URI("/"), httpOption.getUri());
134     }
135 
136     @Test
137     public void testPatchMethodEmptyURI() throws Exception {
138         final HttpPatch httpPatch = new HttpPatch("");
139         Assertions.assertEquals(new URI("/"), httpPatch.getUri());
140     }
141 
142     @Test
143     public void testPutMethodEmptyURI() throws Exception {
144         final HttpPut httpPut = new HttpPut("");
145         Assertions.assertEquals(new URI("/"), httpPut.getUri());
146     }
147 
148     @Test
149     public void testTraceMethodEmptyURI() throws Exception {
150         final HttpTrace httpTrace = new HttpTrace("");
151         Assertions.assertEquals(new URI("/"), httpTrace.getUri());
152     }
153 
154 
155     @Test
156     public void testDeleteMethodEmptyURI() throws Exception {
157         final HttpDelete httpDelete = new HttpDelete("");
158         Assertions.assertEquals(new URI("/"), httpDelete.getUri());
159     }
160 
161 
162     @Test
163     public 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     public 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(methods.size(), 3),
180                 () -> assertTrue(methods.containsAll(Stream.of("HEAD", "DELETE", "GET")
181                         .collect(Collectors.toCollection(HashSet::new))))
182         );
183     }
184 
185 }