Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Set value only when request header does not exist #37

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
Loading