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

Add a HEAD route for every GET #1693

Merged
merged 5 commits into from
May 24, 2019
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 @@ -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