Skip to content

Commit

Permalink
feat: Set value only when request header does not exist (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Apr 3, 2024
1 parent 6e86fb1 commit c6443f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ kotlin.code.style=official
ksp.incremental=true
ksp.incremental.log=true
group=me.ahoo.coapi
version=1.2.5
version=1.2.6
description=Streamlining HTTP client definition in Spring 6, CoApi provides zero boilerplate code auto-configuration for more convenient and efficient interface calls.
website=https://github.com/Ahoo-Wang/CoApi
issues=https://github.com/Ahoo-Wang/CoApi/issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ open class HeaderSetFilter(
private val headerValueMapper: HeaderValueMapper = HeaderValueMapper.IDENTITY
) : ExchangeFilterFunction {
override fun filter(request: ClientRequest, next: ExchangeFunction): Mono<ClientResponse> {
if (request.headers().containsKey(headerName)) {
return next.exchange(request)
}
return headerValueProvider.getHeaderValue()
.map { headerValue ->
ClientRequest.from(request)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package me.ahoo.coapi.spring.client.reactive.auth

import io.mockk.mockk
import me.ahoo.coapi.spring.client.reactive.auth.ExpirableToken.Companion.jwtToExpirableToken
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Test
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.web.reactive.function.client.ClientRequest
import org.springframework.web.reactive.function.client.ExchangeFunction
Expand All @@ -21,7 +23,7 @@ class BearerTokenFilterTest {
.build()
val jwtToken = JwtFixture.generateToken(Date())
val nextException = ExchangeFunction { request ->
assertThat(request.headers().getFirst("Authorization"), equalTo("Bearer $jwtToken"))
assertThat(request.headers().getFirst(HttpHeaders.AUTHORIZATION), equalTo("Bearer $jwtToken"))
Mono.empty()
}
val tokenProvider = object : ExpirableTokenProvider {
Expand All @@ -34,4 +36,24 @@ class BearerTokenFilterTest {
.test()
.verifyComplete()
}

@Test
fun filter_ContainsKey() {
val jwtToken = JwtFixture.generateToken(Date())
val clientRequest = ClientRequest
.create(HttpMethod.GET, URI.create("http://localhost"))
.headers {
it.setBearerAuth(jwtToken)
}
.build()

val nextException = ExchangeFunction { request ->
Mono.empty()
}
val tokenProvider = mockk<ExpirableTokenProvider>()
val bearerTokenFilter = BearerTokenFilter(tokenProvider)
bearerTokenFilter.filter(clientRequest, nextException)
.test()
.verifyComplete()
}
}

0 comments on commit c6443f0

Please sign in to comment.