Search in sources :

Example 96 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class BaseRequestTest method asyncCompletesSuccessfully.

@Test
public void asyncCompletesSuccessfully() {
    doReturn(call).when(client).newCall(any());
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(0)).onResponse(call, response);
        return null;
    }).when(call).enqueue(any());
    CompletableFuture<?> request = new MockBaseRequest<String>(client) {

        @Override
        protected String parseResponse(Response response) throws Auth0Exception {
            return "Success";
        }
    }.executeAsync();
    Exception exception = null;
    Object result = null;
    try {
        result = request.get();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(nullValue()));
    assertThat(result, is(instanceOf(String.class)));
    assertThat(result, is("Success"));
}
Also used : 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 97 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class BaseRequestTest method asyncCompletesWithExceptionWhenRequestFails.

@Test
public void asyncCompletesWithExceptionWhenRequestFails() throws Exception {
    doReturn(call).when(client).newCall(any());
    doAnswer(invocation -> {
        ((Callback) invocation.getArgument(0)).onFailure(call, new IOException("Error!"));
        return null;
    }).when(call).enqueue(any());
    CompletableFuture<?> request = new MockBaseRequest<String>(client) {

        @Override
        protected String parseResponse(Response response) throws Auth0Exception {
            throw new Auth0Exception("Response Parsing Error");
        }
    }.executeAsync();
    Exception exception = null;
    Object result = null;
    try {
        result = request.get();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(result, is(nullValue()));
    assertThat(exception.getCause(), is(instanceOf(Auth0Exception.class)));
    assertThat(exception.getCause().getMessage(), is("Failed to execute request"));
    assertThat(exception.getCause().getCause(), is(instanceOf(IOException.class)));
    assertThat(exception.getCause().getCause().getMessage(), is("Error!"));
}
Also used : Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) Test(org.junit.Test)

Example 98 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class BaseRequestTest method asyncCompletesWithExceptionWhenRequestCreationFails.

@Test
public void asyncCompletesWithExceptionWhenRequestCreationFails() throws Exception {
    CompletableFuture<?> request = new MockBaseRequest<String>(client) {

        @Override
        protected Request parseResponse(Response response) throws Auth0Exception {
            throw new Auth0Exception("Response Parsing Error");
        }

        @Override
        protected Request createRequest() throws Auth0Exception {
            throw new Auth0Exception("Create Request Error");
        }
    }.executeAsync();
    Exception exception = null;
    Object result = null;
    try {
        result = request.get();
    } catch (Exception e) {
        exception = e;
    }
    assertThat(result, is(nullValue()));
    assertThat(exception, is(notNullValue()));
    assertThat(exception.getCause(), is(instanceOf(Auth0Exception.class)));
    assertThat(exception.getCause().getMessage(), is("Create Request Error"));
}
Also used : Auth0Exception(com.auth0.exception.Auth0Exception) Request(okhttp3.Request) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) Test(org.junit.Test)

Example 99 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class CustomRequestTest method shouldParsePlainTextErrorResponse.

@Test
public void shouldParsePlainTextErrorResponse() throws Exception {
    CustomRequest<List> request = new CustomRequest<>(client, server.getBaseUrl(), "GET", listType);
    server.textResponse(AUTH_ERROR_PLAINTEXT, 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(instanceOf(JsonParseException.class)));
    assertThat(exception.getMessage(), is("Request failed with status code 400: A plain-text error response"));
    APIException authException = (APIException) exception;
    assertThat(authException.getDescription(), is("A plain-text error response"));
    assertThat(authException.getError(), is(nullValue()));
    assertThat(authException.getValue("non_existing_key"), is(nullValue()));
    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 100 with Request

use of com.auth0.net.Request in project auth0-java by auth0.

the class CustomRequestTest method shouldNotOverrideContentTypeHeader.

@Test
public void shouldNotOverrideContentTypeHeader() throws Exception {
    CustomRequest<TokenHolder> request = new CustomRequest<>(client, server.getBaseUrl(), "POST", tokenHolderType);
    request.addParameter("non_empty", "body");
    request.addHeader("Content-Type", "plaintext");
    server.jsonResponse(AUTH_TOKENS, 200);
    request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest.getHeader("Content-Type"), is("application/json"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) TokenHolder(com.auth0.json.auth.TokenHolder)

Aggregations

Test (org.junit.Test)193 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)185 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)77 IOException (java.io.IOException)76 List (java.util.List)63 Algorithm (com.auth0.jwt.algorithms.Algorithm)35 VoidRequest (com.auth0.net.VoidRequest)33 Auth0Exception (com.auth0.exception.Auth0Exception)30 APIException (com.auth0.exception.APIException)27 RateLimitException (com.auth0.exception.RateLimitException)25 HashMap (java.util.HashMap)24 PageFilter (com.auth0.client.mgmt.filter.PageFilter)23 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)23 ServletException (javax.servlet.ServletException)23 TokenHolder (com.auth0.json.auth.TokenHolder)22 JWTVerifier (com.auth0.jwt.JWTVerifier)22 ArrayList (java.util.ArrayList)22 Test (org.junit.jupiter.api.Test)22 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)20 Date (java.util.Date)20