Skip to content

Commit

Permalink
Now that ClassicHttpRequests is no longer an enum, we need to way to
Browse files Browse the repository at this point in the history
generically build requests from method names. Update all factory classes
with matching APIs for Method and String method name inputs.

(apache#204), (apache#205)
  • Loading branch information
garydgregory authored and ok2c committed Jan 28, 2020
1 parent afa8f5e commit 95dbbf0
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 2 deletions.
20 changes: 19 additions & 1 deletion RELEASE_NOTES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
Release 5.0-BETA7
Release 5.0-BETA8
-----------------

* GitHub #204: Build requests from method names in ClassicHttpRequests:
ClassicHttpRequests.create(String, String)
ClassicHttpRequests.create(String, URI)
Contributed by Gary Gregory <ggregory at apache.org>

* GitHub #205: Update request factory classes with matching APIs for Method and String method name inputs:
BasicHttpRequests.create(String, URI)
BasicHttpRequests.create(String, String)
ClassicHttpRequests.create(Method, String)
ClassicHttpRequests.create(Method, URI)
SimpleHttpRequests.create(String, URI)
SimpleHttpRequests.create(String, String)
Contributed by Gary Gregory <ggregory at apache.org>


Release 5.0-BETA7
-----------------

This BETA release upgrades HttpCore to the latest version and addresses a number of issues found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
package org.apache.hc.client5.http.async.methods;

import java.net.URI;
import java.util.Locale;

import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.util.Args;

/**
* Common HTTP methods using {@link BasicHttpRequest} as a HTTP request message representation.
Expand All @@ -40,6 +42,38 @@
*/
public final class BasicHttpRequests {

// TODO Next version of HttpCore:
// Method.normalizedValueOf(method)
private static Method normalizedValueOf(final String method) {
return Method.valueOf(Args.notNull(method, "method").toUpperCase(Locale.ROOT));
}

/**
* Creates a new BasicHttpRequest for the given {@code method} and {@code String} URI.
*
* @param method A method supported by this class.
* @param uri a non-null request string URI.
* @return A new BasicHttpRequest.
*/
public static BasicHttpRequest create(final String method, final String uri) {
// TODO Next version of HttpCore:
// return create(Method.normalizedValueOf(method), uri);
return create(normalizedValueOf(method), uri);
}

/**
* Creates a new BasicHttpRequest for the given {@code method} and {@code URI}.
*
* @param method A method supported by this class.
* @param uri a non-null request URI.
* @return A new BasicHttpRequest.
*/
public static BasicHttpRequest create(final String method, final URI uri) {
// TODO Next version of HttpCore:
// return create(Method.normalizedValueOf(method), uri);
return create(normalizedValueOf(method), uri);
}

public static BasicHttpRequest delete(final String uri) {
return delete(URI.create(uri));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
package org.apache.hc.client5.http.async.methods;

import java.net.URI;
import java.util.Locale;

import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.util.Args;

/**
* Common HTTP methods using {@link SimpleHttpRequest} as a HTTP request message representation.
Expand All @@ -39,6 +41,38 @@
*/
public final class SimpleHttpRequests {

// TODO Next version of HttpCore:
// Method.normalizedValueOf(method)
private static Method normalizedValueOf(final String method) {
return Method.valueOf(Args.notNull(method, "method").toUpperCase(Locale.ROOT));
}

/**
* Creates a new BasicHttpRequest for the given {@code method} and {@code String} URI.
*
* @param method A method supported by this class.
* @param uri a non-null request string URI.
* @return A new BasicHttpRequest.
*/
public static SimpleHttpRequest create(final String method, final String uri) {
// TODO Next version of HttpCore:
// return create(Method.normalizedValueOf(method), uri);
return create(normalizedValueOf(method), uri);
}

/**
* Creates a new BasicHttpRequest for the given {@code method} and {@code URI}.
*
* @param method A method supported by this class.
* @param uri a non-null request URI.
* @return A new BasicHttpRequest.
*/
public static SimpleHttpRequest create(final String method, final URI uri) {
// TODO Next version of HttpCore:
// return create(Method.normalizedValueOf(method), uri);
return create(normalizedValueOf(method), uri);
}

public static SimpleHttpRequest delete(final String uri) {
return delete(URI.create(uri));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
package org.apache.hc.client5.http.classic.methods;

import java.net.URI;
import java.util.Locale;

import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.util.Args;


/**
Expand All @@ -40,6 +44,79 @@
*/
public final class ClassicHttpRequests {

private static Method normalizedValueOf(final String method) {
// TODO Next version of HttpCore:
// Method.normalizedValueOf(method)
return Method.valueOf(Args.notNull(method, "method").toUpperCase(Locale.ROOT));
}

/**
* Creates a new HttpUriRequest for the given {@code Method} and {@code String} URI.
*
* @param method A method.
* @param uri a URI.
* @return a new HttpUriRequest.
*/
public static HttpUriRequest create(final Method method, final String uri) {
return create(method, URI.create(uri));
}

/**
* Creates a new HttpUriRequest for the given {@code Method} and {@code URI}.
*
* @param method A method.
* @param uri a URI.
* @return a new HttpUriRequest.
*/
public static HttpUriRequest create(final Method method, final URI uri) {
switch (Args.notNull(method, "method")) {
case DELETE:
return delete(uri);
case GET:
return get(uri);
case HEAD:
return head(uri);
case OPTIONS:
return options(uri);
case PATCH:
return patch(uri);
case POST:
return post(uri);
case PUT:
return put(uri);
case TRACE:
return trace(uri);
default:
throw new IllegalArgumentException(method.toString());
}
}

/**
* Creates a new HttpUriRequest for the given {@code method} and {@code String} URI.
*
* @param method A method supported by this class.
* @param uri a non-null request string URI.
* @throws IllegalArgumentException if the method is not supported.
* @throws IllegalArgumentException if the string uri is null.
* @return A new HttpUriRequest.
*/
public static HttpUriRequest create(final String method, final String uri) {
return create(normalizedValueOf(method), uri);
}

/**
* Creates a new HttpUriRequest for the given {@code method} and {@code URI}.
*
* @param method A method supported by this class.
* @param uri a non-null request URI.
* @throws IllegalArgumentException if the method is not supported.
* @throws IllegalArgumentException if the uri is null.
* @return A new HttpUriRequest.
*/
public static HttpUriRequest create(final String method, final URI uri) {
return create(normalizedValueOf(method), uri);
}

public static HttpUriRequest delete(final String uri) {
return delete(URI.create(uri));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/

package org.apache.hc.client5.http.async.methods;

import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;

import org.apache.hc.core5.http.HttpRequest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class SimpleBasicHttpRequests {

private static final String URI_STRING_FIXTURE = "http://localhost";
private static final URI URI_FIXTURE = URI.create(URI_STRING_FIXTURE);

@Parameters(name = "{index}: {0} => {1}")
public static Iterable<Object[]> data() {
return Arrays.asList(new Object[][] {
// @formatter:off
{ "delete", "DELETE" },
{ "get", "GET" },
{ "head", "HEAD" },
{ "options", "OPTIONS" },
{ "patch", "PATCH" },
{ "post", "POST" },
{ "put", "PUT" },
{ "trace", "TRACE" }
// @formatter:on
});
}

private final String methodName;

private final String expectedMethod;

public SimpleBasicHttpRequests(final String methodName, final String expectedMethod) {
this.methodName = methodName;
this.expectedMethod = expectedMethod;
}

@Test
public void testCreateMethodUri() {
Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(methodName, URI_FIXTURE).getClass());
Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(expectedMethod, URI_FIXTURE).getClass());
}

@Test
public void testCreateMethodUriString() {
Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass());
Assert.assertEquals(SimpleHttpRequest.class, SimpleHttpRequests.create(expectedMethod, URI_STRING_FIXTURE).getClass());
}

@Test
public void testCreateClassicHttpRequest() throws Exception {
final Method httpMethod = SimpleHttpRequests.class.getMethod(methodName, URI.class);
final HttpRequest basicHttpRequest = (HttpRequest) httpMethod.invoke(null, URI_FIXTURE);
Assert.assertEquals(SimpleHttpRequest.class, basicHttpRequest.getClass());
Assert.assertEquals(expectedMethod, basicHttpRequest.getMethod());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
@RunWith(Parameterized.class)
public class TestBasicHttpRequests {

private static final URI URI_FIXTURE = URI.create("http://localhost");
private static final String URI_STRING_FIXTURE = "http://localhost";
private static final URI URI_FIXTURE = URI.create(URI_STRING_FIXTURE);

@Parameters(name = "{index}: {0} => {1}")
public static Iterable<Object[]> data() {
Expand All @@ -69,6 +70,18 @@ public TestBasicHttpRequests(final String methodName, final String expectedMetho
this.expectedMethod = expectedMethod;
}

@Test
public void testCreateMethodUri() {
Assert.assertEquals(BasicHttpRequest.class, BasicHttpRequests.create(methodName, URI_FIXTURE).getClass());
Assert.assertEquals(BasicHttpRequest.class, BasicHttpRequests.create(expectedMethod, URI_FIXTURE).getClass());
}

@Test
public void testCreateMethodUriString() {
Assert.assertEquals(BasicHttpRequest.class, BasicHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass());
Assert.assertEquals(BasicHttpRequest.class, BasicHttpRequests.create(expectedMethod, URI_STRING_FIXTURE).getClass());
}

@Test
public void testCreateClassicHttpRequest() throws Exception {
final Method httpMethod = BasicHttpRequests.class.getMethod(methodName, URI.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public TestClassicHttpRequests(final String methodName,
this.expectedClass = expectedClass;
}

@Test
public void testCreateMethodUri() {
Assert.assertEquals(expectedClass, ClassicHttpRequests.create(methodName, URI_FIXTURE).getClass());
}

@Test
public void testCreateMethodUriString() {
Assert.assertEquals(expectedClass, ClassicHttpRequests.create(methodName, URI_STRING_FIXTURE).getClass());
}

@Test
public void testCreateFromString() throws Exception {
final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, String.class);
Expand Down

0 comments on commit 95dbbf0

Please sign in to comment.