|
| 1 | +package at.bitfire.icsdroid.net |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.test.core.app.ApplicationProvider |
| 5 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 6 | +import at.bitfire.icsdroid.AppHttpClient |
| 7 | +import at.bitfire.icsdroid.MockServer |
| 8 | +import io.ktor.client.request.get |
| 9 | +import io.ktor.client.statement.HttpResponse |
| 10 | +import io.ktor.http.HttpHeaders |
| 11 | +import io.ktor.http.HttpStatusCode |
| 12 | +import kotlinx.coroutines.runBlocking |
| 13 | +import org.junit.After |
| 14 | +import org.junit.Before |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.junit.Assert.assertEquals |
| 18 | +import org.junit.Assert.assertFalse |
| 19 | +import org.junit.Assert.assertNotNull |
| 20 | + |
| 21 | +@RunWith(AndroidJUnit4::class) |
| 22 | +class TestAppHttpClient { |
| 23 | + |
| 24 | + private val context = ApplicationProvider.getApplicationContext<Context>() |
| 25 | + private lateinit var client: AppHttpClient |
| 26 | + |
| 27 | + @Before |
| 28 | + fun setUp() { |
| 29 | + MockServer.clear() |
| 30 | + client = MockServer.httpClient(context) |
| 31 | + } |
| 32 | + |
| 33 | + @After |
| 34 | + fun tearDown() { |
| 35 | + MockServer.clear() |
| 36 | + } |
| 37 | + |
| 38 | + // Verifies that no Accept-Charset header is sent by default |
| 39 | + @Test |
| 40 | + fun request_doesNotContainAcceptCharsetHeader() = runBlocking { |
| 41 | + // enqueue a simple 200 response |
| 42 | + MockServer.enqueue(content = "ok", status = HttpStatusCode.OK) |
| 43 | + |
| 44 | + // perform a GET request to the mock server |
| 45 | + val uri = MockServer.uri("test") |
| 46 | + val response: HttpResponse = client.httpClient.get(uri.toString()) |
| 47 | + |
| 48 | + assertEquals(HttpStatusCode.OK, response.status) |
| 49 | + |
| 50 | + // retrieve the headers recorded by the mock server |
| 51 | + val headers = MockServer.lastRequestHeaders |
| 52 | + |
| 53 | + // Ensure headers were recorded |
| 54 | + assertNotNull(headers) |
| 55 | + |
| 56 | + // Assert that Accept-Charset header is not present |
| 57 | + assertFalse(headers!!.contains(HttpHeaders.AcceptCharset)) |
| 58 | + } |
| 59 | +} |
0 commit comments