Search in sources :

Example 11 with TestHttpClient

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();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 12 with TestHttpClient

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);
}
Also used : Header(org.apache.http.Header) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 13 with TestHttpClient

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();
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 14 with TestHttpClient

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();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponse(org.apache.http.HttpResponse) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Example 15 with TestHttpClient

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();
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) TestHttpClient(io.undertow.testutils.TestHttpClient) Test(org.junit.Test)

Aggregations

TestHttpClient (io.undertow.testutils.TestHttpClient)302 HttpResponse (org.apache.http.HttpResponse)290 Test (org.junit.Test)269 HttpGet (org.apache.http.client.methods.HttpGet)239 Header (org.apache.http.Header)66 HttpPost (org.apache.http.client.methods.HttpPost)54 IOException (java.io.IOException)38 StringEntity (org.apache.http.entity.StringEntity)30 Path (java.nio.file.Path)29 PathHandler (io.undertow.server.handlers.PathHandler)28 HttpHandler (io.undertow.server.HttpHandler)20 CanonicalPathHandler (io.undertow.server.handlers.CanonicalPathHandler)20 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)20 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)20 ArrayList (java.util.ArrayList)20 HttpServerExchange (io.undertow.server.HttpServerExchange)19 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)16 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)14 NameValuePair (org.apache.http.NameValuePair)13 DigestWWWAuthenticateToken (io.undertow.security.impl.DigestWWWAuthenticateToken)11