use of io.apiman.test.common.mock.EchoResponse in project apiman-plugins by apiman.
the class SoapAuthorizationPolicyTest method testMultiple.
@Test
@Configuration("{\r\n" + " \"rules\" : [\r\n" + " { \"action\" : \"reportIncident\", \"role\" : \"user\" },\r\n" + " { \"action\" : \"resolveIncident\", \"role\" : \"admin\" }\r\n" + " ]\r\n" + "}")
public void testMultiple() throws Throwable {
HashSet<String> userRoles = new HashSet<>();
userRoles.add("user");
// Should Succeed
PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/path/to/user/resource");
request.header("SOAPAction", "reportIncident");
request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
PolicyTestResponse response = send(request);
EchoResponse echo = response.entity(EchoResponse.class);
Assert.assertNotNull(echo);
// Should Fail
request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/admin/path/to/admin/resource");
try {
request.header("SOAPAction", "resolveIncident");
request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
send(request);
Assert.fail("Expected a failure response!");
} catch (PolicyFailureError failure) {
PolicyFailure policyFailure = failure.getFailure();
Assert.assertNotNull(policyFailure);
Assert.assertEquals(PolicyFailureType.Authorization, policyFailure.getType());
}
//
userRoles.add("admin");
// Should Succeed
request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/path/to/user/resource");
request.header("SOAPAction", "reportIncident");
request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
response = send(request);
echo = response.entity(EchoResponse.class);
Assert.assertNotNull(echo);
// Should Succeed
request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/admin/path/to/admin/resource");
request.header("SOAPAction", "reportIncident");
request.contextAttribute(SoapAuthorizationPolicy.AUTHENTICATED_USER_ROLES, userRoles);
response = send(request);
echo = response.entity(EchoResponse.class);
Assert.assertNotNull(echo);
}
use of io.apiman.test.common.mock.EchoResponse in project apiman by apiman.
the class EchoBackEndApi method invoke.
/**
* @see io.apiman.test.policies.IPolicyTestBackEndApi#invoke(io.apiman.gateway.engine.beans.ApiRequest, byte[])
*/
@Override
public PolicyTestBackEndApiResponse invoke(ApiRequest request, byte[] requestBody) {
try {
EchoResponse echoResponse = new EchoResponse();
if (requestBody != null) {
echoResponse.setBodyLength(new Long(requestBody.length));
echoResponse.setBodySha1(DigestUtils.sha1Hex(requestBody));
}
echoResponse.setCounter(counter++);
echoResponse.setHeaders(request.getHeaders());
echoResponse.setMethod(request.getType());
echoResponse.setResource(request.getDestination());
echoResponse.setUri("urn:" + request.getDestination());
ApiResponse apiResponse = new ApiResponse();
String errorCode = request.getHeaders().get("X-Echo-ErrorCode");
if (errorCode != null) {
int ec = Integer.parseInt(errorCode);
String errorMsg = request.getHeaders().get("X-Echo-ErrorMessage");
apiResponse.setCode(ec);
apiResponse.setMessage(errorMsg);
} else {
apiResponse.setCode(200);
apiResponse.setMessage("OK");
}
apiResponse.getHeaders().put("Date", new Date().toString());
apiResponse.getHeaders().put("Server", "apiman.policy-test");
apiResponse.getHeaders().put("Content-Type", "application/json");
String responseBody = normalize(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(echoResponse));
apiResponse.getHeaders().put("Content-Length", String.valueOf(responseBody.length()));
PolicyTestBackEndApiResponse response = new PolicyTestBackEndApiResponse(apiResponse, responseBody);
return response;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of io.apiman.test.common.mock.EchoResponse 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.common.mock.EchoResponse 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.common.mock.EchoResponse 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"));
}
Aggregations