From afce72bbe1c3d3c49c1df4b7f4d28273eb135173 Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Fri, 23 Oct 2020 19:44:13 +0200 Subject: [PATCH] Add tests for AuthInterceptor --- .../readrops/api/utils/AuthInterceptorTest.kt | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 api/src/androidTest/java/com/readrops/api/utils/AuthInterceptorTest.kt diff --git a/api/src/androidTest/java/com/readrops/api/utils/AuthInterceptorTest.kt b/api/src/androidTest/java/com/readrops/api/utils/AuthInterceptorTest.kt new file mode 100644 index 00000000..ea425a05 --- /dev/null +++ b/api/src/androidTest/java/com/readrops/api/utils/AuthInterceptorTest.kt @@ -0,0 +1,54 @@ +package com.readrops.api.utils + +import com.readrops.api.services.freshrss.FreshRSSCredentials +import junit.framework.TestCase.assertEquals +import junit.framework.TestCase.assertNull +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.mockwebserver.MockResponse +import okhttp3.mockwebserver.MockWebServer +import org.junit.After +import org.junit.Before +import org.junit.Test + +class AuthInterceptorTest { + + private val interceptor = AuthInterceptor() + private val mockServer = MockWebServer() + private lateinit var okHttpClient: OkHttpClient + + @Before + fun before() { + okHttpClient = OkHttpClient.Builder().addInterceptor(interceptor).build() + mockServer.start(8080) + } + + @After + fun tearDown() { + mockServer.close() + } + + @Test + fun credentialsUrlTest() { + mockServer.enqueue(MockResponse()) + interceptor.credentials = FreshRSSCredentials("token", "http://localhost:8080/rss") + + okHttpClient.newCall(Request.Builder().url(mockServer.url("/url")).build()).execute() + val request = mockServer.takeRequest() + + assertEquals(request.requestUrl.toString(), "http://localhost:8080/rss/url") + assertEquals(request.headers["Authorization"], "GoogleLogin auth=token") + } + + @Test + fun nullCredentialsTest() { + mockServer.enqueue(MockResponse()) + interceptor.credentials = null + + okHttpClient.newCall(Request.Builder().url(mockServer.url("/url")).build()).execute() + val request = mockServer.takeRequest() + + assertEquals(request.requestUrl.toString(), "http://localhost:8080/url") + assertNull(request.headers["Authorization"]) + } +} \ No newline at end of file