use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class OriginTestCase method testStrictOrigin.
/**
* Tests the Origin header is respected when the strictest options are selected
*
*/
@Test
public void testStrictOrigin() throws IOException {
TestHttpClient client = new TestHttpClient();
try {
final OriginHandler handler = new OriginHandler();
handler.addAllowedOrigins("http://www.mysite.com:80", "http://mysite.com:80");
DefaultServer.setRootHandler(handler);
handler.setNext(ResponseCodeHandler.HANDLE_200);
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
HttpResponse result = client.execute(get);
//no origin header, we dny by default
Assert.assertEquals(StatusCodes.FORBIDDEN, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ORIGIN_STRING, "http://www.mysite.com:80");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ORIGIN_STRING, "http://www.mysite.com:80");
get.setHeader(Headers.ORIGIN_STRING, "http://mysite.com:80");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ORIGIN_STRING, "http://www.mysite.com:80");
get.setHeader(Headers.ORIGIN_STRING, "bogus");
result = client.execute(get);
Assert.assertEquals(StatusCodes.FORBIDDEN, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
get.setHeader(Headers.ORIGIN_STRING, "http://www.mysite.com:80");
get.setHeader(Headers.ORIGIN_STRING, "bogus");
result = client.execute(get);
Assert.assertEquals(StatusCodes.FORBIDDEN, result.getStatusLine().getStatusCode());
HttpClientUtils.readResponse(result);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class PredicatedHandlersProxyTestCase method testProxy.
@Test
public void testProxy() throws Exception {
TestHttpClient client = new TestHttpClient();
int port = getHostPort("default");
String upstreamUrl = "http://" + NetworkUtils.formatPossibleIpv6Address(getHostAddress("default")) + ":" + (port + 1);
DefaultServer.setRootHandler(Handlers.predicates(PredicatedHandlersParser.parse(String.format("path-suffix['.html'] -> reverse-proxy[hosts={'%1$s'}, rewrite-host-header=true]\n" + "path-suffix['.jsp'] -> reverse-proxy[hosts={'%1$s'}]", upstreamUrl), getClass().getClassLoader()), ResponseCodeHandler.HANDLE_404));
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/foo.html");
get.addHeader("Host", "original-host");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Header[] header = result.getHeaders("myHost");
Assert.assertEquals("upstream-host", header[0].getValue());
HttpClientUtils.readResponse(result);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/foo.jsp");
get.addHeader("Host", "original-host");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
header = result.getHeaders("myHost");
Assert.assertEquals("original-host", header[0].getValue());
HttpClientUtils.readResponse(result);
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class PredicatedHandlersTestCase method testRewrite.
@Test
public void testRewrite() throws IOException {
DefaultServer.setRootHandler(Handlers.predicates(PredicatedHandlersParser.parse("path(/skipallrules) and true -> done\n" + "method(GET) -> set(attribute='%{o,type}', value=get) \r\n" + "regex('(.*).css') -> {rewrite['${1}.xcss'];set(attribute='%{o,chained}', value=true)} \n" + "regex('(.*).redirect$') -> redirect['${1}.redirected']\n\n\n\n\n" + "set[attribute='%{o,someHeader}', value=always]\n" + "path-template('/foo/{bar}/{f}') -> set[attribute='%{o,template}', value='${bar}']\r\n" + "path-template('/bar->foo') -> redirect(/);" + "regex('(.*).css') -> set[attribute='%{o,css}', value='true'] else set[attribute='%{o,css}', value='false']; " + "path(/restart) -> {rewrite(/foo/a/b); restart; }\r\n", getClass().getClassLoader()), new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send(exchange.getRelativePath());
}
}));
TestHttpClient client = new TestHttpClient();
try {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/foo/a/b");
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertEquals("get", result.getHeaders("type")[0].getValue());
Assert.assertEquals("always", result.getHeaders("someHeader")[0].getValue());
Assert.assertEquals("a", result.getHeaders("template")[0].getValue());
Assert.assertEquals("false", result.getHeaders("css")[0].getValue());
Assert.assertEquals("/foo/a/b", response);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/foo/a/b.css");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("get", result.getHeaders("type")[0].getValue());
Assert.assertEquals("true", result.getHeaders("chained")[0].getValue());
Assert.assertEquals("/foo/a/b.xcss", response);
Assert.assertEquals("always", result.getHeaders("someHeader")[0].getValue());
Assert.assertEquals("true", result.getHeaders("css")[0].getValue());
Assert.assertEquals("a", result.getHeaders("template")[0].getValue());
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/foo/a/b.redirect");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("get", result.getHeaders("type")[0].getValue());
Assert.assertEquals("always", result.getHeaders("someHeader")[0].getValue());
Assert.assertEquals("a", result.getHeaders("template")[0].getValue());
Assert.assertEquals("/foo/a/b.redirected", response);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/skipallrules");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals(0, result.getHeaders("someHeader").length);
get = new HttpGet(DefaultServer.getDefaultServerURL() + "/restart");
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("get", result.getHeaders("type")[0].getValue());
Assert.assertEquals("always", result.getHeaders("someHeader")[0].getValue());
Assert.assertEquals("a", result.getHeaders("template")[0].getValue());
Assert.assertEquals("/foo/a/b", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class NewlineInHeadersTestCase method testNewlineInHeaders.
@Test
public void testNewlineInHeaders() throws IOException {
DefaultServer.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() {
@Override
public void handle(HttpServerExchange exchange, String message) {
exchange.getResponseHeaders().put(HttpString.tryFromString(ECHO), message);
exchange.getResponseSender().send(RESPONSE);
}
});
}
});
final TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL());
post.setEntity(new StringEntity("test"));
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("test", result.getFirstHeader(ECHO).getValue());
Assert.assertEquals(RESPONSE, HttpClientUtils.readResponse(result));
post = new HttpPost(DefaultServer.getDefaultServerURL());
post.setEntity(new StringEntity("test\nnewline"));
result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("test newline", result.getFirstHeader(ECHO).getValue());
Assert.assertEquals(RESPONSE, HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
use of io.undertow.testutils.TestHttpClient in project undertow by undertow-io.
the class FixedLengthResponseTestCase method sendHttpRequest.
@Test
public void sendHttpRequest() throws IOException {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path");
TestHttpClient client = new TestHttpClient();
try {
generateMessage(1);
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(message, HttpClientUtils.readResponse(result));
generateMessage(1000);
result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals(message, HttpClientUtils.readResponse(result));
} finally {
client.getConnectionManager().shutdown();
}
}
Aggregations