Add test on UnifiedPushGatewayResolver

This commit is contained in:
Benoit Marty 2024-05-22 09:23:24 +02:00
parent 7bda5fdee0
commit 50004e37cf
4 changed files with 227 additions and 10 deletions

View file

@ -0,0 +1,37 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.pushproviders.unifiedpush
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.network.RetrofitFactory
import io.element.android.libraries.pushproviders.unifiedpush.network.UnifiedPushApi
import javax.inject.Inject
interface UnifiedPushApiFactory {
fun create(baseUrl: String): UnifiedPushApi
}
@ContributesBinding(AppScope::class)
class DefaultUnifiedPushApiFactory @Inject constructor(
private val retrofitFactory: RetrofitFactory,
) : UnifiedPushApiFactory {
override fun create(baseUrl: String): UnifiedPushApi {
return retrofitFactory.create(baseUrl)
.create(UnifiedPushApi::class.java)
}
}

View file

@ -17,28 +17,25 @@
package io.element.android.libraries.pushproviders.unifiedpush
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.network.RetrofitFactory
import io.element.android.libraries.pushproviders.unifiedpush.network.UnifiedPushApi
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.net.URL
import javax.inject.Inject
class UnifiedPushGatewayResolver @Inject constructor(
private val retrofitFactory: RetrofitFactory,
private val unifiedPushApiFactory: UnifiedPushApiFactory,
private val coroutineDispatchers: CoroutineDispatchers,
) {
suspend fun getGateway(endpoint: String): String? {
val gateway = UnifiedPushConfig.DEFAULT_PUSH_GATEWAY_HTTP_URL
val url = URL(endpoint)
val port = if (url.port != -1) ":${url.port}" else ""
val customBase = "${url.protocol}://${url.host}$port"
val customUrl = "$customBase/_matrix/push/v1/notify"
Timber.i("Testing $customUrl")
try {
val url = URL(endpoint)
val port = if (url.port != -1) ":${url.port}" else ""
val customBase = "${url.protocol}://${url.host}$port"
val customUrl = "$customBase/_matrix/push/v1/notify"
Timber.i("Testing $customUrl")
return withContext(coroutineDispatchers.io) {
val api = retrofitFactory.create(customBase)
.create(UnifiedPushApi::class.java)
val api = unifiedPushApiFactory.create(customBase)
try {
val discoveryResponse = api.discover()
if (discoveryResponse.unifiedpush.gateway == "matrix") {