From 215cd2758b0fc2b3575f7e0f4b2bbc8a710ad420 Mon Sep 17 00:00:00 2001 From: LooKeR Date: Sun, 14 Jul 2024 11:46:14 +0530 Subject: [PATCH] Test KtorDownloader --- .../com/looker/network/KtorDownloaderTest.kt | 92 ++++++++++++++++++- 1 file changed, 89 insertions(+), 3 deletions(-) diff --git a/core/network/src/test/kotlin/com/looker/network/KtorDownloaderTest.kt b/core/network/src/test/kotlin/com/looker/network/KtorDownloaderTest.kt index bf9aa257..5173e96b 100644 --- a/core/network/src/test/kotlin/com/looker/network/KtorDownloaderTest.kt +++ b/core/network/src/test/kotlin/com/looker/network/KtorDownloaderTest.kt @@ -1,18 +1,104 @@ package com.looker.network +import io.ktor.client.engine.mock.MockEngine +import io.ktor.client.engine.mock.respond +import io.ktor.client.engine.mock.respondError +import io.ktor.client.engine.mock.respondOk +import io.ktor.client.plugins.ConnectTimeoutException +import io.ktor.client.plugins.SocketTimeoutException +import io.ktor.http.HttpStatusCode +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.Test +import java.io.File +import kotlin.test.assertEquals +import kotlin.test.assertIs class KtorDownloaderTest { + private val engine = MockEngine { request -> + when (request.url.host) { + "success.com" -> respondOk("success") + "notfound.com" -> respondError(HttpStatusCode.NotFound) + "connection.com" -> throw ConnectTimeoutException(request) + "socket.com" -> throw SocketTimeoutException(request) + "notmodified.com" -> respond("", HttpStatusCode.NotModified) + "authenticate.com" -> respondError(HttpStatusCode.Unauthorized) + + else -> TODO("Not implemented for: ${request.url.host}") + } + } + + private val dispatcher = StandardTestDispatcher() + + private val downloader = KtorDownloader(engine, dispatcher) + + @Test + fun `head call success`() = runTest(dispatcher) { + val response = downloader.headCall("https://success.com") + assertIs(response) + } + + @Test + fun `head call if path not found`() = runTest(dispatcher) { + val response = downloader.headCall("https://notfound.com") + assertIs(response) + } + + @Test + fun `save text to file success`() = runTest(dispatcher) { + val file = File.createTempFile("test", "success") + val response = downloader.downloadToFile("https://success.com", target = file) + assertIs(response) + assertEquals("success", file.readText()) + } + + @Test + fun `save text to read-only file`() = runTest(dispatcher) { + val file = File.createTempFile("test", "success") + file.setReadOnly() + val response = downloader.downloadToFile("https://success.com", target = file) + assertIs(response) + } + + @Test + fun `save text to file with slow connection`() = runTest(dispatcher) { + val file = File.createTempFile("test", "success") + val response = downloader.downloadToFile("https://connection.com", target = file) + assertIs(response) + } + @Test - fun setProxy() { + fun `save text to file with socket error`() = runTest(dispatcher) { + val file = File.createTempFile("test", "success") + val response = downloader.downloadToFile("https://socket.com", target = file) + assertIs(response) } @Test - fun headCall() { + fun `save text to file if not modifier`() = runTest(dispatcher) { + val file = File.createTempFile("test", "success") + val response = downloader.downloadToFile( + "https://notmodified.com", + target = file, + headers = { + ifModifiedSince("") + } + ) + assertIs(response) + assertEquals("", file.readText()) } @Test - fun downloadToFile() { + fun `save text to file with wrong authentication`() = runTest(dispatcher) { + val file = File.createTempFile("test", "success") + val response = downloader.downloadToFile( + "https://authenticate.com", + target = file, + headers = { + authentication("iamlooker", "sneakypeaky") + } + ) + assertIs(response) } }