Search in sources :

Example 21 with Request

use of org.forgerock.http.protocol.Request 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 22 with Request

use of org.forgerock.http.protocol.Request 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 23 with Request

use of org.forgerock.http.protocol.Request 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 24 with Request

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

the class AuthenticationServiceV1Test method shouldReturnFrenchErrorMessageFromCause.

@Test
public void shouldReturnFrenchErrorMessageFromCause() throws IOException {
    // given
    Request httpRequest = new Request();
    AuthLoginException ale = new AuthLoginException("amAuth", "120", null);
    RestAuthException exception = new RestAuthException(401, ale);
    httpRequest.getHeaders().put("Accept-Language", "fr-fr");
    // when
    String message = authServiceV1.getLocalizedMessage(httpRequest, exception);
    // then
    assertThat(message).isEqualTo("L’authentification sur module n’est pas autorisée.");
}
Also used : RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) Request(org.forgerock.http.protocol.Request) AuthLoginException(com.sun.identity.authentication.spi.AuthLoginException) Test(org.testng.annotations.Test)

Example 25 with Request

use of org.forgerock.http.protocol.Request 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

Request (org.forgerock.http.protocol.Request)51 Test (org.testng.annotations.Test)45 Context (org.forgerock.services.context.Context)36 RootContext (org.forgerock.services.context.RootContext)33 AttributesContext (org.forgerock.services.context.AttributesContext)30 ReadRequest (org.forgerock.json.resource.ReadRequest)29 ActionRequest (org.forgerock.json.resource.ActionRequest)21 CreateRequest (org.forgerock.json.resource.CreateRequest)21 DeleteRequest (org.forgerock.json.resource.DeleteRequest)21 PatchRequest (org.forgerock.json.resource.PatchRequest)21 QueryRequest (org.forgerock.json.resource.QueryRequest)21 UpdateRequest (org.forgerock.json.resource.UpdateRequest)21 UriRouterContext (org.forgerock.http.routing.UriRouterContext)19 Response (org.forgerock.http.protocol.Response)14 SessionContext (org.forgerock.http.session.SessionContext)11 Handler (org.forgerock.http.Handler)10 RequestAuditContext (org.forgerock.services.context.RequestAuditContext)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HttpContext (org.forgerock.json.resource.http.HttpContext)7 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)7