320x100
kotlin 으로 작성되었습니다.
@Component
class CustomGatewayFilterFactory : AbstractGatewayFilterFactory<Any>() {
override fun apply(config: Any?) = GatewayFilter { exchange: ServerWebExchange, chain: GatewayFilterChain ->
var request = exchange.request
val value = request.headers["My-Header"]?.firstOrNull()
request = request.mutate()
.headers { it.add("Test-Header", AbstractGatewayFilterFactory) }
.build()
chain.filter(exchange.mutate().request(request).build())
}
}
My-Header 에 있는 값을 Test-Header value로 추가하는 필터
@ExtendWith(MockKExtension::class)
class CustomGatewayFilterFactoryTest {
private lateinit var filterFactory: CustomGatewayFilterFactory
@BeforeEach
fun setup() {
filterFactory = CustomGatewayFilterFactory()
}
@Test
fun `custom filter 테스트`() {
// given
val value = "test"
val exchange = MockServerWebExchange.from(
MockServerHttpRequest.get("/test")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header("My-Header", value)
.build()
)
val chain = mockk<GatewayFilterChain>()
// mock
every { chain.filter(any()) } returns Mono.empty()
// when
val filter = filterFactory.apply(mockk(relaxed = true))
filter.filter(exchange, chain)
// then
assertEquals(value, exchange.request.headers["Test-Header"]?.firstOrNull())
}
}
320x100
'Back-End (web)' 카테고리의 다른 글
[intellij] google code 포매터 적용 (0) | 2020.07.11 |
---|---|
[Spring] GatewayFilterFactory add Order (0) | 2020.07.02 |
[kotlin][coroutine] awaitRetrieve (0) | 2020.04.05 |
[JUnit5] @ParameterizedTest (0) | 2019.12.15 |
[IntellJ] Kotlin 코드를 java 코드로 확인하기 (0) | 2019.12.09 |
댓글