Search in sources :

Example 66 with Request

use of org.restlet.Request in project OpenAM by OpenRock.

the class ResourceSetRegistrationExceptionFilterTest method shouldSet412ExceptionResponse.

@Test
@SuppressWarnings("unchecked")
public void shouldSet412ExceptionResponse() throws Exception {
    //Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Status status = new Status(412);
    given(response.getStatus()).willReturn(status);
    //When
    exceptionFilter.afterHandle(request, response);
    //Then
    ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class);
    verify(response).setEntity(exceptionResponseCaptor.capture());
    Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
    assertThat(responseBody).containsOnly(entry("error", "precondition_failed"));
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Request(org.restlet.Request) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 67 with Request

use of org.restlet.Request in project OpenAM by OpenRock.

the class ResourceSetRegistrationExceptionFilterTest method shouldSetAnyOtherExceptionResponse.

@Test
@SuppressWarnings("unchecked")
public void shouldSetAnyOtherExceptionResponse() throws Exception {
    //Given
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Exception exception = new Exception("MESSAGE");
    Status status = new Status(444, exception);
    given(response.getStatus()).willReturn(status);
    //When
    exceptionFilter.afterHandle(request, response);
    //Then
    ArgumentCaptor<JacksonRepresentation> exceptionResponseCaptor = ArgumentCaptor.forClass(JacksonRepresentation.class);
    verify(response).setEntity(exceptionResponseCaptor.capture());
    Map<String, String> responseBody = (Map<String, String>) exceptionResponseCaptor.getValue().getObject();
    assertThat(responseBody).containsOnly(entry("error", "server_error"), entry("error_description", "MESSAGE"));
    ArgumentCaptor<Status> statusCaptor = ArgumentCaptor.forClass(Status.class);
    verify(response).setStatus(statusCaptor.capture());
    assertThat(statusCaptor.getValue().getCode()).isEqualTo(500);
    assertThat(statusCaptor.getValue().getThrowable()).isEqualTo(exception);
}
Also used : Response(org.restlet.Response) Status(org.restlet.data.Status) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) Request(org.restlet.Request) Map(java.util.Map) BadRequestException(org.forgerock.oauth2.core.exceptions.BadRequestException) Test(org.testng.annotations.Test)

Example 68 with Request

use of org.restlet.Request in project OpenAM by OpenRock.

the class RestletFormBodyAccessTokenVerifierTest method shouldLookupValue.

@Test
public void shouldLookupValue() throws Exception {
    // Given
    Form form = new Form();
    form.add("access_token", "freddy");
    Request request = new Request();
    request.setEntity(form.getWebRepresentation());
    OAuth2Request req = new RestletOAuth2Request(null, request);
    // When
    AccessTokenVerifier.TokenState result = verifier.verify(req);
    // Then
    assertThat(result.isValid()).isFalse();
    verify(tokenStore).readAccessToken(req, "freddy");
}
Also used : OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) Form(org.restlet.data.Form) Request(org.restlet.Request) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier) Test(org.testng.annotations.Test)

Example 69 with Request

use of org.restlet.Request in project camel by apache.

the class RestletProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    RestletEndpoint endpoint = (RestletEndpoint) getEndpoint();
    final RestletBinding binding = endpoint.getRestletBinding();
    Request request;
    String resourceUri = buildUri(endpoint, exchange);
    URI uri = new URI(resourceUri);
    request = new Request(endpoint.getRestletMethod(), resourceUri);
    binding.populateRestletRequestFromExchange(request, exchange);
    loadCookies(exchange, uri, request);
    LOG.debug("Sending request synchronously: {} for exchangeId: {}", request, exchange.getExchangeId());
    Response response = client.handle(request);
    LOG.debug("Received response synchronously: {} for exchangeId: {}", response, exchange.getExchangeId());
    if (response != null) {
        Integer respCode = response.getStatus().getCode();
        storeCookies(exchange, uri, response);
        if (respCode > 207 && throwException) {
            exchange.setException(populateRestletProducerException(exchange, response, respCode));
        } else {
            binding.populateExchangeFromRestletResponse(exchange, response);
        }
    }
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) URI(java.net.URI)

Example 70 with Request

use of org.restlet.Request in project camel by apache.

the class RestletRouteBuilderTest method testNotFound.

@Test
public void testNotFound() throws IOException {
    Client client = new Client(Protocol.HTTP);
    Response response = client.handle(new Request(Method.POST, "http://localhost:" + portNum + "/unknown"));
    // expect error status as no Restlet consumer to handle POST method
    assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response.getStatus());
    assertNotNull(response.getEntity().getText());
}
Also used : Response(org.restlet.Response) Request(org.restlet.Request) Client(org.restlet.Client) Test(org.junit.Test)

Aggregations

Request (org.restlet.Request)79 Response (org.restlet.Response)44 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)37 Test (org.testng.annotations.Test)36 ChallengeResponse (org.restlet.data.ChallengeResponse)18 Status (org.restlet.data.Status)15 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 AccessToken (org.forgerock.oauth2.core.AccessToken)11 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)11 HttpRequest (org.restlet.engine.adapter.HttpRequest)9 Representation (org.restlet.representation.Representation)9 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)8 Form (org.restlet.data.Form)8 Reference (org.restlet.data.Reference)8 BeforeMethod (org.testng.annotations.BeforeMethod)8 Map (java.util.Map)7 OAuth2ProviderSettingsFactory (org.forgerock.oauth2.core.OAuth2ProviderSettingsFactory)6 Test (org.junit.Test)6 Client (org.restlet.Client)6 URI (java.net.URI)4