@@ -206,4 +206,76 @@ class CalendarFetcherTest {
206206
207207 assertEquals(IOException ::class .java, e?.javaClass)
208208 }
209+
210+ @Test
211+ fun testFetchNetwork_validContentType () {
212+ // Test that when a server returns a valid Content-Type header,
213+ // the fetcher correctly parses it and processes the calendar data with the proper contentType
214+ val icalCorrect = testContext.resources.openRawResource(R .raw.vienna_evolution).use { streamCorrect ->
215+ streamCorrect.bufferedReader().use { it.readText() }
216+ }
217+
218+ // create mock response with valid Content-Type header
219+ MockServer .enqueue(
220+ content = icalCorrect,
221+ status = HttpStatusCode .OK ,
222+ headers = buildHeaders {
223+ append(HttpHeaders .ContentType , " text/calendar; charset=utf-8" )
224+ }
225+ )
226+
227+ // make request to local mock server
228+ var ical: String? = null
229+ var receivedContentType: ContentType ? = null
230+ val fetcher = object : CalendarFetcher (appContext, MockServer .uri(), client) {
231+ override suspend fun onSuccess (data : InputStream , contentType : ContentType ? , eTag : String? , lastModified : Long? , displayName : String? ) {
232+ ical = data.bufferedReader().use { it.readText() }
233+ receivedContentType = contentType
234+ }
235+ }
236+ runBlocking {
237+ fetcher.fetch()
238+ }
239+
240+ // assert content is correct and contentType is properly parsed
241+ assertEquals(icalCorrect, ical)
242+ assertEquals(" text/calendar; charset=utf-8" , receivedContentType.toString())
243+ }
244+
245+ @Test
246+ fun testFetchNetwork_malformedContentType () {
247+ // Test that when a server returns a malformed Content-Type header,
248+ // the fetcher logs a warning and successfully processes the calendar data with null contentType
249+ val icalCorrect = testContext.resources.openRawResource(R .raw.vienna_evolution).use { streamCorrect ->
250+ streamCorrect.bufferedReader().use { it.readText() }
251+ }
252+
253+ // create mock response with malformed Content-Type header (containts "-" at start)
254+ // that will trigger BadContentTypeFormatException to be thrown
255+ MockServer .enqueue(
256+ content = icalCorrect,
257+ status = HttpStatusCode .OK ,
258+ headers = buildHeaders {
259+ append(HttpHeaders .ContentType , " -Text/calendar charset=utf-8" )
260+ }
261+ )
262+
263+ // make request to local mock server
264+ var ical: String? = null
265+ var receivedContentType: ContentType ? = null
266+ val fetcher = object : CalendarFetcher (appContext, MockServer .uri(), client) {
267+ override suspend fun onSuccess (data : InputStream , contentType : ContentType ? , eTag : String? , lastModified : Long? , displayName : String? ) {
268+ ical = data.bufferedReader().use { it.readText() }
269+ receivedContentType = contentType
270+ }
271+ }
272+ runBlocking {
273+ fetcher.fetch()
274+ }
275+
276+ // assert content is correct and contentType is null (due to malformed header)
277+ assertEquals(icalCorrect, ical)
278+ assertEquals(null , receivedContentType)
279+ }
280+
209281}
0 commit comments