Search in sources :

Example 31 with Client

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

the class CustomRequestTest method shouldParseJSONErrorResponseWithDescription.

@Test
public void shouldParseJSONErrorResponseWithDescription() throws Exception {
    CustomRequest<List> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType);
    server.jsonResponse(AUTH_ERROR_WITH_DESCRIPTION, 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: The user already exists."));
    APIException authException = (APIException) exception;
    assertThat(authException.getDescription(), is("The user already exists."));
    assertThat(authException.getError(), is("user_exists"));
    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 32 with Client

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

the class CustomRequestTest method shouldAddHeaders.

@Test
public void shouldAddHeaders() throws Exception {
    CustomRequest<TokenHolder> request = new CustomRequest<>(client, server.getBaseUrl(), "POST", tokenHolderType);
    request.addParameter("non_empty", "body");
    request.addHeader("Extra-Info", "this is a test");
    request.addHeader("Authorization", "Bearer my_access_token");
    server.jsonResponse(AUTH_TOKENS, 200);
    request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getHeader("Extra-Info"), is("this is a test"));
    assertThat(recordedRequest.getHeader("Authorization"), is("Bearer my_access_token"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TokenHolder(com.auth0.json.auth.TokenHolder)

Example 33 with Client

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

the class CustomRequestTest method shouldParseRateLimitsHeaders.

@Test
public void shouldParseRateLimitsHeaders() {
    CustomRequest<List> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType);
    server.rateLimitReachedResponse(100, 10, 5);
    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(100L));
    assertThat(rateLimitException.getRemaining(), is(10L));
    assertThat(rateLimitException.getReset(), is(5L));
}
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)

Example 34 with Client

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

the class CustomRequestTest method shouldParseJSONErrorResponseWithMessage.

@Test
public void shouldParseJSONErrorResponseWithMessage() throws Exception {
    CustomRequest<List> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType);
    server.jsonResponse(MGMT_ERROR_WITH_MESSAGE, 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: Query validation error: 'String 'invalid_field' does not match pattern. Must be a comma separated list of the following values: allowed_logout_urls,change_password."));
    APIException authException = (APIException) exception;
    assertThat(authException.getDescription(), is("Query validation error: 'String 'invalid_field' does not match pattern. Must be a comma separated list of the following values: allowed_logout_urls,change_password."));
    assertThat(authException.getError(), is("invalid_query_string"));
    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 35 with Client

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

the class CustomRequestTest method shouldCreateGETRequest.

@Test
public void shouldCreateGETRequest() throws Exception {
    CustomRequest<TokenHolder> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", tokenHolderType);
    assertThat(request, is(notNullValue()));
    server.jsonResponse(AUTH_TOKENS, 200);
    TokenHolder execute = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getMethod(), is("GET"));
    assertThat(execute, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TokenHolder(com.auth0.json.auth.TokenHolder)

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