Search in sources :

Example 6 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class AuthenticationServiceV2Test method shouldReturnResponseContainingUnauthorizedCodeWithJsonErrorMessage.

@Test
public void shouldReturnResponseContainingUnauthorizedCodeWithJsonErrorMessage() throws IOException {
    // given
    Request httpRequest = new Request();
    RestAuthException testException = new RestAuthException(401, "Invalid Password!!");
    testException.setFailureUrl("http://localhost:8080");
    // when
    Response response = authServiceV2.handleErrorResponse(httpRequest, Status.valueOf(401), testException);
    // then
    assertThat(response.getStatus()).isEqualToComparingFieldByField(Status.UNAUTHORIZED);
    JsonValue responseBody = json(response.getEntity().getJson());
    assertThat(responseBody).integerAt("code").isEqualTo(401);
    assertThat(responseBody).stringAt("reason").isEqualTo("Unauthorized");
    assertThat(responseBody).stringAt("message").isEqualTo("Invalid Password!!");
    assertThat(responseBody).stringAt("detail/failureUrl").isEqualTo("http://localhost:8080");
}
Also used : RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) Response(org.forgerock.http.protocol.Response) Request(org.forgerock.http.protocol.Request) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 7 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class AuthenticationServiceV2Test method shouldReturnUnauthorizedCodeWithJsonFailureMessage.

@Test
public void shouldReturnUnauthorizedCodeWithJsonFailureMessage() throws IOException {
    // given
    Request httpRequest = new Request();
    JsonValue jsonValue = json(object(field("failure", true), field("reason", "http-auth-failed"), field("authId", "12345")));
    RestAuthResponseException exception = new RestAuthResponseException(RestAuthException.UNAUTHORIZED, Collections.<String, String>emptyMap(), jsonValue);
    // when
    Response response = authServiceV2.handleErrorResponse(httpRequest, Status.valueOf(exception.getStatusCode()), exception);
    // then
    assertThat(response.getStatus()).isEqualToComparingFieldByField(Status.UNAUTHORIZED);
    JsonValue responseBody = json(response.getEntity().getJson());
    assertThat(responseBody).booleanAt("failure").isTrue();
    assertThat(responseBody).stringAt("reason").isEqualTo("http-auth-failed");
    assertThat(responseBody).stringAt("authId").isEqualTo("12345");
}
Also used : Response(org.forgerock.http.protocol.Response) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) Request(org.forgerock.http.protocol.Request) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 8 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class AuthenticationServiceV2Test method shouldFailAuthenticationWithUnsupportedMediaTypeMessage.

@Test
public void shouldFailAuthenticationWithUnsupportedMediaTypeMessage() throws IOException {
    // given
    AttributesContext context = new AttributesContext(new SessionContext(new RootContext(), mock(Session.class)));
    Request httpRequest = new Request();
    httpRequest.setEntity("<xml></xml>");
    httpRequest.getHeaders().put(ContentTypeHeader.NAME, "application/xml");
    // when
    Response response = authServiceV2.authenticate(context, httpRequest);
    // then
    assertThat(response.getStatus()).isEqualTo(Status.UNSUPPORTED_MEDIA_TYPE);
    JsonValue responseBody = json(response.getEntity().getJson());
    assertThat(responseBody).integerAt("code").isEqualTo(415);
    assertThat(responseBody).stringAt("reason").isEqualTo("Unsupported Media Type");
    assertThat(responseBody).stringAt("message").isEqualTo("Unsupported Media Type");
}
Also used : RootContext(org.forgerock.services.context.RootContext) Response(org.forgerock.http.protocol.Response) AttributesContext(org.forgerock.services.context.AttributesContext) Request(org.forgerock.http.protocol.Request) JsonValue(org.forgerock.json.JsonValue) SessionContext(org.forgerock.http.session.SessionContext) Test(org.testng.annotations.Test)

Example 9 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class AuthenticationServiceV1Test method shouldReturnUnauthorizedCodeWithJsonFailureMessage.

@Test
public void shouldReturnUnauthorizedCodeWithJsonFailureMessage() throws IOException {
    // given
    Request httpRequest = new Request();
    JsonValue jsonValue = json(object(field("failure", true), field("reason", "http-auth-failed"), field("authId", "12345")));
    RestAuthResponseException exception = new RestAuthResponseException(RestAuthException.UNAUTHORIZED, Collections.<String, String>emptyMap(), jsonValue);
    // when
    Response response = authServiceV1.handleErrorResponse(httpRequest, Status.valueOf(exception.getStatusCode()), exception);
    // then
    assertThat(response.getStatus()).isEqualToComparingFieldByField(Status.UNAUTHORIZED);
    JsonValue responseBody = json(response.getEntity().getJson());
    assertThat(responseBody).booleanAt("failure").isTrue();
    assertThat(responseBody).stringAt("reason").isEqualTo("http-auth-failed");
    assertThat(responseBody).stringAt("authId").isEqualTo("12345");
}
Also used : Response(org.forgerock.http.protocol.Response) RestAuthResponseException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException) Request(org.forgerock.http.protocol.Request) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Example 10 with Response

use of org.forgerock.http.protocol.Response in project OpenAM by OpenRock.

the class AuthenticationServiceV1Test method shouldFailAuthenticationWithUnsupportedMediaTypeMessage.

@Test
public void shouldFailAuthenticationWithUnsupportedMediaTypeMessage() throws IOException {
    // given
    AttributesContext context = new AttributesContext(new SessionContext(new RootContext(), mock(Session.class)));
    Request httpRequest = new Request();
    httpRequest.setEntity("<xml></xml>");
    httpRequest.getHeaders().put(ContentTypeHeader.NAME, "application/xml");
    // when
    Response response = authServiceV1.authenticate(context, httpRequest);
    // then
    assertThat(response.getStatus()).isEqualTo(Status.UNSUPPORTED_MEDIA_TYPE);
    assertThat(json(response.getEntity().getJson())).stringAt("errorMessage").isEqualTo("Unsupported Media Type");
}
Also used : RootContext(org.forgerock.services.context.RootContext) Response(org.forgerock.http.protocol.Response) AttributesContext(org.forgerock.services.context.AttributesContext) Request(org.forgerock.http.protocol.Request) SessionContext(org.forgerock.http.session.SessionContext) Test(org.testng.annotations.Test)

Aggregations

Response (org.forgerock.http.protocol.Response)22 Request (org.forgerock.http.protocol.Request)15 Test (org.testng.annotations.Test)14 Context (org.forgerock.services.context.Context)8 JsonValue (org.forgerock.json.JsonValue)6 AttributesContext (org.forgerock.services.context.AttributesContext)6 RootContext (org.forgerock.services.context.RootContext)6 Handler (org.forgerock.http.Handler)4 ReadRequest (org.forgerock.json.resource.ReadRequest)4 ResourceResponse (org.forgerock.json.resource.ResourceResponse)4 RestAuthException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthException)4 RestAuthResponseException (org.forgerock.openam.core.rest.authn.exceptions.RestAuthResponseException)4 NeverThrowsException (org.forgerock.util.promise.NeverThrowsException)4 Map (java.util.Map)3 UriRouterContext (org.forgerock.http.routing.UriRouterContext)3 SessionContext (org.forgerock.http.session.SessionContext)3 ActionRequest (org.forgerock.json.resource.ActionRequest)3 ActionResponse (org.forgerock.json.resource.ActionResponse)3 CreateRequest (org.forgerock.json.resource.CreateRequest)3 DeleteRequest (org.forgerock.json.resource.DeleteRequest)3