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));
}
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);
}
}
}
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));
}
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);
}
}
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¶m2=$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¶m2=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();
}
}
Aggregations