|
16 | 16 |
|
17 | 17 | package org.springframework.web.filter.reactive; |
18 | 18 |
|
19 | | -import java.util.Optional; |
| 19 | +import java.time.Duration; |
20 | 20 |
|
21 | 21 | import org.hamcrest.Matchers; |
22 | 22 | import org.junit.Test; |
|
27 | 27 | import org.springframework.http.HttpMethod; |
28 | 28 | import org.springframework.http.MediaType; |
29 | 29 | import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; |
| 30 | +import org.springframework.mock.http.server.reactive.test.MockServerWebExchange; |
30 | 31 | import org.springframework.web.server.ServerWebExchange; |
31 | 32 | import org.springframework.web.server.WebFilterChain; |
32 | 33 |
|
33 | 34 | import static org.junit.Assert.assertEquals; |
34 | 35 | import static org.junit.Assert.assertThat; |
35 | 36 |
|
36 | 37 | /** |
37 | | - * Tests for {@link HiddenHttpMethodFilter} |
38 | | - * |
| 38 | + * Tests for {@link HiddenHttpMethodFilter}. |
39 | 39 | * @author Greg Turnquist |
| 40 | + * @author Rossen Stoyanchev |
40 | 41 | */ |
41 | 42 | public class HiddenHttpMethodFilterTests { |
42 | 43 |
|
43 | 44 | private final HiddenHttpMethodFilter filter = new HiddenHttpMethodFilter(); |
44 | 45 |
|
| 46 | + private final TestWebFilterChain filterChain = new TestWebFilterChain(); |
| 47 | + |
| 48 | + |
45 | 49 | @Test |
46 | 50 | public void filterWithParameter() { |
47 | | - ServerWebExchange mockExchange = createExchange(Optional.of("DELETE")); |
48 | | - |
49 | | - WebFilterChain filterChain = exchange -> { |
50 | | - assertEquals("Invalid method", HttpMethod.DELETE, exchange.getRequest().getMethod()); |
51 | | - return Mono.empty(); |
52 | | - }; |
| 51 | + postForm("_method=DELETE").block(Duration.ZERO); |
| 52 | + assertEquals(HttpMethod.DELETE, this.filterChain.getHttpMethod()); |
| 53 | + } |
53 | 54 |
|
54 | | - StepVerifier.create(filter.filter(mockExchange, filterChain)) |
55 | | - .expectComplete() |
56 | | - .verify(); |
| 55 | + @Test |
| 56 | + public void filterWithNoParameter() { |
| 57 | + postForm("").block(Duration.ZERO); |
| 58 | + assertEquals(HttpMethod.POST, this.filterChain.getHttpMethod()); |
57 | 59 | } |
58 | 60 |
|
59 | 61 | @Test |
60 | | - public void filterWithInvalidParameter() { |
61 | | - ServerWebExchange mockExchange = createExchange(Optional.of("INVALID")); |
| 62 | + public void filterWithEmptyStringParameter() { |
| 63 | + postForm("_method=").block(Duration.ZERO); |
| 64 | + assertEquals(HttpMethod.POST, this.filterChain.getHttpMethod()); |
| 65 | + } |
62 | 66 |
|
63 | | - WebFilterChain filterChain = exchange -> Mono.empty(); |
| 67 | + @Test |
| 68 | + public void filterWithDifferentMethodParam() { |
| 69 | + this.filter.setMethodParamName("_foo"); |
| 70 | + postForm("_foo=DELETE").block(Duration.ZERO); |
| 71 | + assertEquals(HttpMethod.DELETE, this.filterChain.getHttpMethod()); |
| 72 | + } |
64 | 73 |
|
65 | | - StepVerifier.create(filter.filter(mockExchange, filterChain)) |
| 74 | + @Test |
| 75 | + public void filterWithInvalidMethodValue() { |
| 76 | + StepVerifier.create(postForm("_method=INVALID")) |
66 | 77 | .consumeErrorWith(error -> { |
67 | 78 | assertThat(error, Matchers.instanceOf(IllegalArgumentException.class)); |
68 | | - assertEquals(error.getMessage(), "HttpMethod 'INVALID' is not supported"); |
| 79 | + assertEquals(error.getMessage(), "HttpMethod 'INVALID' not supported"); |
69 | 80 | }) |
70 | 81 | .verify(); |
71 | 82 | } |
72 | 83 |
|
73 | 84 | @Test |
74 | | - public void filterWithNoParameter() { |
75 | | - ServerWebExchange mockExchange = createExchange(Optional.empty()); |
| 85 | + public void filterWithHttpPut() { |
76 | 86 |
|
77 | | - WebFilterChain filterChain = exchange -> { |
78 | | - assertEquals("Invalid method", HttpMethod.POST, exchange.getRequest().getMethod()); |
79 | | - return Mono.empty(); |
80 | | - }; |
| 87 | + ServerWebExchange exchange = MockServerHttpRequest.put("/") |
| 88 | + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
| 89 | + .body("_method=DELETE") |
| 90 | + .toExchange(); |
81 | 91 |
|
82 | | - StepVerifier.create(filter.filter(mockExchange, filterChain)) |
83 | | - .expectComplete() |
84 | | - .verify(); |
| 92 | + this.filter.filter(exchange, this.filterChain).block(Duration.ZERO); |
| 93 | + assertEquals(HttpMethod.PUT, this.filterChain.getHttpMethod()); |
85 | 94 | } |
86 | 95 |
|
87 | | - @Test |
88 | | - public void filterWithEmptyStringParameter() { |
89 | | - ServerWebExchange mockExchange = createExchange(Optional.of("")); |
90 | | - |
91 | | - WebFilterChain filterChain = exchange -> { |
92 | | - assertEquals("Invalid method", HttpMethod.POST, exchange.getRequest().getMethod()); |
93 | | - return Mono.empty(); |
94 | | - }; |
95 | | - |
96 | | - StepVerifier.create(filter.filter(mockExchange, filterChain)) |
97 | | - .expectComplete() |
98 | | - .verify(); |
99 | | - } |
100 | | - |
101 | | - @Test |
102 | | - public void filterWithDifferentMethodParam() { |
103 | | - ServerWebExchange mockExchange = createExchange("_foo", Optional.of("DELETE")); |
104 | 96 |
|
105 | | - WebFilterChain filterChain = exchange -> { |
106 | | - assertEquals("Invalid method", HttpMethod.DELETE, exchange.getRequest().getMethod()); |
107 | | - return Mono.empty(); |
108 | | - }; |
| 97 | + private Mono<Void> postForm(String body) { |
109 | 98 |
|
110 | | - filter.setMethodParam("_foo"); |
| 99 | + MockServerWebExchange exchange = MockServerHttpRequest.post("/") |
| 100 | + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
| 101 | + .body(body) |
| 102 | + .toExchange(); |
111 | 103 |
|
112 | | - StepVerifier.create(filter.filter(mockExchange, filterChain)) |
113 | | - .expectComplete() |
114 | | - .verify(); |
| 104 | + return this.filter.filter(exchange, this.filterChain); |
115 | 105 | } |
116 | 106 |
|
117 | | - @Test |
118 | | - public void filterWithoutPost() { |
119 | | - ServerWebExchange mockExchange = createExchange(Optional.of("DELETE")).mutate() |
120 | | - .request(builder -> builder.method(HttpMethod.PUT)) |
121 | | - .build(); |
122 | 107 |
|
123 | | - WebFilterChain filterChain = exchange -> { |
124 | | - assertEquals("Invalid method", HttpMethod.PUT, exchange.getRequest().getMethod()); |
125 | | - return Mono.empty(); |
126 | | - }; |
| 108 | + private static class TestWebFilterChain implements WebFilterChain { |
127 | 109 |
|
128 | | - StepVerifier.create(filter.filter(mockExchange, filterChain)) |
129 | | - .expectComplete() |
130 | | - .verify(); |
131 | | - } |
132 | | - |
133 | | - private ServerWebExchange createExchange(Optional<String> optionalMethod) { |
134 | | - return createExchange("_method", optionalMethod); |
135 | | - } |
| 110 | + private HttpMethod httpMethod; |
136 | 111 |
|
137 | | - private ServerWebExchange createExchange(String methodName, Optional<String> optionalBody) { |
138 | | - MockServerHttpRequest.BodyBuilder builder = MockServerHttpRequest |
139 | | - .post("/hotels") |
140 | | - .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE); |
141 | 112 |
|
142 | | - MockServerHttpRequest request = optionalBody |
143 | | - .map(method -> builder.body(methodName + "=" + method)) |
144 | | - .orElse(builder.build()); |
| 113 | + public HttpMethod getHttpMethod() { |
| 114 | + return this.httpMethod; |
| 115 | + } |
145 | 116 |
|
146 | | - return request.toExchange(); |
| 117 | + @Override |
| 118 | + public Mono<Void> filter(ServerWebExchange exchange) { |
| 119 | + this.httpMethod = exchange.getRequest().getMethod(); |
| 120 | + return Mono.empty(); |
| 121 | + } |
147 | 122 | } |
148 | 123 |
|
149 | 124 | } |
0 commit comments