Skip to content

Commit

Permalink
Add a HEAD route for every GET (micronaut-projects#1693)
Browse files Browse the repository at this point in the history
* Create a HEAD route for each GET. Closes micronaut-projects#1489

* Fix test

* Update AnnotatedFunctionRouteBuilder.java

* Fix non deterministic test
  • Loading branch information
jameskleeh authored and graemerocher committed May 24, 2019
1 parent ce1ea78 commit 10d8928
Show file tree
Hide file tree
Showing 9 changed files with 657 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> met
String functionName = beanDefinition.getValue(FunctionBean.class, String.class).orElse(methodName);
String functionMethod = beanDefinition.getValue(FunctionBean.class, "method", String.class).orElse(null);

UriRoute route = null;
List<UriRoute> routes = new ArrayList<>(2);
MediaType[] consumes = method.getValue(Consumes.class, String[].class).map((types) ->
Arrays.stream(types).map(MediaType::new).toArray(MediaType[]::new)
).orElse(null);
Expand All @@ -109,7 +109,8 @@ public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> met
String argumentName = argumentNames[0];
int argCount = argumentNames.length;

route = POST(functionPath, beanDefinition, method);
UriRoute route = POST(functionPath, beanDefinition, method);
routes.add(route);
if (argCount == 1) {
route.body(argumentName);
}
Expand Down Expand Up @@ -141,7 +142,8 @@ public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> met
}
} else if (Supplier.class.isAssignableFrom(declaringType) && methodName.equals("get")) {
String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
route = GET(functionPath, beanDefinition, method);
routes.add(GET(functionPath, beanDefinition, method));
routes.add(HEAD(functionPath, beanDefinition, method));
} else {
if (StringUtils.isNotEmpty(functionMethod)) {
if (functionMethod.equals(methodName)) {
Expand All @@ -150,10 +152,11 @@ public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> met
if (argCount < 3) {
String functionPath = resolveFunctionPath(methodName, declaringType, functionName);
if (argCount == 0) {
route = GET(functionPath, beanDefinition, method);

routes.add(GET(functionPath, beanDefinition, method));
routes.add(HEAD(functionPath, beanDefinition, method));
} else {
route = POST(functionPath, beanDefinition, method);
UriRoute route = POST(functionPath, beanDefinition, method);
routes.add(route);
if (argCount == 2 || !ClassUtils.isJavaLangType(argumentTypes[0].getType())) {
if (consumes == null) {
consumes = new MediaType[] {MediaType.APPLICATION_JSON_TYPE};
Expand All @@ -168,17 +171,19 @@ public void process(BeanDefinition<?> beanDefinition, ExecutableMethod<?, ?> met
}
}

if (route != null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Created Route to Function: {}", route);
}
if (!routes.isEmpty()) {
for (UriRoute route: routes) {
if (LOG.isDebugEnabled()) {
LOG.debug("Created Route to Function: {}", route);
}

if (consumes != null) {
route.consumes(consumes);
}
if (consumes != null) {
route.consumes(consumes);
}

if (produces != null) {
route.produces(produces);
if (produces != null) {
route.produces(produces);
}
}

ClassLoadingReporter.reportBeanPresent(method.getReturnType().getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ class WebFunctionSpec extends Specification {
embeddedServer.stop()
}

void "test string supplier with HEAD"() {
given:
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer)
RxHttpClient client = embeddedServer.applicationContext.createBean(RxHttpClient, embeddedServer.getURL())

when:
HttpResponse<String> response = client.toBlocking().exchange(HttpRequest.HEAD('/supplier/string'), String)

then:
response.code() == HttpStatus.OK.code
response.body() == null

cleanup:
embeddedServer.stop()
}

void "test pojo supplier"() {
given:
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import java.time.LocalDate
* @since 1.0
*/
class HttpGetSpec extends Specification {

@Shared @AutoCleanup EmbeddedServer embeddedServer =
ApplicationContext.run(EmbeddedServer)

Expand Down Expand Up @@ -95,7 +96,7 @@ class HttpGetSpec extends Specification {

when:
def flowable = Flowable.fromPublisher(client.exchange(
HttpRequest.GET("/get/error")
HttpRequest.GET("/get/error"), Argument.of(String), Argument.of(String)
))

flowable.blockingFirst()
Expand All @@ -104,27 +105,27 @@ class HttpGetSpec extends Specification {
def e = thrown(HttpClientResponseException)
e.message == "Server error"
e.status == HttpStatus.INTERNAL_SERVER_ERROR
e.response.getBody(String).get() == "Server error"

cleanup:
client.stop()
client.close()
}


void "test 500 request with json body"() {
given:
HttpClient client = HttpClient.create(embeddedServer.getURL())

when:
def flowable = Flowable.fromPublisher(client.exchange(
HttpRequest.GET("/get/jsonError")
HttpRequest.GET("/get/jsonError"), Argument.of(String), Argument.of(Map)
))

flowable.blockingFirst()

then:
def e = thrown(HttpClientResponseException)
e.message == "Internal Server Error"
e.message == "{foo=bar}"
e.status == HttpStatus.INTERNAL_SERVER_ERROR

cleanup:
Expand Down
Loading

0 comments on commit 10d8928

Please sign in to comment.