Search in sources :

Example 1 with Condition

use of com.hotels.styx.server.routing.Condition in project styx by ExpediaGroup.

the class AntlrConditionTest method notExpressionNegatesTheExpressionResult.

@Test
public void notExpressionNegatesTheExpressionResult() {
    Condition condition = condition("NOT header('Host')");
    LiveHttpRequest request1 = newRequest().header(HOST, "bbc.co.uk").build();
    assertThat(condition.evaluate(request1, context), is(false));
    LiveHttpRequest request2 = newRequest().header(CONTENT_LENGTH, 7).build();
    assertThat(condition.evaluate(request2, context), is(true));
}
Also used : Condition(com.hotels.styx.server.routing.Condition) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Test(org.junit.jupiter.api.Test)

Example 2 with Condition

use of com.hotels.styx.server.routing.Condition in project styx by ExpediaGroup.

the class AntlrConditionTest method notMatchingRequestPath.

@Test
public void notMatchingRequestPath() {
    Condition condition = condition("path() == '/some-request'");
    assertThat(condition.evaluate(newRequest("/random-request").build(), context), is(false));
}
Also used : Condition(com.hotels.styx.server.routing.Condition) Test(org.junit.jupiter.api.Test)

Example 3 with Condition

use of com.hotels.styx.server.routing.Condition in project styx by ExpediaGroup.

the class AntlrConditionTest method notExpressionHasHigherPrecedenceThanAndExpression.

@Test
public void notExpressionHasHigherPrecedenceThanAndExpression() {
    Condition condition = condition("header('Host') " + "AND NOT header('App-Name') =~ 'app[0-9]' OR header('App-Name') =~ 'shop[0-9]'");
    LiveHttpRequest request1 = newRequest().header(HOST, "bbc.co.uk").header("App-Name", "landing1").build();
    assertThat(condition.evaluate(request1, context), is(true));
    LiveHttpRequest request2 = newRequest().header(HOST, "bbc.co.uk").header("App-Name", "app2").build();
    assertThat(condition.evaluate(request2, context), is(false));
    LiveHttpRequest request3 = newRequest().header(HOST, "bbc.co.uk").header("App-Name", "shop1").build();
    assertThat(condition.evaluate(request3, context), is(true));
}
Also used : Condition(com.hotels.styx.server.routing.Condition) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Test(org.junit.jupiter.api.Test)

Example 4 with Condition

use of com.hotels.styx.server.routing.Condition in project styx by ExpediaGroup.

the class AntlrConditionTest method regexpMatchesHttpHeader.

@Test
public void regexpMatchesHttpHeader() {
    Condition condition = condition("header('Host') =~ '.*\\.co\\.uk'");
    LiveHttpRequest request = newRequest().header(HOST, "bbc.co.uk").build();
    assertThat(condition.evaluate(request, context), is(true));
    LiveHttpRequest request2 = request.newBuilder().header(HOST, "hotels.com").build();
    assertThat(condition.evaluate(request2, context), is(false));
    LiveHttpRequest request3 = request.newBuilder().header(HOST, "hotels.co.uk").build();
    assertThat(condition.evaluate(request3, context), is(true));
    LiveHttpRequest request4 = newRequest().build();
    assertThat(condition.evaluate(request4, context), is(false));
}
Also used : Condition(com.hotels.styx.server.routing.Condition) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Test(org.junit.jupiter.api.Test)

Example 5 with Condition

use of com.hotels.styx.server.routing.Condition in project styx by ExpediaGroup.

the class AntlrConditionTest method combinesStringEqualsAndStringRegexpMatchOperationsWithAndOperator.

@Test
public void combinesStringEqualsAndStringRegexpMatchOperationsWithAndOperator() {
    Condition condition = condition("header('Host') == 'bbc.co.uk' AND header('Content-Length') =~ '[123][0-9]'");
    LiveHttpRequest request = newRequest().header(HOST, "bbc.co.uk").header(CONTENT_LENGTH, "20").build();
    assertThat(condition.evaluate(request, context), is(true));
    LiveHttpRequest request2 = newRequest().header(HOST, "bbc.co.uk").header(CONTENT_LENGTH, "70").build();
    assertThat(condition.evaluate(request2, context), is(false));
}
Also used : Condition(com.hotels.styx.server.routing.Condition) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

Condition (com.hotels.styx.server.routing.Condition)22 Test (org.junit.jupiter.api.Test)22 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)19