Skip to content

Commit 6a4bf9f

Browse files
authored
fix: proxy-test property sources to lambda handler ctx (#1337)
1 parent ab2428a commit 6a4bf9f

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

function-aws-api-proxy-test/src/main/java/io/micronaut/function/aws/proxy/test/AwsApiProxyTestServer.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
2020
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
2121
import io.micronaut.context.ApplicationContext;
22+
import io.micronaut.context.ApplicationContextBuilder;
2223
import io.micronaut.context.env.Environment;
24+
import io.micronaut.context.env.PropertySource;
2325
import io.micronaut.core.annotation.Internal;
2426
import io.micronaut.core.io.socket.SocketUtils;
2527
import io.micronaut.function.aws.proxy.MicronautLambdaHandler;
@@ -28,11 +30,11 @@
2830
import io.micronaut.http.server.exceptions.ServerStartupException;
2931
import io.micronaut.runtime.ApplicationConfiguration;
3032
import io.micronaut.runtime.server.EmbeddedServer;
33+
import jakarta.inject.Singleton;
3134
import org.eclipse.jetty.server.Request;
3235
import org.eclipse.jetty.server.Server;
3336
import org.eclipse.jetty.server.handler.AbstractHandler;
3437

35-
import jakarta.inject.Singleton;
3638
import javax.servlet.http.HttpServletRequest;
3739
import javax.servlet.http.HttpServletResponse;
3840
import java.io.IOException;
@@ -91,7 +93,7 @@ public EmbeddedServer start() {
9193
while (retryCount <= 3) {
9294
try {
9395
this.server = new Server(port);
94-
this.server.setHandler(new AwsProxyHandler());
96+
this.server.setHandler(new AwsProxyHandler(applicationContext));
9597
this.server.start();
9698
break;
9799
} catch (BindException e) {
@@ -181,8 +183,14 @@ private static class AwsProxyHandler extends AbstractHandler {
181183
private final ServletToAwsProxyResponseAdapter responseAdapter;
182184
private final ContextProvider contextProvider;
183185

184-
public AwsProxyHandler() throws ContainerInitializationException {
185-
lambdaHandler = new MicronautLambdaHandler();
186+
public AwsProxyHandler(ApplicationContext proxyTestApplicationContext) throws ContainerInitializationException {
187+
ApplicationContextBuilder builder = ApplicationContext.builder();
188+
for (PropertySource propertySource : proxyTestApplicationContext.getEnvironment()
189+
.getPropertySources()) {
190+
builder = builder.propertySources(propertySource);
191+
}
192+
lambdaHandler = new MicronautLambdaHandler(builder);
193+
186194
ApplicationContext ctx = lambdaHandler.getApplicationContext();
187195
this.requestAdapter = ctx.getBean(ServletToAwsProxyRequestAdapter.class);
188196
this.responseAdapter = ctx.getBean(ServletToAwsProxyResponseAdapter.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.micronaut.function.aws.proxy.test
2+
3+
import io.micronaut.context.annotation.Property
4+
import io.micronaut.context.annotation.Requires
5+
import io.micronaut.http.HttpRequest
6+
import io.micronaut.http.HttpResponse
7+
import io.micronaut.http.HttpStatus
8+
import io.micronaut.http.MediaType
9+
import io.micronaut.http.annotation.Controller
10+
import io.micronaut.http.annotation.Get
11+
import io.micronaut.http.annotation.Status
12+
import io.micronaut.http.client.HttpClient
13+
import io.micronaut.http.client.annotation.Client
14+
import io.micronaut.test.extensions.spock.annotation.MicronautTest
15+
import jakarta.inject.Inject
16+
import spock.lang.Specification
17+
18+
@Property(name = 'spec.name', value = 'ContentTypeSpec')
19+
@MicronautTest
20+
class ContentTypeSpec extends Specification {
21+
22+
@Inject
23+
@Client("/")
24+
HttpClient httpClient
25+
26+
void "verify controllers return json by default"() {
27+
given:
28+
HttpRequest<?> request = HttpRequest.GET('/json/bydefault').accept(MediaType.APPLICATION_JSON)
29+
30+
when:
31+
HttpResponse<String> response = httpClient.toBlocking().exchange(request, String)
32+
33+
then:
34+
HttpStatus.OK == response.status()
35+
response.body.get() == '{"msg":"Hello world"}'
36+
response.headers
37+
["application/json"] == response.headers.getAll("Content-Type")
38+
}
39+
40+
@Controller('/json')
41+
@Requires(property = 'spec.name', value = 'ContentTypeSpec')
42+
static class BodyController {
43+
44+
@Get("/bydefault")
45+
@Status(HttpStatus.OK)
46+
Map<String, Object> index() {
47+
[msg: "Hello world"]
48+
}
49+
}
50+
}
51+

0 commit comments

Comments
 (0)