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());
}
}
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"));
}
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());
}
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());
}
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());
}
Aggregations