add path method to http request builder
This commit is contained in:
parent
b02014c744
commit
0a4cf575d6
6 changed files with 61 additions and 2 deletions
|
@ -1,6 +1,6 @@
|
||||||
group = org.xbib
|
group = org.xbib
|
||||||
name = netty-http-client
|
name = netty-http-client
|
||||||
version = 4.1.11.0
|
version = 4.1.11.1
|
||||||
|
|
||||||
netty.version = 4.1.11.Final
|
netty.version = 4.1.11.Final
|
||||||
tcnative.version = 2.0.1.Final
|
tcnative.version = 2.0.1.Final
|
||||||
|
|
|
@ -104,6 +104,11 @@ public class HttpClientBuilder implements HttpClientChannelContextDefaults {
|
||||||
|
|
||||||
private Socks5ProxyHandler socks5ProxyHandler;
|
private Socks5ProxyHandler socks5ProxyHandler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set byte buf allocator for payload in HTTP requests.
|
||||||
|
* @param byteBufAllocator the byte buf allocator
|
||||||
|
* @return this builder
|
||||||
|
*/
|
||||||
public HttpClientBuilder withByteBufAllocator(ByteBufAllocator byteBufAllocator) {
|
public HttpClientBuilder withByteBufAllocator(ByteBufAllocator byteBufAllocator) {
|
||||||
this.byteBufAllocator = byteBufAllocator;
|
this.byteBufAllocator = byteBufAllocator;
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -79,7 +79,7 @@ public class HttpClientRequestBuilder implements HttpRequestBuilder, HttpRequest
|
||||||
|
|
||||||
private int maxRedirects = DEFAULT_MAX_REDIRECT;
|
private int maxRedirects = DEFAULT_MAX_REDIRECT;
|
||||||
|
|
||||||
private URI uri;
|
private URI uri = DEFAULT_URI;
|
||||||
|
|
||||||
private QueryStringEncoder queryStringEncoder;
|
private QueryStringEncoder queryStringEncoder;
|
||||||
|
|
||||||
|
@ -163,6 +163,16 @@ public class HttpClientRequestBuilder implements HttpRequestBuilder, HttpRequest
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HttpRequestBuilder path(String path) {
|
||||||
|
if (this.uri != null) {
|
||||||
|
setURL(this.uri.resolve(path).toString());
|
||||||
|
} else {
|
||||||
|
setURL(path);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HttpRequestBuilder addHeader(String name, Object value) {
|
public HttpRequestBuilder addHeader(String name, Object value) {
|
||||||
headers.add(name, value);
|
headers.add(name, value);
|
||||||
|
|
|
@ -41,6 +41,8 @@ public interface HttpRequestBuilder {
|
||||||
|
|
||||||
HttpRequestBuilder setURL(String url);
|
HttpRequestBuilder setURL(String url);
|
||||||
|
|
||||||
|
HttpRequestBuilder path(String path);
|
||||||
|
|
||||||
HttpRequestBuilder setHeader(String name, Object value);
|
HttpRequestBuilder setHeader(String name, Object value);
|
||||||
|
|
||||||
HttpRequestBuilder addHeader(String name, Object value);
|
HttpRequestBuilder addHeader(String name, Object value);
|
||||||
|
|
|
@ -17,6 +17,8 @@ package org.xbib.netty.http.client;
|
||||||
|
|
||||||
import io.netty.handler.codec.http.HttpVersion;
|
import io.netty.handler.codec.http.HttpVersion;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
public interface HttpRequestDefaults {
|
public interface HttpRequestDefaults {
|
||||||
|
@ -25,6 +27,8 @@ public interface HttpRequestDefaults {
|
||||||
|
|
||||||
String DEFAULT_USER_AGENT = HttpClientUserAgent.getUserAgent();
|
String DEFAULT_USER_AGENT = HttpClientUserAgent.getUserAgent();
|
||||||
|
|
||||||
|
URI DEFAULT_URI = URI.create("http://localhost");
|
||||||
|
|
||||||
boolean DEFAULT_GZIP = true;
|
boolean DEFAULT_GZIP = true;
|
||||||
|
|
||||||
boolean DEFAULT_FOLLOW_REDIRECT = true;
|
boolean DEFAULT_FOLLOW_REDIRECT = true;
|
||||||
|
|
38
src/test/java/org/xbib/netty/http/client/test/URITest.java
Normal file
38
src/test/java/org/xbib/netty/http/client/test/URITest.java
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package org.xbib.netty.http.client.test;
|
||||||
|
|
||||||
|
import io.netty.handler.codec.http.HttpMethod;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.xbib.netty.http.client.HttpClientRequestBuilder;
|
||||||
|
import org.xbib.netty.http.client.HttpRequestBuilder;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public class URITest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testURIResolve() {
|
||||||
|
URI uri = URI.create("http://localhost");
|
||||||
|
URI uri2 = uri.resolve("/path");
|
||||||
|
assertEquals("http://localhost/path", uri2.toString());
|
||||||
|
uri = URI.create("http://localhost/path1?a=b");
|
||||||
|
uri2 = uri.resolve("path2?c=d");
|
||||||
|
assertEquals("http://localhost/path2?c=d", uri2.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testClientRequestURIs() {
|
||||||
|
HttpRequestBuilder httpRequestBuilder = HttpClientRequestBuilder.builder(HttpMethod.GET);
|
||||||
|
httpRequestBuilder.setURL("https://localhost").path("/path");
|
||||||
|
assertEquals("/path", httpRequestBuilder.build().uri());
|
||||||
|
httpRequestBuilder.path("/foobar");
|
||||||
|
assertEquals("/foobar", httpRequestBuilder.build().uri());
|
||||||
|
httpRequestBuilder.path("/path1?a=b");
|
||||||
|
assertEquals("/path1?a=b", httpRequestBuilder.build().uri());
|
||||||
|
httpRequestBuilder.path("/path2?c=d");
|
||||||
|
assertEquals("/path2?c=d", httpRequestBuilder.build().uri());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue