-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from gettyimages/authFailureHandling
Auth failure handling
- Loading branch information
Showing
5 changed files
with
144 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
gettyimagesapi-sdk/src/main/java/com/gettyimages/api/HttpClientErrorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.gettyimages.api; | ||
|
||
import org.apache.http.StatusLine; | ||
|
||
public class HttpClientErrorException extends SdkException { | ||
|
||
private final int statusCode; | ||
private final String reasonPhrase; | ||
|
||
public HttpClientErrorException(final StatusLine statusLine) { | ||
super("Client Error (" + statusLine.getStatusCode() + "): " + statusLine.getReasonPhrase()); | ||
this.statusCode = statusLine.getStatusCode(); | ||
this.reasonPhrase = statusLine.getReasonPhrase(); | ||
} | ||
|
||
public int getStatusCode() { | ||
return statusCode; | ||
} | ||
|
||
public String getReasonPhrase() { | ||
return reasonPhrase; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
gettyimagesapi-sdk/src/main/java/com/gettyimages/api/HttpSystemErrorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.gettyimages.api; | ||
|
||
import org.apache.http.StatusLine; | ||
|
||
public class HttpSystemErrorException extends SdkException { | ||
|
||
private final int statusCode; | ||
private final String reasonPhrase; | ||
|
||
public HttpSystemErrorException(final StatusLine statusLine) { | ||
super("System Error (" + statusLine.getStatusCode() + "): " + statusLine.getReasonPhrase()); | ||
this.statusCode = statusLine.getStatusCode(); | ||
this.reasonPhrase = statusLine.getReasonPhrase(); | ||
} | ||
|
||
public int getStatusCode() { | ||
return statusCode; | ||
} | ||
|
||
public String getReasonPhrase() { | ||
return reasonPhrase; | ||
} | ||
|
||
public HttpSystemErrorException(final Exception e) { | ||
super("System Error (500): Unexpected Error"); | ||
this.statusCode = 500; | ||
this.reasonPhrase = "UnexpectedError"; // TODO: de-dupe | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockserver.integration.ClientAndServer.startClientAndServer; | ||
import static org.mockserver.model.HttpRequest.request; | ||
import static org.mockserver.model.HttpResponse.response; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
import com.gettyimages.api.ApiClient; | ||
import com.gettyimages.api.HttpClientErrorException; | ||
import com.gettyimages.api.HttpSystemErrorException; | ||
import com.gettyimages.api.Search.SearchImages; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockserver.client.server.MockServerClient; | ||
import org.mockserver.integration.ClientAndServer; | ||
import org.mockserver.model.Parameter; | ||
|
||
public class AuthFailureTest { | ||
private ClientAndServer mockServer; | ||
|
||
@BeforeEach | ||
public void startProxy() throws Exception { | ||
final Field field = ApiClient.class.getDeclaredField("baseUrl"); | ||
field.setAccessible(true); | ||
field.set(null, "http://127.0.0.1:1080/"); | ||
mockServer = startClientAndServer(1080); | ||
} | ||
|
||
@Test | ||
void clientErrorOnAuthentication() throws Exception { | ||
final int statusCode = 400; | ||
createMock(statusCode); | ||
final ApiClient client = ApiClient.GetApiClientWithClientCredentials("apiKey", "apiSecret"); | ||
final SearchImages search = client.searchimages() | ||
.withPhrase("cat"); | ||
|
||
final HttpClientErrorException exception = assertThrows(HttpClientErrorException.class, () -> { search.executeAsync(); } ); | ||
assertEquals(exception.getStatusCode(), statusCode); | ||
} | ||
|
||
@Test | ||
void systemErrorOnAuthentication() throws Exception { | ||
final int statusCode = 500; | ||
createMock(statusCode); | ||
final ApiClient client = ApiClient.GetApiClientWithClientCredentials("apiKey", "apiSecret"); | ||
final SearchImages search = client.searchimages() | ||
.withPhrase("cat"); | ||
|
||
final HttpSystemErrorException exception = assertThrows(HttpSystemErrorException.class, () -> { search.executeAsync(); } ); | ||
assertEquals(exception.getStatusCode(), statusCode); | ||
} | ||
|
||
|
||
@AfterEach | ||
public void stopProxy() { | ||
mockServer.stop(); | ||
} | ||
|
||
private void createMock(final int statusCode) { | ||
final MockServerClient client = new MockServerClient("127.0.0.1", 1080); | ||
|
||
client.when(request().withMethod("POST").withPath("/oauth2/token")) | ||
.respond(response().withStatusCode(statusCode)); | ||
client.when(request().withMethod("GET").withPath("/search/images") | ||
.withQueryStringParameters(new Parameter("phrase", "cat"))) | ||
.respond(response().withStatusCode(200).withBody("success")); | ||
} | ||
} |