-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imp: build route with a media type other than JSON (#9756)
- Loading branch information
Showing
3 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
test-suite/src/test/java/io/micronaut/context/router/RouteBuilderMediaTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package io.micronaut.context.router; | ||
|
||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.context.BeanContext; | ||
import io.micronaut.context.ExecutionHandleLocator; | ||
import io.micronaut.context.annotation.Executable; | ||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.core.annotation.Introspected; | ||
import io.micronaut.core.util.StringUtils; | ||
import io.micronaut.http.*; | ||
import io.micronaut.http.annotation.*; | ||
import io.micronaut.http.client.BlockingHttpClient; | ||
import io.micronaut.http.client.HttpClient; | ||
import io.micronaut.http.client.annotation.Client; | ||
import io.micronaut.http.uri.UriBuilder; | ||
import io.micronaut.inject.BeanDefinition; | ||
import io.micronaut.inject.ExecutableMethod; | ||
import io.micronaut.inject.ExecutionHandle; | ||
import io.micronaut.inject.MethodExecutionHandle; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import io.micronaut.web.router.DefaultRouteBuilder; | ||
import jakarta.inject.Singleton; | ||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
import spock.lang.Specification; | ||
|
||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
@Property(name = "micronaut.http.client.follow-redirects", value = StringUtils.FALSE) | ||
@Property(name = "spec.name", value = "RouteBuilderMediaTypeSpec") | ||
@MicronautTest | ||
class RouteBuilderMediaTypeTest extends Specification { | ||
|
||
@Test | ||
void createTest(@Client("/") HttpClient httpClient) { | ||
BlockingHttpClient client = httpClient.toBlocking(); | ||
HttpRequest<?> request = HttpRequest.GET("/contact/create").accept(MediaType.TEXT_HTML); | ||
HttpResponse<String> responseHtml = assertDoesNotThrow(() -> client.exchange(request, String.class)); | ||
assertTrue(responseHtml.getContentType().isPresent()); | ||
assertEquals(MediaType.TEXT_HTML_TYPE, responseHtml.getContentType().get()); | ||
String html = responseHtml.body(); | ||
assertNotNull(html); | ||
} | ||
|
||
@Test | ||
void saveTest(@Client("/") HttpClient httpClient) { | ||
BlockingHttpClient client = httpClient.toBlocking(); | ||
HttpRequest<?> request = HttpRequest.POST(UriBuilder.of("/contact").path("save").build(), | ||
Map.of("firstName", "Sergio", "lastName", "del Amo")) | ||
.contentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE); | ||
HttpResponse<?> response = assertDoesNotThrow(() -> client.exchange(request, String.class)); | ||
assertEquals(HttpStatus.SEE_OTHER, response.getStatus()); | ||
assertEquals("/foo", response.getHeaders().get(HttpHeaders.LOCATION)); | ||
} | ||
|
||
@Requires(property = "spec.name", value = "RouteBuilderMediaTypeSpec") | ||
@Singleton | ||
static class CreateSaveRouteBuilder extends DefaultRouteBuilder { | ||
|
||
CreateSaveRouteBuilder(ExecutionHandleLocator executionHandleLocator, | ||
BeanContext beanContext, | ||
List<ContactController> contactControllerList) { | ||
super(executionHandleLocator); | ||
for (ContactController controller : contactControllerList) { | ||
beanContext.getBeanDefinition(ContactController.class); | ||
BeanDefinition<ContactController> bd = beanContext.getBeanDefinition(ContactController.class); | ||
bd.findMethod("create", HttpRequest.class).ifPresent(m -> { | ||
MethodExecutionHandle<Object, Object> executionHandle = ExecutionHandle.of(controller, (ExecutableMethod) m); | ||
buildRoute(HttpMethod.GET, "/contact/create", executionHandle); | ||
}); | ||
bd.findMethod("save", HttpRequest.class, Contact.class).ifPresent(m -> { | ||
MethodExecutionHandle<Object, Object> executionHandle = ExecutionHandle.of(controller, (ExecutableMethod) m); | ||
buildRoute(HttpMethod.POST, "/contact/save", Collections.singletonList(MediaType.APPLICATION_FORM_URLENCODED_TYPE), executionHandle); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
|
||
@Requires(property = "spec.name", value = "RouteBuilderMediaTypeSpec") | ||
@Singleton | ||
static class ContactController { | ||
|
||
@Produces(MediaType.TEXT_HTML) | ||
@Get | ||
@Executable | ||
String create(HttpRequest request) { | ||
return "<!DOCTYPE html><html><body></body></html>"; | ||
} | ||
|
||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Post | ||
@Executable | ||
HttpResponse<?> save(HttpRequest request, @NotNull @Valid @Body Contact form) { | ||
return HttpResponse.seeOther(URI.create("/foo")); | ||
} | ||
} | ||
|
||
@Introspected | ||
record Contact(String firstName, String lastName) { | ||
|
||
} | ||
|
||
} |