Skip to content

Commit

Permalink
Merge pull request #481 from vimeo/fix-crash-for-non-ascii-ua
Browse files Browse the repository at this point in the history
Allow non ASCII characters as a user agent
  • Loading branch information
anthonycr authored Apr 8, 2021
2 parents 2af09c1 + dc14643 commit 7598617
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions api-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ dependencies {
// Okio used by Moshi
implementation "com.squareup.okio:okio:$okioVersion"

// Test dependencies
testImplementation "junit:junit:$junitVersion"
}

compileKotlin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import okhttp3.Response
* @param userAgent The user agent that should be sent with every request.
*/
class UserAgentHeaderInterceptor(private val userAgent: String) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response =
chain.proceed(chain.request().newBuilder().header(HEADER_USER_AGENT, userAgent).build())
override fun intercept(chain: Interceptor.Chain): Response = chain.proceed(chain.request().let { request ->
val updatedHeaders = request.headers().newBuilder().addUnsafeNonAscii(HEADER_USER_AGENT, userAgent).build()

request.newBuilder().headers(updatedHeaders).build()
})

companion object {
private const val HEADER_USER_AGENT = "User-Agent"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.vimeo.networking2.internal.interceptor

import okhttp3.Call
import okhttp3.Connection
import okhttp3.Interceptor
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.Response
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.concurrent.TimeUnit

class UserAgentHeaderInterceptorTest {

private fun createChain(): Interceptor.Chain = object : Interceptor.Chain {
override fun request(): Request = Request.Builder().url("https://vimeo.com").build()

override fun proceed(request: Request): Response = Response.Builder()
.request(request)
.protocol(Protocol.HTTP_2)
.code(200)
.message("hello")
.build()

override fun connection(): Connection = error("Unsupported")

override fun call(): Call = error("Unsupported")

override fun connectTimeoutMillis(): Int = error("Unsupported")

override fun withConnectTimeout(timeout: Int, unit: TimeUnit): Interceptor.Chain = error("Unsupported")

override fun readTimeoutMillis(): Int = error("Unsupported")

override fun withReadTimeout(timeout: Int, unit: TimeUnit): Interceptor.Chain = error("Unsupported")

override fun writeTimeoutMillis(): Int = error("Unsupported")

override fun withWriteTimeout(timeout: Int, unit: TimeUnit): Interceptor.Chain = error("Unsupported")
}

@Test
fun `normal characters are supported`() {
assertEquals(
UserAgentHeaderInterceptor(userAgent = "test").intercept(createChain()).request().header("User-Agent"),
"test"
)
}

@Test
fun `special characters are supported`() {
assertEquals(
UserAgentHeaderInterceptor(userAgent = "test²").intercept(createChain()).request().header("User-Agent"),
"test²"
)
}
}

0 comments on commit 7598617

Please sign in to comment.