Skip to content

Commit

Permalink
Merge pull request #503 from auth0/fix-json-header
Browse files Browse the repository at this point in the history
Fix bug parsing content type headers
  • Loading branch information
lbalmaceda authored Jul 19, 2021
2 parents e3f82a5 + 31db7e4 commit 5407445
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.auth0.android.request

import java.io.InputStream
import java.util.*

/**
* Contains the information received from the server after executing a network request.
Expand All @@ -23,5 +24,8 @@ public data class ServerResponse(
* Checks if the Content-Type headers declare the received media type as 'application/json'.
* @return whether this response contains a JSON body or not.
*/
public fun isJson(): Boolean = headers["Content-Type"]?.contains("application/json") ?: false
public fun isJson(): Boolean =
headers.mapKeys { it.key.lowercase(Locale.getDefault()) }["content-type"]
?.contains("application/json")
?: false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.auth0.android.request

import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mockito.Mockito.mock
import java.io.InputStream

public class ServerResponseTest {

@Test
public fun shouldDetectSuccessfulResponse() {
val responseSuccess = ServerResponse(
200,
mock(InputStream::class.java),
emptyMap()
)
val responseNoContent = ServerResponse(
204,
mock(InputStream::class.java),
emptyMap()
)
assertTrue(responseSuccess.isSuccess())
assertTrue(responseNoContent.isSuccess())
}

@Test
public fun shouldDetectFailedResponse() {
val responseMultipleChoices = ServerResponse(
300,
mock(InputStream::class.java),
emptyMap()
)
val responseUnauthorized = ServerResponse(
401,
mock(InputStream::class.java),
emptyMap()
)
assertFalse(responseMultipleChoices.isSuccess())
assertFalse(responseUnauthorized.isSuccess())
}

@Test
public fun shouldDetectJsonContentHeader() {
val responseMixed = ServerResponse(
200,
mock(InputStream::class.java),
mapOf("Content-Type" to listOf("application/json"))
)
val responseLower = ServerResponse(
200,
mock(InputStream::class.java),
mapOf("content-type" to listOf("application/json"))
)
val responseUpper = ServerResponse(
200,
mock(InputStream::class.java),
mapOf("CONTENT-TYPE" to listOf("application/json"))
)

assertTrue(responseMixed.isJson())
assertTrue(responseLower.isJson())
assertTrue(responseUpper.isJson())
}
}

0 comments on commit 5407445

Please sign in to comment.