Search in sources :

Example 21 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class PredicateParsingTestCase method testPredicateContextVariable.

@Test
public void testPredicateContextVariable() {
    Predicate predicate = PredicateParser.parse("regex[pattern=\"/publicdb/(.*?)/.*\", value=\"%R\", full-match=false] and equals[%{i,username}, ${1}]", PredicateParsingTestCase.class.getClassLoader());
    HttpServerExchange e = new HttpServerExchange(null);
    e.setRelativePath("/publicdb/foo/bar");
    Assert.assertFalse(predicate.resolve(e));
    e.getRequestHeaders().add(new HttpString("username"), "foo");
    Assert.assertTrue(predicate.resolve(e));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpString(io.undertow.util.HttpString) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 22 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class PredicateParsingTestCase method testArrayValues.

@Test
public void testArrayValues() {
    Predicate predicate;
    for (String string : new String[] { "contains[value=%{i,Content-Type}, search=text]", "contains[value=\"%{i,Content-Type}\", search={text}]", "contains[value=\"%{i,Content-Type}\", search={text, \"other text\"}]" }) {
        try {
            predicate = PredicateParser.parse(string, PredicateParsingTestCase.class.getClassLoader());
            HttpServerExchange e = new HttpServerExchange(null);
            Assert.assertFalse(predicate.resolve(e));
            e.getRequestHeaders().add(Headers.CONTENT_TYPE, "text");
            Assert.assertTrue(predicate.resolve(e));
        } catch (Throwable ex) {
            throw new RuntimeException("String " + string, ex);
        }
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpString(io.undertow.util.HttpString) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 23 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class PredicateParsingTestCase method testRegularExpressionsWithPredicateContext.

@Test
public void testRegularExpressionsWithPredicateContext() {
    Predicate predicate = PredicateParser.parse("regex[pattern=a* , value=%{RELATIVE_PATH}] and equals[{$0, aaa}]", PredicateParsingTestCase.class.getClassLoader());
    HttpServerExchange e = new HttpServerExchange(null);
    e.putAttachment(Predicate.PREDICATE_CONTEXT, new HashMap<String, Object>());
    e.setRelativePath("aaab");
    Assert.assertTrue(predicate.resolve(e));
    e.setRelativePath("aaaab");
    Assert.assertFalse(predicate.resolve(e));
    predicate = PredicateParser.parse("regex[pattern='a(b*)a*' , value=%{RELATIVE_PATH}] and equals[$1, bb]", PredicateParsingTestCase.class.getClassLoader());
    e.putAttachment(Predicate.PREDICATE_CONTEXT, new HashMap<String, Object>());
    e.setRelativePath("abb");
    Assert.assertTrue(predicate.resolve(e));
    e.setRelativePath("abbaaa");
    Assert.assertTrue(predicate.resolve(e));
    e.setRelativePath("abbb");
    Assert.assertFalse(predicate.resolve(e));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpString(io.undertow.util.HttpString) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 24 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class PredicateParsingTestCase method expect.

private void expect(String string, boolean result1, boolean result2) {
    try {
        Predicate predicate = PredicateParser.parse(string, PredicateParsingTestCase.class.getClassLoader());
        HttpServerExchange e = new HttpServerExchange(null);
        e.getRequestHeaders().add(Headers.TRAILER, "a");
        Assert.assertEquals(result1, predicate.resolve(e));
        e.getRequestHeaders().add(Headers.CONTENT_LENGTH, "a");
        Assert.assertEquals(result2, predicate.resolve(e));
    } catch (Throwable ex) {
        throw new RuntimeException("String " + string, ex);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange)

Example 25 with HttpServerExchange

use of io.undertow.server.HttpServerExchange 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" + "regex('^/path/([^/]+)/(.*)/?$') -> rewrite('/newpath'); set(attribute='%{o,result}', value='param1=$1&param2=$2'); done()", 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() + "/path/a/b");
        result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        response = HttpClientUtils.readResponse(result);
        Assert.assertEquals("param1=a&param2=b", result.getHeaders("result")[0].getValue());
        Assert.assertEquals("/newpath", 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)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)270 HttpHandler (io.undertow.server.HttpHandler)126 Test (org.junit.Test)109 IOException (java.io.IOException)87 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)43 TestHttpClient (io.undertow.testutils.TestHttpClient)42 HttpGet (org.apache.http.client.methods.HttpGet)40 HttpResponse (org.apache.http.HttpResponse)37 HttpString (io.undertow.util.HttpString)36 Header (org.apache.http.Header)24 Undertow (io.undertow.Undertow)19 ByteBuffer (java.nio.ByteBuffer)19 Map (java.util.Map)16 SessionConfig (io.undertow.server.session.SessionConfig)15 Sender (io.undertow.io.Sender)14 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 HeaderValues (io.undertow.util.HeaderValues)12 URI (java.net.URI)12