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.async.methods;
29  
30  import java.lang.reflect.Method;
31  import java.net.URI;
32  import java.util.Arrays;
33  
34  import org.apache.hc.core5.http.HttpRequest;
35  import org.junit.Assert;
36  import org.junit.Test;
37  import org.junit.runner.RunWith;
38  import org.junit.runners.Parameterized;
39  import org.junit.runners.Parameterized.Parameters;
40  
41  @RunWith(Parameterized.class)
42  public class SimpleBasicHttpRequests {
43  
44      private static final String URI_STRING_FIXTURE = "http://localhost";
45      private static final URI URI_FIXTURE = URI.create(URI_STRING_FIXTURE);
46  
47    @Parameters(name = "{index}: {0} => {1}")
48    public static Iterable<Object[]> data() {
49      return Arrays.asList(new Object[][] {
50        // @formatter:off
51        { "delete", "DELETE" },
52        { "get", "GET" },
53        { "head", "HEAD" },
54        { "options", "OPTIONS" },
55        { "patch", "PATCH" },
56        { "post", "POST" },
57        { "put", "PUT" },
58        { "trace", "TRACE" }
59        // @formatter:on
60      });
61    }
62  
63    private final String methodName;
64  
65    private final String expectedMethod;
66  
67    public SimpleBasicHttpRequests(final String methodName, final String expectedMethod) {
68      this.methodName = methodName;
69      this.expectedMethod = expectedMethod;
70    }
71  
72    @Test
73    public void testCreateMethodUri() {
74        Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(methodName, URI_FIXTURE).getClass());
75        Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(expectedMethod, URI_FIXTURE).getClass());
76    }
77  
78    @Test
79    public void testCreateMethodUriString() {
80        Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass());
81        Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(expectedMethod, URI_STRING_FIXTURE).getClass());
82    }
83  
84    @Test
85    public void testCreateClassicHttpRequest() throws Exception {
86      final Method httpMethod = SimpleHttpRequests.class.getMethod(methodName, URI.class);
87      final HttpRequest basicHttpRequest = (HttpRequest) httpMethod.invoke(null, URI_FIXTURE);
88      Assert.assertEquals(SimpleHttpRequest.class, basicHttpRequest.getClass());
89      Assert.assertEquals(expectedMethod, basicHttpRequest.getMethod());
90    }
91  }