Skip to content

Commit

Permalink
Merge pull request #9 from agorapulse/fix/aws-api-gateway-path-bug
Browse files Browse the repository at this point in the history
workaround bug in API Gateway where path contains stage prefix
  • Loading branch information
musketyr authored Oct 22, 2018
2 parents 39cc888 + 564409a commit 93d5ce4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
allprojects {
group 'com.agorapulse'
version micronautVersion
version "${micronautVersion}.1"

repositories {
jcenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.*;
import java.util.regex.Pattern;

abstract class ApiGatewayProxyHttpRequest<B> implements HttpRequest<B> {

Expand Down Expand Up @@ -145,7 +146,9 @@ public Collection<Cookie> values() {

remoteAddress = new InetSocketAddress(identity.getSourceIp(), HTTPS_PORT);

MutableHttpRequest request = HttpRequest.create(this.method, "https://" + serverName + input.getPath());
MutableHttpRequest request = HttpRequest.create(this.method,
"https://" + serverName + reconstructPath(Optional.ofNullable(input.getResource()).orElse(input.getPath()), input.getPathParameters())
);

this.uri = request.getUri();

Expand Down Expand Up @@ -197,4 +200,15 @@ public MutableConvertibleValues<Object> getAttributes() {
public String toString() {
return getClass().getSimpleName() + ": " + input.toString();
}

static String reconstructPath(String resource, Map<String, String> pathVariables) {
if (pathVariables == null) {
return resource;
}
String path = resource;
for (Map.Entry<String, String> variable : pathVariables.entrySet()) {
path = path.replaceAll("\\{" + Pattern.quote(variable.getKey()) +"\\+?}", variable.getValue());
}
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.agorapulse.gru.Gru
import com.agorapulse.gru.agp.ApiGatewayProxy
import com.agorapulse.micronaut.http.server.tck.AbstractApiGatewayProxyHttpRequestSpec
import org.junit.Rule
import spock.lang.Unroll


class ApiGatewayProxyHttpRequestSpec extends AbstractApiGatewayProxyHttpRequestSpec {
Expand All @@ -16,4 +17,15 @@ class ApiGatewayProxyHttpRequestSpec extends AbstractApiGatewayProxyHttpRequestS
map '/hello/mfa' to ApiGatewayProxyHandler
})

@Unroll
void 'test reconstruct path #path with variable #variables to #original'() {
expect:
ApiGatewayProxyHttpRequest.reconstructPath(resource, variables) == original
where:
original | resource | variables
'/foo/bar' | '/foo/bar' | null
'/foo/bar' | '/foo/{place}' | [place: 'bar']
'/foo/bar' | '/{proxy+}' | [proxy: 'foo/bar']
}

}

0 comments on commit 93d5ce4

Please sign in to comment.