Search in sources :

Example 91 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class BaseRequestTest method alwaysCloseResponseOnAPIException.

@Test
public void alwaysCloseResponseOnAPIException() {
    Exception exception = null;
    try {
        new MockBaseRequest<String>(client) {

            @Override
            protected String parseResponse(Response response) throws Auth0Exception {
                throw new APIException("APIException", 500, null);
            }
        }.execute();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(APIException.class)));
    assertThat(exception.getMessage(), is("Request failed with status code 500: APIException"));
    verify(response, times(1)).close();
}
Also used : APIException(com.auth0.exception.APIException) Auth0Exception(com.auth0.exception.Auth0Exception) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) Test(org.junit.Test)

Example 92 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class CreateUserRequestTest method setUp.

@Before
public void setUp() throws Exception {
    client = new OkHttpClient();
    server = new MockServer();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) MockServer(com.auth0.client.MockServer) Before(org.junit.Before)

Example 93 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class CreateUserRequestTest method shouldCreatePOSTRequest.

@Test
public void shouldCreatePOSTRequest() throws Exception {
    CreateUserRequest request = new CreateUserRequest(client, server.getBaseUrl());
    assertThat(request, is(notNullValue()));
    request.addParameter("non_empty", "body");
    server.jsonResponse(AUTH_SIGN_UP, 200);
    CreatedUser execute = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod(), is("POST"));
    assertThat(execute, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) CreatedUser(com.auth0.json.auth.CreatedUser) Test(org.junit.Test)

Example 94 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class CustomRequestTest method shouldParseJSONErrorResponseWithError.

@Test
public void shouldParseJSONErrorResponseWithError() throws Exception {
    CustomRequest<List> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType);
    server.jsonResponse(AUTH_ERROR_WITH_ERROR, 400);
    Exception exception = null;
    try {
        request.execute();
        server.takeRequest();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(APIException.class)));
    assertThat(exception.getCause(), is(nullValue()));
    assertThat(exception.getMessage(), is("Request failed with status code 400: missing username for Username-Password-Authentication connection with requires_username enabled"));
    APIException authException = (APIException) exception;
    assertThat(authException.getDescription(), is("missing username for Username-Password-Authentication connection with requires_username enabled"));
    assertThat(authException.getError(), is("missing username for Username-Password-Authentication connection with requires_username enabled"));
    assertThat(authException.getStatusCode(), is(400));
}
Also used : APIException(com.auth0.exception.APIException) List(java.util.List) Auth0Exception(com.auth0.exception.Auth0Exception) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ExpectedException(org.junit.rules.ExpectedException)

Example 95 with Client

use of com.auth0.json.mgmt.client.Client in project auth0-java by auth0.

the class CustomRequestTest method shouldDefaultRateLimitsHeadersWhenMissing.

@Test
public void shouldDefaultRateLimitsHeadersWhenMissing() {
    CustomRequest<List> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType);
    server.rateLimitReachedResponse(-1, -1, -1);
    Exception exception = null;
    try {
        request.execute();
        server.takeRequest();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(RateLimitException.class)));
    assertThat(exception.getCause(), is(nullValue()));
    assertThat(exception.getMessage(), is("Request failed with status code 429: Rate limit reached"));
    RateLimitException rateLimitException = (RateLimitException) exception;
    assertThat(rateLimitException.getDescription(), is("Rate limit reached"));
    assertThat(rateLimitException.getError(), is(nullValue()));
    assertThat(rateLimitException.getValue("non_existing_key"), is(nullValue()));
    assertThat(rateLimitException.getStatusCode(), is(429));
    assertThat(rateLimitException.getLimit(), is(-1L));
    assertThat(rateLimitException.getRemaining(), is(-1L));
    assertThat(rateLimitException.getReset(), is(-1L));
}
Also used : RateLimitException(com.auth0.exception.RateLimitException) List(java.util.List) Auth0Exception(com.auth0.exception.Auth0Exception) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ExpectedException(org.junit.rules.ExpectedException)

Aggregations

IOException (java.io.IOException)36 APIException (com.auth0.exception.APIException)27 Auth0Exception (com.auth0.exception.Auth0Exception)27 RateLimitException (com.auth0.exception.RateLimitException)27 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)26 List (java.util.List)25 Test (org.junit.Test)25 VoidRequest (com.auth0.net.VoidRequest)24 TokenHolder (com.auth0.json.auth.TokenHolder)22 JsonParseException (com.fasterxml.jackson.core.JsonParseException)19 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)19 ExpectedException (org.junit.rules.ExpectedException)19 RecordedMultipartRequest (com.auth0.net.multipart.RecordedMultipartRequest)16 Test (org.junit.jupiter.api.Test)14 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)14 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9 AuthAPI (com.auth0.client.auth.AuthAPI)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 OkHttpClient (okhttp3.OkHttpClient)7