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