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