-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECO-5063] feat: integration tests checking agent params
Added tests that checks channel params and rest request headers contains agent information
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
chat-android/src/test/java/com/ably/chat/integration/EmbeddedServer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.ably.chat.integration | ||
|
||
import fi.iki.elonen.NanoHTTPD | ||
import java.io.ByteArrayInputStream | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
|
||
data class Request( | ||
val path: String, | ||
val params: Map<String, String> = emptyMap(), | ||
val headers: Map<String, String> = emptyMap(), | ||
) | ||
|
||
data class Response( | ||
val mimeType: String, | ||
val data: ByteArray, | ||
) | ||
|
||
fun json(json: String): Response = Response( | ||
mimeType = "application/json", | ||
data = json.toByteArray(), | ||
) | ||
|
||
fun interface RequestHandler { | ||
fun handle(request: Request): Response | ||
} | ||
|
||
class EmbeddedServer(port: Int, private val requestHandler: RequestHandler? = null) : NanoHTTPD(port) { | ||
private val _servedRequests = MutableSharedFlow<Request>( | ||
extraBufferCapacity = 1, | ||
onBufferOverflow = BufferOverflow.DROP_OLDEST, | ||
) | ||
|
||
val servedRequests: Flow<Request> = _servedRequests | ||
|
||
override fun serve(session: IHTTPSession): Response { | ||
val request = Request( | ||
path = session.uri, | ||
params = session.parms, | ||
headers = session.headers, | ||
) | ||
_servedRequests.tryEmit(request) | ||
val response = requestHandler?.handle(request) | ||
return response?.toNanoHttp() ?: newFixedLengthResponse("<!DOCTYPE html><title>404</title>") | ||
} | ||
} | ||
|
||
private fun Response.toNanoHttp(): NanoHTTPD.Response = NanoHTTPD.newFixedLengthResponse( | ||
NanoHTTPD.Response.Status.OK, | ||
mimeType, | ||
ByteArrayInputStream(data), | ||
data.size.toLong(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
chat-android/src/test/java/com/ably/chat/integration/RequestHeaderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.ably.chat.integration | ||
|
||
import app.cash.turbine.test | ||
import com.ably.chat.ChatClient | ||
import com.ably.chat.assertWaiter | ||
import fi.iki.elonen.NanoHTTPD | ||
import io.ably.lib.realtime.AblyRealtime | ||
import io.ably.lib.types.ClientOptions | ||
import java.util.UUID | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.AfterClass | ||
import org.junit.Assert | ||
import org.junit.BeforeClass | ||
import org.junit.Test | ||
|
||
class RequestHeaderTest { | ||
|
||
@Test | ||
fun `should use additional agents in Realtime wrapper SDK client calls`() = runTest { | ||
val ablyRealtime = createAblyRealtime() | ||
val chatClient = ChatClient(ablyRealtime) | ||
val roomId = UUID.randomUUID().toString() | ||
|
||
server.servedRequests.test { | ||
chatClient.rooms.get(roomId).messages.get() | ||
val agents = awaitItem().headers["ably-agent"]?.split(" ") ?: setOf() | ||
Assert.assertTrue( | ||
agents.contains("chat-kotlin/${com.ably.chat.BuildConfig.APP_VERSION}"), | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
const val PORT = 27332 | ||
lateinit var server: EmbeddedServer | ||
|
||
@JvmStatic | ||
@BeforeClass | ||
fun setUp() = runTest { | ||
server = EmbeddedServer(PORT) { | ||
when (it.path) { | ||
"/time" -> json("[1739551931167]") | ||
else -> json("[]") | ||
} | ||
} | ||
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, true) | ||
assertWaiter { server.wasStarted() } | ||
} | ||
|
||
@JvmStatic | ||
@AfterClass | ||
fun tearDown() { | ||
server.stop() | ||
} | ||
} | ||
} | ||
|
||
private fun createAblyRealtime(): AblyRealtime { | ||
val options = ClientOptions("xxxxx:yyyyyyy").apply { | ||
port = RequestHeaderTest.PORT | ||
useBinaryProtocol = false | ||
realtimeHost = "localhost" | ||
restHost = "localhost" | ||
tls = false | ||
autoConnect = false | ||
} | ||
|
||
return AblyRealtime(options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters