Add proxy password interceptor.

It passes request chain either to hex password interceptor or
to md5+salt interceptor depending on current protocol version.
This commit is contained in:
Yahor Berdnikau 2017-12-02 21:21:23 +01:00
parent 0c5ac61402
commit e838750ee2
4 changed files with 64 additions and 0 deletions

View File

@ -21,6 +21,7 @@ ext.versions = [
okhttp : "3.9.0",
junit : "4.12",
mockito : "2.12.0",
mockitoKotlin : "1.5.0",
kluent : "1.26",
apacheCodecs : "1.10",
@ -53,6 +54,8 @@ ext.testing = [
junit : "junit:junit:$versions.junit",
kotlinJunit : "org.jetbrains.kotlin:kotlin-test-junit:$versions.kotlin",
mockitoKotlin : "com.nhaarman:mockito-kotlin:$versions.mockitoKotlin",
mockito : "org.mockito:mockito-core:$versions.mockito",
mockitoInline : "org.mockito:mockito-inline:$versions.mockito",
kluent : "org.amshove.kluent:kluent:$versions.kluent",
mockWebServer : "com.squareup.okhttp3:mockwebserver:$versions.okhttp",
apacheCodecs : "commons-codec:commons-codec:$versions.apacheCodecs",

View File

@ -21,6 +21,8 @@ dependencies {
testImplementation testing.junit
testImplementation testing.kotlinJunit
testImplementation testing.mockito
testImplementation testing.mockitoInline
testImplementation testing.mockitoKotlin
testImplementation testing.kluent
testImplementation testing.mockWebServer

View File

@ -0,0 +1,23 @@
package org.moire.ultrasonic.api.subsonic.interceptors
import okhttp3.Interceptor
import okhttp3.Interceptor.Chain
import okhttp3.Response
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions
/**
* Proxy [Interceptor] that uses one of [hexInterceptor] or [mD5Interceptor] depends on [apiVersion].
*/
internal class ProxyPasswordInterceptor(
initialAPIVersions: SubsonicAPIVersions,
private val hexInterceptor: PasswordHexInterceptor,
private val mD5Interceptor: PasswordMD5Interceptor) : Interceptor {
var apiVersion: SubsonicAPIVersions = initialAPIVersions
override fun intercept(chain: Chain): Response =
if (apiVersion < SubsonicAPIVersions.V1_13_0) {
hexInterceptor.intercept(chain)
} else {
mD5Interceptor.intercept(chain)
}
}

View File

@ -0,0 +1,36 @@
package org.moire.ultrasonic.api.subsonic.interceptors
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import okhttp3.Interceptor.Chain
import org.junit.Test
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_12_0
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions.V1_13_0
/**
* Unit test for [ProxyPasswordInterceptor].
*/
class ProxyPasswordInterceptorTest {
private val mockPasswordHexInterceptor = mock<PasswordHexInterceptor>()
private val mockPasswordMd5Interceptor = mock<PasswordMD5Interceptor>()
private val mockChain = mock<Chain>()
private val proxyInterceptor = ProxyPasswordInterceptor(V1_12_0,
mockPasswordHexInterceptor, mockPasswordMd5Interceptor)
@Test
fun `Should use hex password on versions less then 1 13 0`() {
proxyInterceptor.intercept(mockChain)
verify(mockPasswordHexInterceptor).intercept(mockChain)
}
@Test
fun `Should use md5 password on version 1 13 0`() {
proxyInterceptor.apiVersion = V1_13_0
proxyInterceptor.intercept(mockChain)
verify(mockPasswordMd5Interceptor).intercept(mockChain)
}
}