Skip to content

Commit

Permalink
Fix issue with root routing + improve tests (micronaut-projects#11123)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Aug 29, 2024
1 parent 06ca036 commit 713ff1c
Show file tree
Hide file tree
Showing 66 changed files with 514 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.micronaut

import io.micronaut.http.server.netty.AbstractMicronautSpec
import io.micronaut.web.router.Router

class PleaseEnableRoutesBySpecNameSpec extends AbstractMicronautSpec {

void "don't add more public routes in test"() {
// A lot of activate routes, filters etc. complicates debugging
// Please use @Requires(property="spec.name.. to enable your route and filters only for particular tests
given:
Router router = applicationContext.getBean(Router)
expect:
router.uriRoutes().toList().size() == 0
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.io.Writable;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.MediaType;
Expand All @@ -28,6 +29,7 @@
//end::imports[]

//tag::clazz[]
@Requires(property = "spec.name", value = "WritableSpec")
@Controller("/template")
public class TemplateController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import spock.lang.Specification

class WritableSpec extends Specification {

@Shared @AutoCleanup EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer)
@Shared @AutoCleanup EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, ['spec.name': 'WritableSpec'])
@Shared @AutoCleanup HttpClient client = embeddedServer.getApplicationContext().createBean(HttpClient, embeddedServer.getURL())


void "test render template"() {
expect:
HttpResponse<String> resp = client.toBlocking().exchange('/template/welcome', String)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.micronaut.http.server.netty

import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpHeaders
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
Expand Down Expand Up @@ -86,6 +87,7 @@ class ConnectionCloseSpec extends Specification {
server.close()
}

@Requires(property = 'spec.name', value = 'ConnectionCloseSpec')
@Controller("/connection")
static class ConnectionCloseController {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.micronaut.http.server.netty


import io.micronaut.context.annotation.Property
import io.micronaut.core.util.StringUtils
import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpRequest
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
Expand All @@ -17,7 +16,7 @@ import jakarta.inject.Named
import spock.lang.Specification

@MicronautTest
@Property(name = "NettyMultiServerSpec", value = StringUtils.TRUE)
@Property(name = "spec.name", value = "NettyMultiServerSpec")
class NettyMultiServerSpec extends Specification {

@Inject ApplicationConfiguration applicationConfiguration
Expand Down Expand Up @@ -46,6 +45,7 @@ class NettyMultiServerSpec extends Specification {
secondaryClient.close()
}

@Requires(property = "spec.name", value = "NettyMultiServerSpec")
@Controller("/test/secondary/server")
static class TestController {
@Get("/")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.micronaut.http.server.netty

import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpStatus
import io.micronaut.http.annotation.Controller
Expand Down Expand Up @@ -71,6 +72,7 @@ class RequestCertificateSpec extends AbstractMicronautSpec {
]
}

@Requires(property = 'spec.name', value = 'RequestCertificateSpec')
@Controller
static class TestController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
import io.micronaut.context.annotation.Bean;
import io.micronaut.context.annotation.Factory;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.server.netty.configuration.NettyHttpServerConfiguration;
import io.micronaut.runtime.ApplicationConfiguration;
import jakarta.inject.Inject;
import jakarta.inject.Named;

@Factory
@Requires(property = "NettyMultiServerSpec", value = StringUtils.TRUE)
@Requires(property = "spec.name", value = "NettyMultiServerSpec")
public class TestNettySecondaryServer {
@Inject ApplicationConfiguration applicationConfiguration;
@Inject NettyEmbeddedServerFactory embeddedServerFactory;

@Named("secondary")
@Bean(preDestroy = "stop")
@Requires(property = "NettyMultiServerSpec", value = StringUtils.TRUE)
@Requires(property = "spec.name", value = "NettyMultiServerSpec")
NettyEmbeddedServer secondaryServer() {
NettyEmbeddedServer embeddedServer = embeddedServerFactory
.build(new NettyHttpServerConfiguration(applicationConfiguration));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.http.server.netty.binding

import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpRequest
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.CookieValue
Expand Down Expand Up @@ -69,10 +70,12 @@ class CookieBindingSpec extends AbstractMicronautSpec {
result == "Cookie Value: foo"
}

@Requires(property = "spec.name", value = "CookieBindingSpec")
@Client('/cookie')
static interface CookieClient extends CookieApi {
}

@Requires(property = "spec.name", value = "CookieBindingSpec")
@Controller("/cookie")
static class CookieController implements CookieApi {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.micronaut.http.server.netty.binding

import io.micronaut.context.annotation.Requires
import io.micronaut.core.annotation.Introspected
import io.micronaut.core.convert.format.Format
import io.micronaut.http.*
Expand Down Expand Up @@ -83,6 +84,7 @@ class CustomParameterBindingSpec extends AbstractMicronautSpec {
response.getBody().get() == "Parameter Value: The Stand"
}

@Requires(property = 'spec.name', value = 'CustomParameterBindingSpec')
@Controller(value = "/parameter", produces = MediaType.TEXT_PLAIN)
static class ParameterController {
@CustomHttpMethod(method="REPORT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micronaut.http.server.netty.binding

import io.micronaut.context.annotation.Requires
import io.micronaut.core.convert.format.Format
import io.micronaut.http.HttpHeaders
import io.micronaut.http.HttpRequest
Expand Down Expand Up @@ -67,6 +68,7 @@ class HeaderBindingSpec extends AbstractMicronautSpec {
'/header/with-value' | "Header: application/json" | ['Content-Type': 'application/json']
}

@Requires(property = 'spec.name', value = 'HeaderBindingSpec')
@Controller("/header")
static class HeaderController {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package io.micronaut.http.server.netty.binding
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Requires
import io.micronaut.http.HttpHeaders
import io.micronaut.http.HttpMessage
import io.micronaut.http.HttpMethod
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.client.HttpClient
Expand Down Expand Up @@ -298,4 +301,81 @@ class HttpResponseSpec extends AbstractMicronautSpec {
Map<String, Object> getConfiguration() {
super.getConfiguration() << ['micronaut.server.date-header': false]
}

@Requires(property = 'spec.name', value = 'HttpResponseSpec')
@Controller("/java/response")
static class ResponseController {

@Get("/disallow")
public HttpResponse disallow() {
return HttpResponse.notAllowed(HttpMethod.DELETE);
}

@Get("/accepted")
public HttpResponse accepted() {
return HttpResponse.accepted();
}

@Get("/accepted-uri")
public HttpResponse acceptedUri() {
return HttpResponse.accepted(HttpResponse.uri("http://example.com"));
}

@Get("/created-uri")
public HttpResponse createdUri() {
return HttpResponse.created(HttpResponse.uri("http://test.com"));
}

@Get("/created-body")
public HttpResponse createdBody() {
return HttpResponse.created(new io.micronaut.http.server.netty.java.Foo("blah", 10));
}

@Get("/created-body-uri")
public HttpResponse createdBodyUri() {
return HttpResponse.created(new io.micronaut.http.server.netty.java.Foo("blah", 10), HttpResponse.uri("http://test.com"));
}

@Get("/ok")
public HttpResponse ok() {
return HttpResponse.ok();
}

@Get(value = "/ok-with-body", produces = MediaType.TEXT_PLAIN)
public HttpResponse okWithBody() {
return HttpResponse.ok("some text");
}

@Get(value = "/error-with-body", produces = MediaType.TEXT_PLAIN)
public HttpResponse errorWithBody() {
return HttpResponse.serverError().body("some text");
}

@Get("/ok-with-body-object")
public HttpResponse<io.micronaut.http.server.netty.java.Foo> okWithBodyObject() {
return HttpResponse.ok(new io.micronaut.http.server.netty.java.Foo("blah", 10))
.headers((headers)->
headers.contentType(MediaType.APPLICATION_JSON_TYPE)
);
}

@Get("/status")
public HttpMessage status() {
return HttpResponse.status(HttpStatus.MOVED_PERMANENTLY);
}

@Get("/custom-headers")
public HttpResponse customHeaders() {
return HttpResponse.ok("abc").contentType("text/plain").contentLength(7);
}

@Get("/optional-response/{empty}")
public Optional<HttpResponse> optionalResponse(Boolean empty) {
if (empty) {
return Optional.empty();
} else {
return Optional.of(HttpResponse.ok());
}
}
}
}
Loading

0 comments on commit 713ff1c

Please sign in to comment.