Skip to content

Commit a5b94f3

Browse files
committed
Use HttpRequest.getMethodValue
This commit uses the newly introduced HttpRequest.getMethodValue in the webflux module. Issue: SPR-15545
1 parent 630fc19 commit a5b94f3

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/DispatcherHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -120,7 +120,7 @@ protected void initStrategies(ApplicationContext context) {
120120
public Mono<Void> handle(ServerWebExchange exchange) {
121121
if (logger.isDebugEnabled()) {
122122
ServerHttpRequest request = exchange.getRequest();
123-
logger.debug("Processing " + request.getMethod() + " request for [" + request.getURI() + "]");
123+
logger.debug("Processing " + request.getMethodValue() + " request for [" + request.getURI() + "]");
124124
}
125125
return Flux.fromIterable(this.handlerMappings)
126126
.concatMap(mapping -> mapping.getHandler(exchange))

spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public Mono<Void> handle(ServerWebExchange exchange) {
283283
}))
284284
.flatMap(resource -> {
285285
try {
286-
if (HttpMethod.OPTIONS.equals(exchange.getRequest().getMethod())) {
286+
if (HttpMethod.OPTIONS.matches(exchange.getRequest().getMethodValue())) {
287287
exchange.getResponse().getHeaders().add("Allow", "GET,HEAD,OPTIONS");
288288
return Mono.empty();
289289
}
@@ -323,7 +323,7 @@ public Mono<Void> handle(ServerWebExchange exchange) {
323323
}
324324

325325
// Content phase
326-
if (HttpMethod.HEAD.equals(exchange.getRequest().getMethod())) {
326+
if (HttpMethod.HEAD.matches(exchange.getRequest().getMethodValue())) {
327327
setHeaders(exchange, resource, mediaType);
328328
exchange.getResponse().getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
329329
logger.trace("HEAD request - skipping content");

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/RequestMethodsRequestCondition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -107,7 +107,7 @@ public RequestMethodsRequestCondition getMatchingCondition(ServerWebExchange exc
107107
return matchPreFlight(exchange.getRequest());
108108
}
109109
if (getMethods().isEmpty()) {
110-
if (RequestMethod.OPTIONS.name().equals(exchange.getRequest().getMethod().name())) {
110+
if (RequestMethod.OPTIONS.name().equals(exchange.getRequest().getMethodValue())) {
111111
return null; // No implicit match for OPTIONS (we handle it)
112112
}
113113
return this;

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfoHandlerMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ protected HandlerMethod handleNoMatch(Set<RequestMappingInfo> infos, String look
218218
ServerHttpRequest request = exchange.getRequest();
219219

220220
if (helper.hasMethodsMismatch()) {
221-
HttpMethod httpMethod = request.getMethod();
221+
String httpMethod = request.getMethodValue();
222222
Set<HttpMethod> methods = helper.getAllowedMethods();
223-
if (HttpMethod.OPTIONS.matches(httpMethod.name())) {
223+
if (HttpMethod.OPTIONS.matches(httpMethod)) {
224224
HttpOptionsHandler handler = new HttpOptionsHandler(methods);
225225
return new HandlerMethod(handler, HTTP_OPTIONS_HANDLE_METHOD);
226226
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.reactive.function.server;
18+
19+
import okhttp3.OkHttpClient;
20+
import okhttp3.Request;
21+
import okhttp3.Response;
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.*;
25+
26+
/**
27+
* @author Arjen Poutsma
28+
*/
29+
public class InvalidHttpMethodIntegrationTests extends AbstractRouterFunctionIntegrationTests {
30+
31+
@Override
32+
protected RouterFunction<?> routerFunction() {
33+
return RouterFunctions.route(RequestPredicates.GET("/"),
34+
request -> ServerResponse.ok().syncBody("FOO"))
35+
.andRoute(RequestPredicates.all(),
36+
request -> ServerResponse.ok().syncBody("BAR"));
37+
}
38+
39+
@Test
40+
public void invalidHttpMethod() throws Exception {
41+
OkHttpClient client = new OkHttpClient();
42+
43+
Request request = new Request.Builder()
44+
.method("BAZ", null)
45+
.url("http://localhost:" + port + "/")
46+
.build();
47+
48+
try (Response response = client.newCall(request).execute()) {
49+
assertEquals("BAR", response.body().string());
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)