Now that ClassicHttpRequests is no longer an enum, we need to way to

generically build requests from method names. Update all factory classes
with matching APIs for Method and String method name inputs.

(#204), (#205)
This commit is contained in:
Gary Gregory 2020-01-28 12:17:46 -05:00 committed by Oleg Kalnichevski
parent afa8f5ecd1
commit 95dbbf099b
7 changed files with 279 additions and 2 deletions

View File

@ -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 This BETA release upgrades HttpCore to the latest version and addresses a number of issues found

View File

@ -28,10 +28,12 @@
package org.apache.hc.client5.http.async.methods; package org.apache.hc.client5.http.async.methods;
import java.net.URI; import java.net.URI;
import java.util.Locale;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Method; import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.message.BasicHttpRequest; 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. * Common HTTP methods using {@link BasicHttpRequest} as a HTTP request message representation.
@ -40,6 +42,38 @@ import org.apache.hc.core5.http.message.BasicHttpRequest;
*/ */
public final class BasicHttpRequests { 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) { public static BasicHttpRequest delete(final String uri) {
return delete(URI.create(uri)); return delete(URI.create(uri));
} }

View File

@ -28,9 +28,11 @@
package org.apache.hc.client5.http.async.methods; package org.apache.hc.client5.http.async.methods;
import java.net.URI; import java.net.URI;
import java.util.Locale;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.Method; 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. * Common HTTP methods using {@link SimpleHttpRequest} as a HTTP request message representation.
@ -39,6 +41,38 @@ import org.apache.hc.core5.http.Method;
*/ */
public final class SimpleHttpRequests { 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) { public static SimpleHttpRequest delete(final String uri) {
return delete(URI.create(uri)); return delete(URI.create(uri));
} }

View File

@ -28,6 +28,10 @@
package org.apache.hc.client5.http.classic.methods; package org.apache.hc.client5.http.classic.methods;
import java.net.URI; import java.net.URI;
import java.util.Locale;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.util.Args;
/** /**
@ -40,6 +44,79 @@ import java.net.URI;
*/ */
public final class ClassicHttpRequests { 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) { public static HttpUriRequest delete(final String uri) {
return delete(URI.create(uri)); return delete(URI.create(uri));
} }

View File

@ -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());
}
}

View File

@ -42,7 +42,8 @@ import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class TestBasicHttpRequests { 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}") @Parameters(name = "{index}: {0} => {1}")
public static Iterable<Object[]> data() { public static Iterable<Object[]> data() {
@ -69,6 +70,18 @@ public class TestBasicHttpRequests {
this.expectedMethod = expectedMethod; 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 @Test
public void testCreateClassicHttpRequest() throws Exception { public void testCreateClassicHttpRequest() throws Exception {
final Method httpMethod = BasicHttpRequests.class.getMethod(methodName, URI.class); final Method httpMethod = BasicHttpRequests.class.getMethod(methodName, URI.class);

View File

@ -70,6 +70,16 @@ public class TestClassicHttpRequests {
this.expectedClass = expectedClass; 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 @Test
public void testCreateFromString() throws Exception { public void testCreateFromString() throws Exception {
final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, String.class); final Method httpMethod = ClassicHttpRequests.class.getMethod(methodName, String.class);