Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class DefaultUnifiedPushGatewayResolver(
UnifiedPushGatewayResolverResult.NoMatrixGateway
}
} catch (throwable: Throwable) {
if ((throwable as? HttpException)?.code() == HttpURLConnection.HTTP_NOT_FOUND) {
Timber.tag(loggerTag.value).i("Checking for UnifiedPush endpoint yielded 404, using fallback")
val code = (throwable as? HttpException)?.code()
if (code in NoMatrixGatewayResp) {
Timber.tag(loggerTag.value).i("Checking for UnifiedPush endpoint yielded $code, using fallback")
UnifiedPushGatewayResolverResult.NoMatrixGateway
} else {
Timber.tag(loggerTag.value).e(throwable, "Error checking for UnifiedPush endpoint")
Expand All @@ -74,4 +75,14 @@ class DefaultUnifiedPushGatewayResolver(
}
}
}

companion object {
private val NoMatrixGatewayResp = listOf<Int>(
HttpURLConnection.HTTP_UNAUTHORIZED,
HttpURLConnection.HTTP_FORBIDDEN,
HttpURLConnection.HTTP_NOT_FOUND,
HttpURLConnection.HTTP_BAD_METHOD,
HttpURLConnection.HTTP_NOT_ACCEPTABLE
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class DefaultUnifiedPushGatewayUrlResolver(
): String {
return when (gatewayResult) {
is UnifiedPushGatewayResolverResult.Error -> {
// Use previous gateway if any, or the provided one
unifiedPushStore.getPushGateway(instance) ?: gatewayResult.gateway
// Use previous gateway if any, or the default one
unifiedPushStore.getPushGateway(instance)
?: defaultPushGatewayHttpUrlProvider.provide()
}
UnifiedPushGatewayResolverResult.ErrorInvalidUrl,
UnifiedPushGatewayResolverResult.NoMatrixGateway -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class DefaultUnifiedPushGatewayResolverTest {
}

@Test
fun `when a custom url is forbidden (403), Error is returned`() = runTest {
fun `when a custom url is forbidden (403), NoMatrixGateway is returned`() = runTest {
val unifiedPushApiFactory = FakeUnifiedPushApiFactory(
discoveryResponse = {
throw HttpException(Response.error<Unit>(HttpURLConnection.HTTP_FORBIDDEN, "".toResponseBody()))
Expand All @@ -129,6 +129,36 @@ class DefaultUnifiedPushGatewayResolverTest {
)
val result = sut.getGateway("http://custom.url")
assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url")
assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.NoMatrixGateway)
}

@Test
fun `when a custom url is not acceptable (406), NoMatrixGateway is returned`() = runTest {
val unifiedPushApiFactory = FakeUnifiedPushApiFactory(
discoveryResponse = {
throw HttpException(Response.error<Unit>(HttpURLConnection.HTTP_NOT_ACCEPTABLE, "".toResponseBody()))
}
)
val sut = createDefaultUnifiedPushGatewayResolver(
unifiedPushApiFactory = unifiedPushApiFactory
)
val result = sut.getGateway("http://custom.url")
assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url")
assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.NoMatrixGateway)
}

@Test
fun `when a custom url is internal error (500), Error is returned`() = runTest {
val unifiedPushApiFactory = FakeUnifiedPushApiFactory(
discoveryResponse = {
throw HttpException(Response.error<Unit>(HttpURLConnection.HTTP_INTERNAL_ERROR, "".toResponseBody()))
}
)
val sut = createDefaultUnifiedPushGatewayResolver(
unifiedPushApiFactory = unifiedPushApiFactory
)
val result = sut.getGateway("http://custom.url")
assertThat(unifiedPushApiFactory.baseUrlParameter).isEqualTo("http://custom.url")
assertThat(result).isEqualTo(UnifiedPushGatewayResolverResult.Error("http://custom.url/_matrix/push/v1/notify"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DefaultUnifiedPushGatewayUrlResolverTest {
}

@Test
fun `resolve Error returns the url if no current url is available`() {
fun `resolve Error returns the default gateway url if no current url is available`() {
val sut = createDefaultUnifiedPushGatewayUrlResolver(
unifiedPushStore = FakeUnifiedPushStore(
getPushGatewayResult = { instance ->
Expand All @@ -72,7 +72,7 @@ class DefaultUnifiedPushGatewayUrlResolverTest {
gatewayResult = UnifiedPushGatewayResolverResult.Error("aUrl"),
instance = "instance",
)
assertThat(result).isEqualTo("aUrl")
assertThat(result).isEqualTo(FakeDefaultPushGatewayHttpUrlProvider().provide())
}

private fun createDefaultUnifiedPushGatewayUrlResolver(
Expand Down
Loading