Search in sources :

Example 6 with EchoResponse

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);
}
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) HashSet(java.util.HashSet) Configuration(io.apiman.test.policies.Configuration) ApimanPolicyTest(io.apiman.test.policies.ApimanPolicyTest) Test(org.junit.Test)

Example 7 with EchoResponse

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);
    }
}
Also used : EchoResponse(io.apiman.test.common.mock.EchoResponse) IOException(java.io.IOException) ApiResponse(io.apiman.gateway.engine.beans.ApiResponse) Date(java.util.Date)

Example 8 with EchoResponse

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);
}
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 9 with EchoResponse

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);
}
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 10 with EchoResponse

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

Aggregations

EchoResponse (io.apiman.test.common.mock.EchoResponse)25 ApimanPolicyTest (io.apiman.test.policies.ApimanPolicyTest)23 Configuration (io.apiman.test.policies.Configuration)23 PolicyTestResponse (io.apiman.test.policies.PolicyTestResponse)23 Test (org.junit.Test)23 PolicyTestRequest (io.apiman.test.policies.PolicyTestRequest)18 PolicyFailure (io.apiman.gateway.engine.beans.PolicyFailure)7 PolicyFailureError (io.apiman.test.policies.PolicyFailureError)7 HashSet (java.util.HashSet)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintStream (java.io.PrintStream)3 ApiResponse (io.apiman.gateway.engine.beans.ApiResponse)1 IOException (java.io.IOException)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 Date (java.util.Date)1