Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public String getPathTranslated() {

@Override
public String getContextPath() {
return "/";
return request.getRequestContext().getStage();
}


Expand Down Expand Up @@ -268,10 +268,15 @@ public String getRequestURI() {
@Override
public StringBuffer getRequestURL() {
String url = "";
url += getHeaderCaseInsensitive(HttpHeaders.HOST);
url += getServerName();
url += "/";
url += getContextPath();
url += "/";
url += request.getPath();
return new StringBuffer(url);

url = url.replaceAll("/+", "/");

return new StringBuffer(getScheme() + "://" + url);
}


Expand Down Expand Up @@ -608,13 +613,18 @@ public String getScheme() {

@Override
public String getServerName() {
return "lambda.amazonaws.com";
String name = getHeaderCaseInsensitive(HttpHeaders.HOST);

if (name == null || name.length() == 0) {
name = "lambda.amazonaws.com";
}
return name;
}


@Override
public int getServerPort() {
return 0;
return getLocalPort();
}


Expand Down Expand Up @@ -720,7 +730,14 @@ public String getLocalAddr() {

@Override
public int getLocalPort() {
return 0;
int port = 0;

if ("https".equals(getScheme())) {
port = 443;
} else if ("http".equals(getScheme())) {
port = 80;
}
return port;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public AwsProxyRequestBuilder(String path, String httpMethod) {
this.request.setPath(path);
this.request.setQueryStringParameters(new HashMap<>());
this.request.setRequestContext(new ApiGatewayRequestContext());
this.request.getRequestContext().setStage("test");
ApiGatewayRequestIdentity identity = new ApiGatewayRequestIdentity();
identity.setSourceIp("127.0.0.1");
this.request.getRequestContext().setIdentity(identity);
Expand Down Expand Up @@ -168,6 +169,28 @@ public AwsProxyRequestBuilder cookie(String name, String value) {
return this;
}

public AwsProxyRequestBuilder scheme(String scheme) {
if (request.getHeaders() == null) {
request.setHeaders(new HashMap<>());
}

request.getHeaders().put("CloudFront-Forwarded-Proto", scheme);
return this;
}

public AwsProxyRequestBuilder serverName(String serverName) {
if (request.getHeaders() == null) {
request.setHeaders(new HashMap<>());
}

request.getHeaders().put("Host", serverName);
return this;
}

public AwsProxyRequestBuilder stage(String stage) {
this.request.getRequestContext().setStage(stage);
return this;
}

public AwsProxyRequest build() {
return this.request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,31 @@ public void servletRequestEncoding_acceptEncoding_okStatusCode() {
assertEquals(200, output.getStatusCode());
}

@Test
public void request_requestURI() {
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/request-URI", "GET")
.build();

AwsProxyResponse output = handler.proxy(request, lambdaContext);
assertEquals(200, output.getStatusCode());

validateSingleValueModel(output, "/echo/request-URI");
}

@Test
public void request_requestURL() {
AwsProxyRequest request = new AwsProxyRequestBuilder("/echo/request-Url", "GET")
.scheme("https")
.serverName("api.myserver.com")
.stage("prod")
.build();

AwsProxyResponse output = handler.proxy(request, lambdaContext);
assertEquals(200, output.getStatusCode());

validateSingleValueModel(output, "https://api.myserver.com/prod/echo/request-Url");
}

private void validateMapResponseModel(AwsProxyResponse output) {
try {
MapResponseModel response = objectMapper.readValue(output.getBody(), MapResponseModel.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,20 @@ public ResponseEntity<byte[]> echoBinaryData() {

return new ResponseEntity<byte[]>(b, HttpStatus.OK);
}

@RequestMapping(path = "/request-URI", method = RequestMethod.GET)
public SingleValueModel echoRequestURI(HttpServletRequest request) {
SingleValueModel valueModel = new SingleValueModel();
valueModel.setValue(request.getRequestURI());

return valueModel;
}

@RequestMapping(path = "/request-Url", method = RequestMethod.GET)
public SingleValueModel echoRequestURL(HttpServletRequest request) {
SingleValueModel valueModel = new SingleValueModel();
valueModel.setValue(request.getRequestURL().toString());

return valueModel;
}
}