Skip to content

Commit

Permalink
Upgrade mockwebserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuvist committed Feb 17, 2020
1 parent 8e3f9d4 commit e74521e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.10.2'
implementation "com.squareup.okhttp3:mockwebserver:3.2.0"
implementation "com.squareup.okhttp3:mockwebserver:4.3.1"
testImplementation "org.junit.jupiter:junit-jupiter:5.6.0"
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/com/blogwind/easywebmock/MockServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,18 @@ class MockServer {
.setBody("Internal error")
}

var resp = getResponseOnce(request.path)
val path = request.requestUrl!!.encodedPath
var resp = getResponseOnce(path)
if (resp != null) {
return resp
}

resp = defaultResponses[request.path]
resp = defaultResponses[path]
if (resp != null) {
return resp
}

val handler = defaultHandlers[request.path]
val handler = defaultHandlers[path]
if (handler != null) {
return try {
handler(request)
Expand All @@ -112,7 +113,7 @@ class MockServer {
.setBody("Not Found")
}
}
server.setDispatcher(dispatcher)
server.dispatcher = dispatcher
server.start()
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/com/blogwind/easywebmock/MockServerManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ class MockServerManagerTest {
Assertions.assertEquals(client.get(toPath), "<html></html>")
}

@Test
fun testPath() {
val server = MockServerManager.defaultServer
val client = UrlClient(server.getUrl())
val toPath = "/foobar"

server.setOneTimeResponse(toPath, "foobar")
Assertions.assertEquals(client.get("$toPath?user=1"), "foobar")
}

@Test
fun testMisc() {
val server = MockServerManager()
Expand Down
9 changes: 5 additions & 4 deletions src/test/kotlin/com/blogwind/easywebmock/UrlClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package com.blogwind.easywebmock
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.net.URL

class UrlClient(baseUrl: String) {
Expand All @@ -22,15 +23,15 @@ class UrlClient(baseUrl: String) {

fun post(path: String, content: String = ""): String {
val client = OkHttpClient()
val contentType = MediaType.parse("application/json; charset=utf-8")
val body = RequestBody.create(contentType, content)
val contentType = "application/json; charset=utf-8".toMediaType()
val body = content.toRequestBody(contentType)

val request: Request = Request.Builder()
.url(baseUrl + path)
.post(body).build()

val response = client.newCall(request).execute()
return response.body()!!.string()
return response.body!!.string()
}

inline fun <reified T> postForObject(path: String, obj: Any): T {
Expand Down

0 comments on commit e74521e

Please sign in to comment.