Skip to content

Commit 7eff66d

Browse files
quantranhong1999Arsnael
authored andcommitted
JAMES-3539 WebPushClient should accept more flexible 20x codes from Push server
It seems that some push gateways, like MSGraph, are using return codes 200 and 202 as well.
1 parent bbe9ffd commit 7eff66d

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/pushsubscription/WebPushClient.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class DefaultWebPushClient @Inject()(configuration: PushClientConfiguration) ext
128128
private def afterHTTPResponseHandler(httpResponse: HttpClientResponse, dataBuf: ByteBufMono): Mono[Void] =
129129
Mono.just(httpResponse.status())
130130
.flatMap {
131-
case HttpResponseStatus.CREATED => Mono.empty()
131+
case HttpResponseStatus.OK | HttpResponseStatus.CREATED | HttpResponseStatus.ACCEPTED => Mono.empty()
132132
case HttpResponseStatus.BAD_REQUEST => preProcessingData(dataBuf)
133133
.flatMap(string => Mono.error(WebPushInvalidRequestException(string)))
134134
case statusCode: HttpResponseStatus => preProcessingData(dataBuf)

server/protocols/jmap-rfc-8621/src/test/scala/org/apache/james/jmap/pushsubscription/WebPushClientContract.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ package org.apache.james.jmap.pushsubscription
2121

2222
import java.net.URL
2323
import java.nio.charset.StandardCharsets
24+
import java.time.Clock
25+
import java.util.UUID
2426

2527
import org.apache.james.jmap.api.model.PushSubscriptionServerURL
2628
import org.apache.james.jmap.pushsubscription.WebPushClientTestFixture.PUSH_REQUEST_SAMPLE
@@ -31,6 +33,7 @@ import org.junit.jupiter.params.provider.ValueSource
3133
import org.mockserver.integration.ClientAndServer
3234
import org.mockserver.model.HttpRequest.request
3335
import org.mockserver.model.HttpResponse.response
36+
import org.mockserver.model.NottableString.string
3437
import org.mockserver.verify.VerificationTimes
3538
import reactor.core.scala.publisher.SMono
3639

@@ -110,6 +113,36 @@ trait WebPushClientContract {
110113
VerificationTimes.atLeast(1))
111114
}
112115

116+
@ParameterizedTest
117+
@ValueSource(ints = Array(200, 201, 202))
118+
def pushClientShouldAcceptFlexible20xCodes(httpStatusCode: Int, pushServer: ClientAndServer): Unit = {
119+
pushServer
120+
.when(request
121+
.withPath("/pushh")
122+
.withMethod("POST")
123+
.withHeader(string("Content-type"), string("application/json charset=utf-8"))
124+
.withHeader(string("Urgency"))
125+
.withHeader(string("Topic"))
126+
.withHeader(string("TTL")))
127+
.respond(response
128+
.withStatusCode(httpStatusCode)
129+
.withHeader("Location", String.format("https://push.example.net/message/%s", UUID.randomUUID))
130+
.withHeader("Date", Clock.systemUTC.toString)
131+
.withBody(UUID.randomUUID.toString))
132+
133+
assertThatCode(() => SMono.fromPublisher(testee.push(PushSubscriptionServerURL.from(s"${pushServerBaseUrl.toString}/pushh").get, PUSH_REQUEST_SAMPLE))
134+
.block())
135+
.doesNotThrowAnyException()
136+
137+
pushServer.verify(request()
138+
.withPath("/pushh")
139+
.withHeader("TTL", "15")
140+
.withHeader("Topic", "topicabc")
141+
.withHeader("Urgency", "High")
142+
.withBody(new String(PUSH_REQUEST_SAMPLE.payload, StandardCharsets.UTF_8)),
143+
VerificationTimes.once)
144+
}
145+
113146
@Test
114147
def pushRequestShouldParserErrorResponseFromPushServerWhenFail(pushServer: ClientAndServer): Unit = {
115148
pushServer

0 commit comments

Comments
 (0)