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 java.lang.reflect.Method;
31  import java.net.URI;
32  import java.util.Arrays;
33  
34  import org.apache.hc.core5.http.ClassicHttpRequest;
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 TestClassicHttpRequests {
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", HttpDelete.class },
52        { "get", HttpGet.class },
53        { "head", HttpHead.class },
54        { "options", HttpOptions.class },
55        { "patch", HttpPatch.class },
56        { "post", HttpPost.class },
57        { "put", HttpPut.class },
58        { "trace", HttpTrace.class }
59        // @formatter:on
60      });
61    }
62  
63    private final String methodName;
64  
65    private final Class<ClassicHttpRequest> expectedClass;
66  
67    public TestClassicHttpRequests(final String methodName,
68        final Class<ClassicHttpRequest> expectedClass) {
69      this.methodName = methodName;
70      this.expectedClass = expectedClass;
71    }
72  
73    @Test
74    public void testCreateMethodUri() {
75        Assert.assertEquals(expectedClass, ClassicHttpRequests.create(methodName, URI_FIXTURE).getClass());
76    }
77  
78    @Test
79    public void testCreateMethodUriString() {
80        Assert.assertEquals(expectedClass, ClassicHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass());
81    }
82  
83    @Test
84    public void testCreateFromString() throws Exception {
85        final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, String.class);
86        Assert.assertEquals(expectedClass,
87                httpMethod.invoke(null, URI_STRING_FIXTURE).getClass());
88    }
89  
90    @Test
91    public void testCreateFromURI() throws Exception {
92      final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, URI.class);
93      Assert.assertEquals(expectedClass,
94              httpMethod.invoke(null, URI_FIXTURE).getClass());
95    }
96  }