Search in sources :

Example 16 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman by apiman.

the class TransferQuotaPolicyTest method testBothLimit.

@Test
@Configuration("{" + "  \"limit\" : 500," + "  \"direction\" : \"both\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Day\"," + "  \"headerRemaining\" : \"X-Bytes-Remaining\"," + "  \"headerLimit\" : \"X-Bytes-Limit\"," + "  \"headerReset\" : \"X-Bytes-Reset\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testBothLimit() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.PUT, "/some/resource");
    request.header("X-Payload-Size", "50");
    byte[] data = new byte[50];
    Arrays.fill(data, (byte) 80);
    request.body(new String(data));
    PolicyTestResponse response = send(request);
    Assert.assertNotNull(response.body());
    Assert.assertEquals("450", response.header("X-Bytes-Remaining"));
    Assert.assertEquals("500", response.header("X-Bytes-Limit"));
    send(request);
    send(request);
    send(request);
    send(request);
    // Now if we try it one more time, we'll get a failure!
    try {
        send(request);
        Assert.fail("Expected a policy failure!");
    } catch (PolicyFailureError e) {
        PolicyFailure failure = e.getFailure();
        Assert.assertEquals(PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, failure.getFailureCode());
        Assert.assertEquals("Transfer quota exceeded.", failure.getMessage());
        Assert.assertEquals(429, failure.getResponseCode());
    }
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) IPolicyTestBackEndApi(io.apiman.test.policies.IPolicyTestBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 17 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman-plugins by apiman.

the class CircuitBreakerPolicyTest method testTripAndReset.

@Test
@Configuration("{" + "    \"errorCodes\" : [ \"5**\", \"4**\" ]," + "    \"window\" : 1," + "    \"limit\" : 5," + "    \"reset\" : 2," + "    \"failureCode\" : 503" + "}")
public void testTripAndReset() throws PolicyFailureError, Throwable {
    for (int iterations = 0; iterations < 2; iterations++) {
        PolicyTestRequest okRequest = PolicyTestRequest.build(PolicyTestRequestType.GET, "/path/to/resource");
        okRequest.header("Accept", "application/json");
        PolicyTestRequest errorRequest = PolicyTestRequest.build(PolicyTestRequestType.GET, "/path/to/resource");
        errorRequest.header("Accept", "application/json");
        errorRequest.header("X-Echo-ErrorCode", "500");
        PolicyTestResponse response;
        int numOK, numError;
        // Send a bunch of requests that return 200.  Everything should work fine.
        for (numOK = 0; numOK < 10; numOK++) {
            System.out.println(getClass().getName() + "::testTripAndReset:: OK request #" + (numOK + 1));
            response = send(okRequest);
            Assert.assertEquals(200, response.code());
        }
        // Now send three that will respond with errors.
        for (numError = 0; numError < 3; numError++) {
            System.out.println(getClass().getName() + "::testTripAndReset:: ERROR request #" + (numError + 1));
            response = send(errorRequest);
            Assert.assertEquals(500, response.code());
        }
        // The circuit was not yet tripped.  Try a couple of OK requests again.  Should work.
        for (; numOK < 12; numOK++) {
            System.out.println(getClass().getName() + "::testTripAndReset:: OK redux request #" + (numOK + 1));
            response = send(okRequest);
            Assert.assertEquals(200, response.code());
        }
        // Now send two more failures.  The second one (making 5 within the time window) should trip the circuit.
        for (; numError < 5; numError++) {
            System.out.println(getClass().getName() + "::testTripAndReset:: ERROR request #" + (numError + 1));
            response = send(errorRequest);
            Assert.assertEquals(500, response.code());
        }
        // Now that the circuit is tripped, the policy should fail with a 503
        try {
            response = send(okRequest);
        } catch (PolicyFailureError pf) {
            Assert.assertEquals(503, pf.getFailure().getResponseCode());
            Assert.assertEquals(20001, pf.getFailure().getFailureCode());
        }
        // Wait for the circuit reset time to elapse.
        Thread.sleep(2001);
        // Send a bunch more OK requests!  The circuit should now be reset.
        for (; numOK < 15; numOK++) {
            System.out.println(getClass().getName() + "::testTripAndReset:: OK request #" + (numOK + 1));
            response = send(okRequest);
            Assert.assertEquals(200, response.code());
        }
    }
}
Also used : PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 18 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman-plugins by apiman.

the class JWTPolicyTest method shouldFailWhenNoTokenProvided.

@Test
@Configuration("{\n" + "  \"requireJWT\": true,\n" + "  \"requireSigned\": false,\n" + "  \"requireTransportSecurity\": true,\n" + "  \"stripTokens\": true,\n" + "  \"signingKeyString\": \"" + PUBLIC_KEY_PEM + "\",\n" + "  \"allowedClockSkew\": 0,\n" + "  \"requiredClaims\": [{ \"claimName\": \"sub\", \"claimValue\": \"france frichot\" }]\n" + "}")
public void shouldFailWhenNoTokenProvided() throws Throwable {
    PolicyFailure failure = null;
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/amirante");
    try {
        send(request);
    } catch (PolicyFailureError pfe) {
        failure = pfe.getFailure();
    }
    Assert.assertNotNull(failure);
    Assert.assertEquals(401, failure.getResponseCode());
    Assert.assertEquals(12005, failure.getFailureCode());
    Assert.assertEquals(PolicyFailureType.Authentication, failure.getType());
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 19 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman-plugins by apiman.

the class JWTPolicyTest method shouldFailWhenTokenNotYetValid.

@Test
@Configuration("{\n" + "  \"requireJWT\": true,\n" + "  \"requireSigned\": false,\n" + "  \"requireTransportSecurity\": true,\n" + "  \"stripTokens\": true,\n" + "  \"signingKeyString\": \"" + PUBLIC_KEY_PEM + "\",\n" + "  \"allowedClockSkew\": 0,\n" + "  \"requiredClaims\": [{ \"claimName\": \"sub\", \"claimValue\": \"france frichot\" }]\n" + "}")
public void shouldFailWhenTokenNotYetValid() throws Throwable {
    PolicyFailure failure = null;
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/amirante").header("Authorization", "Bearer " + unsignedNotYetValidToken());
    try {
        send(request);
    } catch (PolicyFailureError pfe) {
        failure = pfe.getFailure();
    }
    Assert.assertNotNull(failure);
    Assert.assertEquals(401, failure.getResponseCode());
    Assert.assertEquals(12010, failure.getFailureCode());
    Assert.assertEquals(PolicyFailureType.Authentication, failure.getType());
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 20 with PolicyFailureError

use of io.apiman.test.policies.PolicyFailureError in project apiman-plugins by apiman.

the class JWTPolicyTest method shouldFailWhenTokenExpired.

@Test
@Configuration("{\n" + "  \"requireJWT\": true,\n" + "  \"requireSigned\": false,\n" + "  \"requireTransportSecurity\": true,\n" + "  \"stripTokens\": true,\n" + "  \"signingKeyString\": \"" + PUBLIC_KEY_PEM + "\",\n" + "  \"allowedClockSkew\": 0,\n" + "  \"requiredClaims\": [{ \"claimName\": \"sub\", \"claimValue\": \"france frichot\" }]\n" + "}")
public void shouldFailWhenTokenExpired() throws Throwable {
    PolicyFailure failure = null;
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/amirante").header("Authorization", "Bearer " + unsignedExpiredToken());
    try {
        send(request);
    } catch (PolicyFailureError pfe) {
        failure = pfe.getFailure();
    }
    Assert.assertNotNull(failure);
    Assert.assertEquals(401, failure.getResponseCode());
    Assert.assertEquals(12006, failure.getFailureCode());
    Assert.assertEquals(PolicyFailureType.Authentication, failure.getType());
}
Also used : PolicyFailure(io.apiman.gateway.engine.beans.PolicyFailure) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Aggregations

ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)24 Configuration (io.apiman.test.policies.Configuration)24 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)24 Test (org.junit.Test)24 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)21 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)19 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)15 EchoResponse (io.apiman.test.common.mock.EchoResponse)7 BackEndApi (io.apiman.test.policies.BackEndApi)7 IPolicyTestBackEndApi (io.apiman.test.policies.IPolicyTestBackEndApi)7 HashSet (java.util.HashSet)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3