Search in sources :

Example 81 with Client

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

the class MultipartRequestTest method shouldThrowOnParseInvalidSuccessfulResponse.

@Test
public void shouldThrowOnParseInvalidSuccessfulResponse() throws Exception {
    MultipartRequest<List> request = new MultipartRequest<>(client, server.getBaseUrl(), "POST", listType);
    request.addPart("non_empty", "body");
    server.jsonResponse(AUTH_TOKENS, 200);
    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(JsonMappingException.class)));
    assertThat(exception.getMessage(), is("Request failed with status code 200: Failed to parse the response body."));
    APIException authException = (APIException) exception;
    assertThat(authException.getDescription(), is("Failed to parse the response body."));
    assertThat(authException.getError(), is(nullValue()));
    assertThat(authException.getStatusCode(), is(200));
}
Also used : APIException(com.auth0.exception.APIException) List(java.util.List) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ExpectedException(org.junit.rules.ExpectedException) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Example 82 with Client

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

the class MultipartRequestTest method shouldThrowOnExecuteFailure.

@Test
public void shouldThrowOnExecuteFailure() throws Exception {
    exception.expect(Auth0Exception.class);
    exception.expectCause(Matchers.<Throwable>instanceOf(IOException.class));
    exception.expectMessage("Failed to execute request");
    OkHttpClient client = mock(OkHttpClient.class);
    Call call = mock(Call.class);
    when(client.newCall(any(okhttp3.Request.class))).thenReturn(call);
    when(call.execute()).thenThrow(IOException.class);
    MultipartRequest<Void> request = new MultipartRequest<>(client, server.getBaseUrl(), "POST", voidType);
    request.addPart("non_empty", "body");
    request.execute();
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest)

Example 83 with Client

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

the class MultipartRequestTest method shouldAddHeaders.

@Test
public void shouldAddHeaders() throws Exception {
    MultipartRequest<TokenHolder> request = new MultipartRequest<>(client, server.getBaseUrl(), "POST", tokenHolderType);
    request.addPart("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) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest)

Example 84 with Client

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

the class MultipartRequestTest method shouldParseSuccessfulResponse.

@Test
public void shouldParseSuccessfulResponse() throws Exception {
    MultipartRequest<TokenHolder> request = new MultipartRequest<>(client, server.getBaseUrl(), "POST", tokenHolderType);
    request.addPart("non_empty", "body");
    server.jsonResponse(AUTH_TOKENS, 200);
    TokenHolder response = request.execute();
    server.takeRequest();
    assertThat(response, is(notNullValue()));
    assertThat(response.getAccessToken(), not(emptyOrNullString()));
    assertThat(response.getIdToken(), not(emptyOrNullString()));
    assertThat(response.getRefreshToken(), not(emptyOrNullString()));
    assertThat(response.getTokenType(), not(emptyOrNullString()));
    assertThat(response.getExpiresIn(), is(notNullValue()));
}
Also used : TokenHolder(com.auth0.json.auth.TokenHolder) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest)

Example 85 with Client

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

the class MultipartRequestTest method shouldParseJSONErrorResponseWithError.

@Test
public void shouldParseJSONErrorResponseWithError() throws Exception {
    MultipartRequest<List> request = new MultipartRequest<>(client, server.getBaseUrl(), "POST", listType);
    request.addPart("non_empty", "body");
    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) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) APIException(com.auth0.exception.APIException) RateLimitException(com.auth0.exception.RateLimitException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ExpectedException(org.junit.rules.ExpectedException) Auth0Exception(com.auth0.exception.Auth0Exception) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

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