Skip to content

Commit

Permalink
Add support for 403 responses (#7)
Browse files Browse the repository at this point in the history
* Add support for 403 responses
  • Loading branch information
palemorningdun authored and nicmunroe committed Nov 22, 2016
1 parent c969e4d commit efe65eb
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ gradle-app.setting
out/
classes/
.idea_modules/

### Eclipse stuff
.project
.settings
.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.nike.internal.util.StringUtils;
import com.nike.riposte.server.error.exception.DownstreamChannelClosedUnexpectedlyException;
import com.nike.riposte.server.error.exception.DownstreamIdleChannelTimeoutException;
import com.nike.riposte.server.error.exception.Forbidden403Exception;
import com.nike.riposte.server.error.exception.HostnameResolutionException;
import com.nike.riposte.server.error.exception.InvalidCharsetInContentTypeHeaderException;
import com.nike.riposte.server.error.exception.MethodNotAllowed405Exception;
Expand Down Expand Up @@ -171,6 +172,19 @@ public ApiExceptionHandlerListenerResult shouldHandleException(Throwable ex) {
extraDetails
);
}

if (ex instanceof Forbidden403Exception) {
Forbidden403Exception theEx = (Forbidden403Exception) ex;
List<Pair<String, String>> extraDetails = new ArrayList<>();
extraDetails.add(Pair.of("message", ex.getMessage()));
extraDetails.add(Pair.of("incoming_request_path", theEx.requestPath));
extraDetails.add(Pair.of("authorization_header", theEx.authorizationHeader));
extraDetails.addAll((theEx).extraDetailsForLogging);
return ApiExceptionHandlerListenerResult.handleResponse(
singletonError(projectApiErrors.getForbiddenApiError()),
extraDetails
);
}

if (ex instanceof MultipleMatchingEndpointsException) {
MultipleMatchingEndpointsException theEx = (MultipleMatchingEndpointsException) ex;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
*
*/
package com.nike.riposte.server.error.exception;

import java.util.ArrayList;
import java.util.List;

import com.nike.internal.util.Pair;

/**
* Thrown when a request does not have a valid authorization header. Represents a HTTP 403 response code.
*
* @author pevans
*
*/
public class Forbidden403Exception extends RuntimeException {

public final String requestPath;
public final String authorizationHeader;
public final List<Pair<String, String>> extraDetailsForLogging;

public Forbidden403Exception(String message, String requestPath, String authorizationHeader) {
this(message, requestPath, authorizationHeader, new ArrayList<>());
}

public Forbidden403Exception(String message, String requestPath, String authorizationHeader,
List<Pair<String, String>> extraDetailsForLogging) {
super(message);

this.requestPath = requestPath;
this.authorizationHeader = authorizationHeader;
this.extraDetailsForLogging = extraDetailsForLogging;
}
private static final long serialVersionUID = 4921880566299500314L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.nike.fastbreak.exception.CircuitBreakerTimeoutException;
import com.nike.riposte.server.error.exception.DownstreamChannelClosedUnexpectedlyException;
import com.nike.riposte.server.error.exception.DownstreamIdleChannelTimeoutException;
import com.nike.riposte.server.error.exception.Forbidden403Exception;
import com.nike.riposte.server.error.exception.HostnameResolutionException;
import com.nike.riposte.server.error.exception.InvalidCharsetInContentTypeHeaderException;
import com.nike.riposte.server.error.exception.MethodNotAllowed405Exception;
Expand Down Expand Up @@ -143,6 +144,11 @@ public void shouldHandleMethodNotAllowed405Exception() {
public void should_handle_Unauthorized401Exception() {
verifyExceptionHandled(new Unauthorized401Exception("foo", "/bar", "blah"), singletonError(testProjectApiErrors.getUnauthorizedApiError()));
}

@Test
public void should_handle_Forbidden403Exception() {
verifyExceptionHandled(new Forbidden403Exception("foo", "/bar", "blah"), singletonError(testProjectApiErrors.getForbiddenApiError()));
}

@Test
public void shouldHandleMultipleMatchingEndpointsException() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nike.riposte.server.error.exception;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.UUID;

import org.junit.Before;
import org.junit.Test;

public class Forbidden403ExceptionTest {

@Before
public void setUp() throws Exception {
}

@Test
public void should_honor_constructor_params() {
//given
String requestPath = UUID.randomUUID().toString();
String authorizationHeader = UUID.randomUUID().toString();
String message = UUID.randomUUID().toString();

//when
Forbidden403Exception ex = new Forbidden403Exception(message, requestPath, authorizationHeader);

//then
assertThat(ex.getMessage(), is(message));
assertThat(ex.requestPath, is(requestPath));
assertThat(ex.authorizationHeader, is(authorizationHeader));
}

}

0 comments on commit efe65eb

Please sign in to comment.