Introduce CoWebFilter

This commit introduces the CoWebFilter, an abstract WebFilter
implementation that allows for coroutine usage.

Closes gh-29672
This commit is contained in:
Arjen Poutsma
2023-01-16 11:00:33 +01:00
parent c0e38a2f1e
commit 3a585f8c29
3 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.server
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.mockito.BDDMockito
import org.mockito.Mockito
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest
import org.springframework.web.testfixture.server.MockServerWebExchange
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
/**
* @author Arjen Poutsma
*/
class CoWebFilterTests {
@Test
fun filter() {
val exchange = MockServerWebExchange.from(MockServerHttpRequest.get("https://example.com"))
val chain = Mockito.mock(WebFilterChain::class.java)
BDDMockito.given(chain.filter(exchange)).willReturn(Mono.empty())
val filter = MyCoWebFilter()
val result = filter.filter(exchange, chain)
StepVerifier.create(result)
.verifyComplete()
assertThat(exchange.attributes["foo"]).isEqualTo("bar")
}
}
private class MyCoWebFilter : CoWebFilter() {
override suspend fun filter(exchange: ServerWebExchange, chain: CoWebFilterChain) {
exchange.attributes["foo"] = "bar"
chain.filter(exchange)
}
}