Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proxy-test property sources to lambda handler ctx #1337

Merged
merged 1 commit into from
Apr 12, 2022
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 @@ -19,7 +19,9 @@
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.ApplicationContextBuilder;
import io.micronaut.context.env.Environment;
import io.micronaut.context.env.PropertySource;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.io.socket.SocketUtils;
import io.micronaut.function.aws.proxy.MicronautLambdaHandler;
Expand All @@ -28,11 +30,11 @@
import io.micronaut.http.server.exceptions.ServerStartupException;
import io.micronaut.runtime.ApplicationConfiguration;
import io.micronaut.runtime.server.EmbeddedServer;
import jakarta.inject.Singleton;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

import jakarta.inject.Singleton;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
Expand Down Expand Up @@ -91,7 +93,7 @@ public EmbeddedServer start() {
while (retryCount <= 3) {
try {
this.server = new Server(port);
this.server.setHandler(new AwsProxyHandler());
this.server.setHandler(new AwsProxyHandler(applicationContext));
this.server.start();
break;
} catch (BindException e) {
Expand Down Expand Up @@ -181,8 +183,14 @@ private static class AwsProxyHandler extends AbstractHandler {
private final ServletToAwsProxyResponseAdapter responseAdapter;
private final ContextProvider contextProvider;

public AwsProxyHandler() throws ContainerInitializationException {
lambdaHandler = new MicronautLambdaHandler();
public AwsProxyHandler(ApplicationContext proxyTestApplicationContext) throws ContainerInitializationException {
ApplicationContextBuilder builder = ApplicationContext.builder();
for (PropertySource propertySource : proxyTestApplicationContext.getEnvironment()
.getPropertySources()) {
builder = builder.propertySources(propertySource);
}
lambdaHandler = new MicronautLambdaHandler(builder);

ApplicationContext ctx = lambdaHandler.getApplicationContext();
this.requestAdapter = ctx.getBean(ServletToAwsProxyRequestAdapter.class);
this.responseAdapter = ctx.getBean(ServletToAwsProxyResponseAdapter.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.micronaut.function.aws.proxy.test

import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.HttpStatus
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Status
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@Property(name = 'spec.name', value = 'ContentTypeSpec')
@MicronautTest
class ContentTypeSpec extends Specification {

@Inject
@Client("/")
HttpClient httpClient

void "verify controllers return json by default"() {
given:
HttpRequest<?> request = HttpRequest.GET('/json/bydefault').accept(MediaType.APPLICATION_JSON)

when:
HttpResponse<String> response = httpClient.toBlocking().exchange(request, String)

then:
HttpStatus.OK == response.status()
response.body.get() == '{"msg":"Hello world"}'
response.headers
["application/json"] == response.headers.getAll("Content-Type")
}

@Controller('/json')
@Requires(property = 'spec.name', value = 'ContentTypeSpec')
static class BodyController {

@Get("/bydefault")
@Status(HttpStatus.OK)
Map<String, Object> index() {
[msg: "Hello world"]
}
}
}