Skip to content

Commit

Permalink
Add MockWebServer JUnit4 dependency & use server Rule in mock test
Browse files Browse the repository at this point in the history
  • Loading branch information
erawhctim committed Nov 7, 2023
1 parent 5c620eb commit 0ead252
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ kotlinpoet = "1.14.2"
mavenplugin = "0.25.3"
moshi = "1.15.0"
okhttp = "4.12.0"
okhttp-junit = "5.0.0-alpha.11"
okio = "3.6.0"
protobuf = "3.25.0"
slf4j = "2.0.9"
Expand Down Expand Up @@ -46,6 +47,7 @@ moshiKotlinCodegen = { module = "com.squareup.moshi:moshi-kotlin-codegen", versi
okhttp-core = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
okhttp-tls = { module = "com.squareup.okhttp3:okhttp-tls", version.ref = "okhttp" }
okhttp-mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "okhttp" }
okhttp-mockwebserver-junit4 = { module = "com.squareup.okhttp3:mockwebserver3-junit4", version.ref = "okhttp-junit" }
okio-core = { module = "com.squareup.okio:okio", version.ref = "okio" }
protobuf-java = { module = "com.google.protobuf:protobuf-java", version.ref = "protobuf" }
protobuf-java-util = { module = "com.google.protobuf:protobuf-java-util", version.ref = "protobuf" }
Expand Down
1 change: 1 addition & 0 deletions okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies {

testImplementation(libs.assertj)
testImplementation(libs.okhttp.mockwebserver)
testImplementation(libs.okhttp.mockwebserver.junit4)
testImplementation(libs.kotlin.coroutines.test)
testImplementation(project(":extensions:google-java"))
testImplementation(project(":examples:generated-google-java"))
Expand Down
18 changes: 8 additions & 10 deletions okhttp/src/test/kotlin/com/connectrpc/okhttp/MockWebServerTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ import com.connectrpc.extensions.GoogleJavaProtobufStrategy
import com.connectrpc.impl.ProtocolClient
import com.connectrpc.protocols.NetworkProtocol
import kotlinx.coroutines.test.runTest
import mockwebserver3.MockResponse
import mockwebserver3.junit4.MockWebServerRule
import okhttp3.OkHttpClient
import okhttp3.Protocol
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.assertj.core.api.Assertions.assertThat
import org.junit.Rule
import org.junit.Test

class MockWebServerTests {

@get:Rule val mockWebServerRule = MockWebServerRule()

@Test
fun `compressed empty failure response is parsed correctly`() = runTest {
val mockWebServer = MockWebServer()
mockWebServer.start()

mockWebServer.enqueue(
mockWebServerRule.server.enqueue(
MockResponse().apply {
addHeader("accept-encoding", "gzip")
addHeader("content-encoding", "gzip")
Expand All @@ -47,7 +47,7 @@ class MockWebServerTests {
},
)

val host = mockWebServer.url("/")
val host = mockWebServerRule.server.url("/")

val protocolClient = ProtocolClient(
ConnectOkHttpClient(
Expand All @@ -66,12 +66,10 @@ class MockWebServerTests {

val response = ElizaServiceClient(protocolClient).say(sayRequest { sentence = "hello" })

mockWebServer.takeRequest().apply {
mockWebServerRule.server.takeRequest().apply {
assertThat(path).isEqualTo("/connectrpc.eliza.v1.ElizaService/Say")
}

assertThat(response.code).isEqualTo(Code.UNKNOWN)

mockWebServer.shutdown()
}
}

0 comments on commit 0ead252

Please sign in to comment.