Search in sources :

Example 21 with Configuration

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

the class BasicAuthenticationPolicyTest method testBasicAuthNotRequired.

@Test
@Configuration("{\r\n" + "    \"realm\" : \"TestRealm\",\r\n" + "    \"requireBasicAuth\" : false,\r\n" + "    \"staticIdentity\" : {\r\n" + "      \"identities\" : [\r\n" + "        { \"username\" : \"ckent\", \"password\" : \"ckent123!\" },\r\n" + "        { \"username\" : \"bwayne\", \"password\" : \"bwayne123!\" },\r\n" + "        { \"username\" : \"dprince\", \"password\" : \"dprince123!\" }\r\n" + "      ]\r\n" + "    }\r\n" + "}")
public void testBasicAuthNotRequired() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    PolicyTestResponse response = send(request);
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    String header = echo.getHeaders().get("X-Authenticated-Identity");
    Assert.assertNull(header);
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 22 with Configuration

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

the class BasicAuthenticationPolicyTest method testStatic.

@Test
@Configuration("{\r\n" + "    \"realm\" : \"TestRealm\",\r\n" + "    \"forwardIdentityHttpHeader\" : \"X-Authenticated-Identity\",\r\n" + "    \"staticIdentity\" : {\r\n" + "      \"identities\" : [\r\n" + "        { \"username\" : \"ckent\", \"password\" : \"ckent123!\" },\r\n" + "        { \"username\" : \"bwayne\", \"password\" : \"bwayne123!\" },\r\n" + "        { \"username\" : \"dprince\", \"password\" : \"dprince123!\" }\r\n" + "      ]\r\n" + "    }\r\n" + "}")
public void testStatic() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    // Failure
    try {
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authentication, policyFailure.getType());
        Assert.assertEquals(10004, policyFailure.getFailureCode());
    }
    // Failure
    try {
        request.basicAuth("ckent", "invalid_password");
        send(request);
        Assert.fail("Expected a failure response!");
    } catch (PolicyFailureError failure) {
        PolicyFailure policyFailure = failure.getFailure();
        Assert.assertNotNull(policyFailure);
        Assert.assertEquals(PolicyFailureType.Authentication, policyFailure.getType());
        Assert.assertEquals(10003, policyFailure.getFailureCode());
    }
    // Success
    request.basicAuth("ckent", "ckent123!");
    PolicyTestResponse response = send(request);
    Assert.assertEquals(200, response.code());
    EchoResponse echo = response.entity(EchoResponse.class);
    Assert.assertNotNull(echo);
    String header = echo.getHeaders().get("X-Authenticated-Identity");
    Assert.assertNotNull(header);
    Assert.assertEquals("ckent", header);
}
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 23 with Configuration

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

the class CachingPolicyTest method testCaching.

@Test
@Configuration("{" + "  \"ttl\" : 2" + "}")
public void testCaching() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/cached-resource");
    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"));
    // Now send the request again - we should get the *same* counter value!
    response = send(request);
    echo = response.entity(EchoResponse.class);
    assertNotNull(echo);
    Long counterValue2 = echo.getCounter();
    assertNotNull(counterValue2);
    assertEquals(counterValue, counterValue2);
    assertEquals("application/json", response.header("Content-Type"));
    // One more time, just to be sure
    response = send(request);
    echo = response.entity(EchoResponse.class);
    assertNotNull(echo);
    Long counterValue3 = echo.getCounter();
    assertNotNull(counterValue3);
    assertEquals(counterValue, counterValue3);
    assertEquals("application/json", response.header("Content-Type"));
    // Now wait for 3s and make sure the cache entry expired
    Thread.sleep(3000);
    response = send(request);
    echo = response.entity(EchoResponse.class);
    assertNotNull(echo);
    Long counterValue4 = echo.getCounter();
    assertNotNull(counterValue4);
    assertNotEquals(counterValue, counterValue4);
    assertEquals("application/json", response.header("Content-Type"));
    // And again - should be re-cached
    response = send(request);
    echo = response.entity(EchoResponse.class);
    assertNotNull(echo);
    Long counterValue5 = echo.getCounter();
    assertNotNull(counterValue5);
    assertEquals(counterValue4, counterValue5);
    assertEquals("application/json", response.header("Content-Type"));
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 24 with Configuration

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

the class TransferQuotaPolicyTest method testDownloadLimitNoHeaderConfig.

@Test
@Configuration("{" + "  \"limit\" : 1000," + "  \"direction\" : \"download\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Minute\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimitNoHeaderConfig() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    request.header("X-Payload-Size", "389");
    PolicyTestResponse response = send(request);
    Assert.assertNotNull(response.body());
    Assert.assertEquals("1000", response.header("X-TransferQuota-Remaining"));
    Assert.assertEquals("1000", response.header("X-TransferQuota-Limit"));
    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 25 with Configuration

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

the class TransferQuotaPolicyTest method testDownloadLimit.

@Test
@Configuration("{" + "  \"limit\" : 1000," + "  \"direction\" : \"download\"," + "  \"granularity\" : \"Api\"," + "  \"period\" : \"Day\"," + "  \"headerRemaining\" : \"X-DBytes-Remaining\"," + "  \"headerLimit\" : \"X-DBytes-Limit\"," + "  \"headerReset\" : \"X-DBytes-Reset\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimit() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    request.header("X-Payload-Size", "389");
    PolicyTestResponse response = send(request);
    Assert.assertNotNull(response.body());
    Assert.assertEquals("1000", response.header("X-DBytes-Remaining"));
    Assert.assertEquals("1000", response.header("X-DBytes-Limit"));
    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)

Aggregations

ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)51 Configuration (io.apiman.test.policies.Configuration)51 Test (org.junit.Test)51 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)40 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)39 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)24 EchoResponse (io.apiman.test.common.mock.EchoResponse)23 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)19 BackEndApi (io.apiman.test.policies.BackEndApi)15 ConsumeJsonBackEndApi (io.apiman.plugins.transformation_policy.backend.ConsumeJsonBackEndApi)8 ConsumeXmlBackEndApi (io.apiman.plugins.transformation_policy.backend.ConsumeXmlBackEndApi)8 ProduceComplexJsonBackEndApi (io.apiman.plugins.transformation_policy.backend.ProduceComplexJsonBackEndApi)8 ProduceJsonBackEndApi (io.apiman.plugins.transformation_policy.backend.ProduceJsonBackEndApi)8 ProduceXmlBackEndApi (io.apiman.plugins.transformation_policy.backend.ProduceXmlBackEndApi)8 HashSet (java.util.HashSet)8 IPolicyTestBackEndApi (io.apiman.test.policies.IPolicyTestBackEndApi)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 PrintStream (java.io.PrintStream)4 TimeoutConfigBean (io.apiman.plugins.timeoutpolicy.beans.TimeoutConfigBean)1