Search in sources :

Example 11 with BackEndApi

use of io.apiman.test.policies.BackEndApi 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 12 with BackEndApi

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

the class TimeoutPolicyTest method shouldsettingNothing_onEmptyValue.

/**
 * Execute with empty value
 */
@Test
@Configuration("{\"timeoutConnect\" : \"\", \"timeoutRead\" : \"\" }")
@BackEndApi(EndPointPropertiesEcho.class)
public void shouldsettingNothing_onEmptyValue() throws PolicyFailureError, Throwable {
    // WHEN Execute the policy
    PolicyTestResponse response = null;
    try {
        response = send(PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource"));
    } catch (PolicyFailureError ex) {
        fail("Configuration error");
    }
    // THEN timeouts are not set
    HashMap<?, ?> responseMap = new ObjectMapper().readValue(response.body(), HashMap.class);
    assertFalse("timeoutConnect", responseMap.containsKey("timeouts.connect"));
    assertFalse("timeoutRead", responseMap.containsKey("timeouts.read"));
}
Also used : PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) PolicyFailureError(io.apiman.test.policies.PolicyFailureError) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IPolicyTestBackEndApi(io.apiman.test.policies.IPolicyTestBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) Configuration(io.apiman.test.policies.Configuration) Test(org.junit.Test) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest)

Example 13 with BackEndApi

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

the class TransformationPolicyTest method transformServerXmlResponseToJson.

@Test
@Configuration("{\"clientFormat\": \"JSON\", \"serverFormat\": \"XML\"}")
@BackEndApi(ProduceXmlBackEndApi.class)
public void transformServerXmlResponseToJson() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    PolicyTestResponse response = send(request);
    String expectedResponse = "{\"name\":\"apiman\"}";
    assertEquals("application/json", response.header("Content-Type"));
    assertNull(response.header("Content-Length"));
    assertEquals(expectedResponse, response.body());
}
Also used : PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) ProduceXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceXmlBackEndApi) ProduceJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceJsonBackEndApi) ProduceComplexJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceComplexJsonBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) ConsumeJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeJsonBackEndApi) ConsumeXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeXmlBackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 14 with BackEndApi

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

the class TransformationPolicyTest method transformServerJsonResponseToXml.

@Test
@Configuration("{\"clientFormat\": \"XML\", \"serverFormat\": \"JSON\"}")
@BackEndApi(ProduceJsonBackEndApi.class)
public void transformServerJsonResponseToXml() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    PolicyTestResponse response = send(request);
    String expectedResponse = "<name>apiman</name>";
    assertEquals("application/xml", response.header("Content-Type"));
    assertNull(response.header("Content-Length"));
    assertEquals(expectedResponse, response.body());
}
Also used : PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) ProduceXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceXmlBackEndApi) ProduceJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceJsonBackEndApi) ProduceComplexJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceComplexJsonBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) ConsumeJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeJsonBackEndApi) ConsumeXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeXmlBackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 15 with BackEndApi

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

the class TransformationPolicyTest method keepServerJsonResponseAsJson.

@Test
@Configuration("{\"clientFormat\": \"JSON\", \"serverFormat\": \"JSON\"}")
@BackEndApi(ProduceJsonBackEndApi.class)
public void keepServerJsonResponseAsJson() throws Throwable {
    PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
    PolicyTestResponse response = send(request);
    String expectedResponse = "{\"name\":\"apiman\"}";
    assertEquals("application/json", response.header("Content-Type"));
    assertEquals(String.valueOf(expectedResponse.getBytes("UTF-8").length), response.header("Content-Length"));
    assertEquals(expectedResponse, response.body());
}
Also used : PolicyTestRequest(io.apiman.test.policies.PolicyTestRequest) PolicyTestResponse(io.apiman.test.policies.PolicyTestResponse) ProduceXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceXmlBackEndApi) ProduceJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceJsonBackEndApi) ProduceComplexJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ProduceComplexJsonBackEndApi) BackEndApi(io.apiman.test.policies.BackEndApi) ConsumeJsonBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeJsonBackEndApi) ConsumeXmlBackEndApi(io.apiman.plugins.transformation_policy.backend.ConsumeXmlBackEndApi) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Aggregations

ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)15 BackEndApi (io.apiman.test.policies.BackEndApi)15 Configuration (io.apiman.test.policies.Configuration)15 Test (org.junit.Test)15 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)13 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)12 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 IPolicyTestBackEndApi (io.apiman.test.policies.IPolicyTestBackEndApi)7 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)7 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3