@@ -8,23 +8,20 @@ import android.content.ContentResolver
88import android.content.Context
99import android.net.Uri
1010import androidx.test.platform.app.InstrumentationRegistry
11- import at.bitfire.icsdroid.HttpUtils.toAndroidUri
1211import at.bitfire.icsdroid.test.BuildConfig
1312import at.bitfire.icsdroid.test.R
14- import kotlinx.coroutines.Dispatchers
13+ import io.ktor.client.utils.buildHeaders
14+ import io.ktor.http.ContentType
15+ import io.ktor.http.HttpHeaders
16+ import io.ktor.http.HttpStatusCode
17+ import io.ktor.http.headers
1518import kotlinx.coroutines.runBlocking
16- import kotlinx.coroutines.withContext
17- import okhttp3.MediaType
18- import okhttp3.mockwebserver.MockResponse
19- import okhttp3.mockwebserver.MockWebServer
20- import org.junit.AfterClass
2119import org.junit.Assert.assertArrayEquals
2220import org.junit.Assert.assertEquals
23- import org.junit.BeforeClass
21+ import org.junit.Before
2422import org.junit.Test
2523import java.io.IOException
2624import java.io.InputStream
27- import java.net.HttpURLConnection
2825import java.util.LinkedList
2926
3027class CalendarFetcherTest {
@@ -34,29 +31,24 @@ class CalendarFetcherTest {
3431 val appContext: Context by lazy { InstrumentationRegistry .getInstrumentation().targetContext }
3532 val testContext: Context by lazy { InstrumentationRegistry .getInstrumentation().context }
3633
37- val server = MockWebServer ()
34+ }
3835
39- @BeforeClass
40- @JvmStatic
41- fun setUp () {
42- server.start()
43- }
36+ private lateinit var client: AppHttpClient
4437
45- @AfterClass
46- @JvmStatic
47- fun tearDown () {
48- server.shutdown()
49- }
38+ @Before
39+ fun setUp () {
40+ MockServer .clear()
5041
42+ client = MockServer .httpClient(appContext)
5143 }
5244
5345 @Test
5446 fun testFetchLocal_readsCorrectly () {
5547 val uri = Uri .parse(" ${ContentResolver .SCHEME_ANDROID_RESOURCE } ://${BuildConfig .APPLICATION_ID } /${R .raw.vienna_evolution} " )
5648
5749 var ical: String? = null
58- val fetcher = object : CalendarFetcher (appContext, uri) {
59- override suspend fun onSuccess (data : InputStream , contentType : MediaType ? , eTag : String? , lastModified : Long? , displayName : String? ) {
50+ val fetcher = object : CalendarFetcher (appContext, uri, client ) {
51+ override suspend fun onSuccess (data : InputStream , contentType : ContentType ? , eTag : String? , lastModified : Long? , displayName : String? ) {
6052 ical = data.bufferedReader().use { it.readText() }
6153 }
6254 }
@@ -79,18 +71,21 @@ class CalendarFetcherTest {
7971 }
8072
8173 // create mock response
82- server.enqueue(MockResponse ()
83- .setResponseCode(HttpURLConnection .HTTP_OK )
84- .addHeader(" ETag" , etagCorrect)
85- .addHeader(" Last-Modified" , lastModifiedCorrect)
86- .setBody(icalCorrect))
74+ MockServer .enqueue(
75+ content = icalCorrect,
76+ status = HttpStatusCode .OK ,
77+ headers = buildHeaders {
78+ append(HttpHeaders .ETag , etagCorrect)
79+ append(HttpHeaders .LastModified , lastModifiedCorrect)
80+ }
81+ )
8782
8883 // make request to local mock server
8984 var ical: String? = null
9085 var etag: String? = null
9186 var lastmod: Long? = null
92- val fetcher = object : CalendarFetcher (appContext, server.url( " / " ).toAndroidUri() ) {
93- override suspend fun onSuccess (data : InputStream , contentType : MediaType ? , eTag : String? , lastModified : Long? , displayName : String? ) {
87+ val fetcher = object : CalendarFetcher (appContext, MockServer .uri(), client ) {
88+ override suspend fun onSuccess (data : InputStream , contentType : ContentType ? , eTag : String? , lastModified : Long? , displayName : String? ) {
9489 ical = data.bufferedReader().use { it.readText() }
9590 etag = eTag
9691 lastmod = lastModified
@@ -110,28 +105,37 @@ class CalendarFetcherTest {
110105 fun testFetchNetwork_onRedirectWithLocation () {
111106 // create mock responses:
112107 // 1. redirect with absolute target URL
113- server.enqueue(MockResponse ()
114- .setResponseCode(HttpURLConnection .HTTP_MOVED_TEMP )
115- .addHeader(" Location" , server.url(" new-location/vienna-evolution.ics" )))
108+ MockServer .enqueue(
109+ status = HttpStatusCode .TemporaryRedirect ,
110+ headers = headers {
111+ append(
112+ HttpHeaders .Location , MockServer .uri(" new-location" , " vienna-evolution.ics" ).toString()
113+ )
114+ }
115+ )
116116 // 2. redirect with relative target URL
117- server.enqueue(MockResponse ()
118- .setResponseCode(HttpURLConnection .HTTP_MOVED_TEMP )
119- .addHeader(" Location" , " the-file-is-here" ))
117+ MockServer .enqueue(
118+ status = HttpStatusCode .TemporaryRedirect ,
119+ headers = headers {
120+ append(HttpHeaders .Location , " the-file-is-here" )
121+ }
122+ )
120123 // 3. finally the real resource
121- server.enqueue(MockResponse ()
122- .setResponseCode(HttpURLConnection .HTTP_OK )
123- .setBody(" icalCorrect" ))
124+ MockServer .enqueue(
125+ content = " icalCorrect" ,
126+ status = HttpStatusCode .OK
127+ )
124128
125129 // make initial request to local mock server
126- val baseUrl = server.url( " / " ).toAndroidUri ()
130+ val baseUrl = MockServer .uri ()
127131 var ical: String? = null
128132 val redirects = LinkedList <Uri >()
129- val fetcher = object : CalendarFetcher (appContext, baseUrl) {
130- override suspend fun onRedirect (httpCode : Int , target : Uri ) {
133+ val fetcher = object : CalendarFetcher (appContext, baseUrl, client ) {
134+ override suspend fun onRedirect (httpCode : HttpStatusCode , target : Uri ) {
131135 redirects + = target
132136 super .onRedirect(httpCode, target)
133137 }
134- override suspend fun onSuccess (data : InputStream , contentType : MediaType ? , eTag : String? , lastModified : Long? , displayName : String? ) {
138+ override suspend fun onSuccess (data : InputStream , contentType : ContentType ? , eTag : String? , lastModified : Long? , displayName : String? ) {
135139 ical = data.bufferedReader().use { it.readText() }
136140 }
137141 }
@@ -157,12 +161,11 @@ class CalendarFetcherTest {
157161
158162 @Test
159163 fun testFetchNetwork_onRedirectWithoutLocation () {
160- server.enqueue(MockResponse ()
161- .setResponseCode(HttpURLConnection .HTTP_MOVED_TEMP ))
164+ MockServer .enqueue(status = HttpStatusCode .TemporaryRedirect )
162165
163166 var e: Exception ? = null
164167 runBlocking {
165- object : CalendarFetcher (appContext, server.url( " / " ).toAndroidUri() ) {
168+ object : CalendarFetcher (appContext, MockServer .uri(), client ) {
166169 override suspend fun onError (error : Exception ) {
167170 e = error
168171 }
@@ -174,12 +177,11 @@ class CalendarFetcherTest {
174177
175178 @Test
176179 fun testFetchNetwork_onNotModified () {
177- server.enqueue(MockResponse ()
178- .setResponseCode(HttpURLConnection .HTTP_NOT_MODIFIED ))
180+ MockServer .enqueue(status = HttpStatusCode .NotModified )
179181
180182 var notModified = false
181183 runBlocking {
182- object : CalendarFetcher (appContext, server.url( " / " ).toAndroidUri() ) {
184+ object : CalendarFetcher (appContext, MockServer .uri(), client ) {
183185 override suspend fun onNotModified () {
184186 notModified = true
185187 }
@@ -191,12 +193,11 @@ class CalendarFetcherTest {
191193
192194 @Test
193195 fun testFetchNetwork_onError () {
194- server.enqueue(MockResponse ()
195- .setResponseCode(HttpURLConnection .HTTP_NOT_FOUND ))
196+ MockServer .enqueue(status = HttpStatusCode .NotFound )
196197
197198 var e: Exception ? = null
198199 runBlocking {
199- object : CalendarFetcher (appContext, server.url( " / " ).toAndroidUri() ) {
200+ object : CalendarFetcher (appContext, MockServer .uri(), client ) {
200201 override suspend fun onError (error : Exception ) {
201202 e = error
202203 }
0 commit comments