Search in sources :

Example 11 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse in project apiman by apiman.

the class TransferQuotaPolicyTest method testUploadLimit.

@Test
@Configuration("{" + "  \"limit\" : 100," + "  \"direction\" : \"upload\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Day\"," + "  \"headerRemaining\" : \"X-Bytes-Remaining\"," + "  \"headerLimit\" : \"X-Bytes-Limit\"," + "  \"headerReset\" : \"X-Bytes-Reset\"" + "}")
public void testUploadLimit() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.POST, "/some/resource");
    request.body("0123456789");
    PolicyTestResponse response = send(request);
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    Assert.assertEquals("90", response.header("X-Bytes-Remaining"));
    Assert.assertEquals("100", response.header("X-Bytes-Limit"));
    // Now try sending a few more times to get closer to the limit
    for (int i = 0; i < 9; i++) {
        response = send(request);
    }
    echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    Assert.assertEquals("0", response.header("X-Bytes-Remaining"));
    Assert.assertEquals("100", response.header("X-Bytes-Limit"));
    // 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 : EchoResponse(io.apiman.test.common.mock.EchoResponse) 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) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 12 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse in project apiman by apiman.

the class TransferQuotaPolicyTest method testLargeUploadLimit.

@Test
@Configuration("{" + "  \"limit\" : 10485760," + "  \"direction\" : \"upload\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Day\"," + "  \"headerRemaining\" : \"X-Data-Remaining\"," + "  \"headerLimit\" : \"X-Data-Limit\"," + "  \"headerReset\" : \"X-Data-Reset\"" + "}")
public void testLargeUploadLimit() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.POST, "/some/large-resource");
    // The 4th of these should exceed our limits
    byte[] data = new byte[11000000 / 4];
    Arrays.fill(data, (byte) 80);
    request.body(new String(data));
    PolicyTestResponse response = send(request);
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    Assert.assertEquals("7735760", response.header("X-Data-Remaining"));
    Assert.assertEquals("10485760", response.header("X-Data-Limit"));
    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());
        String remaining = failure.getHeaders().get("X-Data-Remaining");
        Assert.assertEquals("-514240", remaining);
    }
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) 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) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 13 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse in project apiman by apiman.

the class CachingPolicyTest method testCachingUsingQueryString.

/**
 * Verify that the query string is used as part of the cache key - expect
 * that requests with different query strings are treated as different, from
 * a caching perspective.
 */
@Test
@Configuration("{" + "  \"ttl\" : 2," + "  \"includeQueryInKey\" : true" + "}")
public void testCachingUsingQueryString() throws Throwable {
    final String originalUri = "/some/cached-resource?foo=bar";
    PolicyTestResponse response = send(PolicyTestRequest.build(PolicyTestRequestType.GET, originalUri));
    EchoResponse echo = response.entity(EchoResponse.class);
    assertNotNull(echo);
    Long counterValue = echo.getCounter();
    assertNotNull(counterValue);
    assertEquals("application/json", response.header("Content-Type"));
    assertEquals(200, response.code());
    // Request with a different query string - expect an uncached response
    response = send(PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/cached-resource?foo=different"));
    echo = response.entity(EchoResponse.class);
    assertNotNull(echo);
    Long counterValue2 = echo.getCounter();
    assertNotNull(counterValue2);
    assertNotEquals(counterValue, counterValue2);
    assertEquals("application/json", response.header("Content-Type"));
    assertEquals(200, response.code());
    // Request the original URI (including query string) - expect a cached response
    response = send(PolicyTestRequest.build(PolicyTestRequestType.GET, originalUri));
    echo = response.entity(EchoResponse.class);
    assertNotNull(echo);
    Long counterValue3 = echo.getCounter();
    assertNotNull(counterValue3);
    assertEquals(counterValue, counterValue3);
    assertEquals("application/json", response.header("Content-Type"));
    assertEquals(200, response.code());
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 14 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse in project apiman by apiman.

the class CachingResourcesPolicyTest method doRequest.

/**
 * Simulate a request
 * @param request
 * @return counterValue as result of the request
 * @throws Throwable
 */
private Long doRequest(PolicyTestRequest request) throws Throwable {
    PolicyTestResponse response = send(request);
    EchoResponse echo = response.entity(EchoResponse.class);
    assertNotNull(echo);
    Long counterValue = echo.getCounter();
    assertNotNull(counterValue);
    assertEquals("application/json", response.header("Content-Type"));
    return counterValue;
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse)

Example 15 with EchoResponse

use of io.apiman.test.common.mock.EchoResponse in project apiman-plugins by apiman.

the class LogHeadersPolicyTest method testLogHeadersHappyFlowRequestOnly.

/**
 * A simple happy flow test to verify the policy does not blow up in our face.
 */
@Test
@Configuration("{ \"direction\" : \"request\" }")
public void testLogHeadersHappyFlowRequestOnly() throws PolicyFailureError, Throwable {
    PrintStream out = System.out;
    ByteArrayOutputStream testOutput = new ByteArrayOutputStream();
    System.setOut(new PrintStream(testOutput));
    try {
        PolicyTestResponse response = send(PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource").header("X-Test-Name", "testGet"));
        Assert.assertEquals(200, response.code());
        EchoResponse entity = response.entity(EchoResponse.class);
        Assert.assertEquals("testGet", entity.getHeaders().get("X-Test-Name"));
        String output = testOutput.toString("UTF-8");
        output = redactDates(output);
        output = normalize(output);
        String expected = "INFO: Logging 1 HTTP Request headers for io.apiman.test.policies.EchoBackEndApi\n" + "Key : X-Test-Name, Value : testGet\n" + "";
        Assert.assertEquals(expected, output);
    } finally {
        System.setOut(out);
    }
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PrintStream(java.io.PrintStream) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Aggregations

EchoResponse (io.apiman.test.common.mock.EchoResponse)25 ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)23 Configuration (io.apiman.test.policies.Configuration)23 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)23 Test (org.junit.Test)23 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)18 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)7 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)7 HashSet (java.util.HashSet)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintStream (java.io.PrintStream)3 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)1 IOException (java.io.IOException)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 Date (java.util.Date)1