Skip to content

Commit c4dd960

Browse files
authored
Fix push gateway with some push provider (Sunup/autopush) (#5741)
* Add more HTTP response code returning NoMatrixGateway Fix Push notifications with Mozilla's autopush that returns 406 * Update gateway resolver tests to match new known errors
1 parent 0cb9d86 commit c4dd960

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushGatewayResolver.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ class DefaultUnifiedPushGatewayResolver(
6464
UnifiedPushGatewayResolverResult.NoMatrixGateway
6565
}
6666
} catch (throwable: Throwable) {
67-
if ((throwable as? HttpException)?.code() == HttpURLConnection.HTTP_NOT_FOUND) {
68-
Timber.tag(loggerTag.value).i("Checking for UnifiedPush endpoint yielded 404, using fallback")
67+
val code = (throwable as? HttpException)?.code()
68+
if (code in NoMatrixGatewayResp) {
69+
Timber.tag(loggerTag.value).i("Checking for UnifiedPush endpoint yielded $code, using fallback")
6970
UnifiedPushGatewayResolverResult.NoMatrixGateway
7071
} else {
7172
Timber.tag(loggerTag.value).e(throwable, "Error checking for UnifiedPush endpoint")
@@ -75,4 +76,14 @@ class DefaultUnifiedPushGatewayResolver(
7576
}
7677
}
7778
}
79+
80+
companion object {
81+
private val NoMatrixGatewayResp = listOf<Int>(
82+
HttpURLConnection.HTTP_UNAUTHORIZED,
83+
HttpURLConnection.HTTP_FORBIDDEN,
84+
HttpURLConnection.HTTP_NOT_FOUND,
85+
HttpURLConnection.HTTP_BAD_METHOD,
86+
HttpURLConnection.HTTP_NOT_ACCEPTABLE
87+
)
88+
}
7889
}

libraries/pushproviders/unifiedpush/src/test/kotlin/io/element/android/libraries/pushproviders/unifiedpush/DefaultUnifiedPushGatewayResolverTest.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class DefaultUnifiedPushGatewayResolverTest {
119119
}
120120

121121
@Test
122-
fun `when a custom url is forbidden (403), Error is returned`() = runTest {
122+
fun `when a custom url is forbidden (403), NoMatrixGateway is returned`() = runTest {
123123
val unifiedPushApiFactory = FakeUnifiedPushApiFactory(
124124
discoveryResponse = {
125125
throw HttpException(Response.error<Unit>(HttpURLConnection.HTTP_FORBIDDEN, "".toResponseBody()))
@@ -130,6 +130,36 @@ class DefaultUnifiedPushGatewayResolverTest {
130130
)
131131
val result = sut.getGateway("http://custom.url")
132132
assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url")
133+
assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.NoMatrixGateway)
134+
}
135+
136+
@Test
137+
fun `when a custom url is not acceptable (406), NoMatrixGateway is returned`() = runTest {
138+
val unifiedPushApiFactory = FakeUnifiedPushApiFactory(
139+
discoveryResponse = {
140+
throw HttpException(Response.error<Unit>(HttpURLConnection.HTTP_NOT_ACCEPTABLE, "".toResponseBody()))
141+
}
142+
)
143+
val sut = createDefaultUnifiedPushGatewayResolver(
144+
unifiedPushApiFactory = unifiedPushApiFactory
145+
)
146+
val result = sut.getGateway("http://custom.url")
147+
assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url")
148+
assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.NoMatrixGateway)
149+
}
150+
151+
@Test
152+
fun `when a custom url is internal error (500), Error is returned`() = runTest {
153+
val unifiedPushApiFactory = FakeUnifiedPushApiFactory(
154+
discoveryResponse = {
155+
throw HttpException(Response.error<Unit>(HttpURLConnection.HTTP_INTERNAL_ERROR, "".toResponseBody()))
156+
}
157+
)
158+
val sut = createDefaultUnifiedPushGatewayResolver(
159+
unifiedPushApiFactory = unifiedPushApiFactory
160+
)
161+
val result = sut.getGateway("http://custom.url")
162+
assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url")
133163
assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.Error("http://custom.url/_matrix/push/v1/notify"))
134164
}
135165

0 commit comments

Comments
 (0)