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

Handle Consumes only for methods with body #484

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 @@ -75,45 +75,65 @@ protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Ex

@Override
public void registerHttpEndpoint(HttpEndpointModel model) {
RequestMappingInfo info = asRequestMappingInfo(model);
List<RequestMappingInfo> requestMappingInfos = asRequestMappingInfo(model);
Method m = ReflectionHelper.findMethod(SpringBootPlatformHttpConsumer.class, "service",
HttpServletRequest.class, HttpServletResponse.class);
registerMapping(info, model.getConsumer(), m);
for (RequestMappingInfo info : requestMappingInfos) {
registerMapping(info, model.getConsumer(), m);
}
}

@Override
public void unregisterHttpEndpoint(HttpEndpointModel model) {
// noop
}

private RequestMappingInfo asRequestMappingInfo(HttpEndpointModel model) {
private List<RequestMappingInfo> asRequestMappingInfo(HttpEndpointModel model) {
List<RequestMappingInfo> result = new ArrayList<>();

// allowed methods from model or endpoint
List<RequestMethod> methods = new ArrayList<>();
String verbs = model.getVerbs();
if (verbs == null && model.getConsumer() != null) {
PlatformHttpEndpoint endpoint = (PlatformHttpEndpoint) model.getConsumer().getEndpoint();
verbs = endpoint.getHttpMethodRestrict();

for (RequestMethod rm : RequestMethod.values()) {
createRequestMappingInfo(model, rm, result);
}
}
if (verbs != null) {
for (String v : model.getVerbs().split(",")) {
RequestMethod rm = RequestMethod.resolve(v);
methods.add(rm);

createRequestMappingInfo(model, rm, result);
}
}

return result;
}

private void createRequestMappingInfo(HttpEndpointModel model, RequestMethod rm, List<RequestMappingInfo> result) {
RequestMethod[] methods = new RequestMethod[]{};
if (rm != null) {
methods = new RequestMethod[]{rm};
}

RequestMappingInfo.Builder info = RequestMappingInfo
.paths(model.getUri())
.methods(methods.toArray(new RequestMethod[0]))
.methods(methods)
.options(this.getBuilderConfiguration());

if (model.getConsumes() != null) {
if (model.getConsumes() != null
&& (RequestMethod.POST.name().equals(rm.name()) || RequestMethod.PUT.name().equals(rm.name()) || RequestMethod.PATCH.name().equals(rm.name()))) {
info.consumes(model.getConsumes().split(","));
}
if (model.getProduces() != null) {
info.produces(model.getProduces().split(","));
}

return info.build();
result.add(info.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public void configure() {
.bindingMode(RestBindingMode.auto)
.to("direct:rest");

rest("rest").post("/test")
.consumes("application/json,application/xml")
.produces("application/json,application/xml")
.bindingMode(RestBindingMode.auto)
.to("direct:rest");

from("direct:rest")
.setBody(simple("Hello"));

Expand Down Expand Up @@ -166,7 +172,7 @@ public void testLoad() throws Exception {
public void nonSupportedContentType() {
RestAssured.given()
.header("Content-Type", "notSupported")
.get("rest/test")
.post("rest/test")
.then()
.statusCode(415);
}
Expand All @@ -182,6 +188,19 @@ public void oneContentType() {
.body(is("Hello"));
}

@Test
public void noContentTypeOkForGet() {
String body = RestAssured.given()
.get("rest/test")
.then()
.statusCode(200)
.extract()
.body()
.asString();

Assertions.assertThat(body).contains("Hello");
}

@Test
public void nonSupportedAccept() {
RestAssured.given()
Expand Down