Skip to content

Commit

Permalink
Replace the OkHttp MockWebServer with the native JDK web server.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelhg committed May 13, 2023
1 parent 0db2a38 commit a137dcd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
3 changes: 0 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jsoup:jsoup:1.16.1")
testImplementation(enforcedPlatform("org.junit:junit-bom:5.9.3"))
testImplementation("com.squareup.okhttp3:mockwebserver:4.11.0") {
exclude(group = "junit", module = "junit")
}
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/mikael/ksoup/http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,5 @@ interface ResponseToDocumentParser<in S, out D> {
}

open class StringToDocumentParser : ResponseToDocumentParser<String, Document> {
override fun parse(data: String) = Jsoup.parse(data)!!
override fun parse(data: String) = Jsoup.parse(data)
}
2 changes: 2 additions & 0 deletions src/test/kotlin/io/mikael/ksoup/test/simple.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SimpleTests : StaticWebTest() {
val gh = KSoup.extract<GitHubPage> {
result { GitHubPage() }
url = testUrl("/mikaelhg")
println(url)
element(".p-nickname", Element::text, GitHubPage::username)
text(".p-name", GitHubPage::fullName)
}
Expand Down Expand Up @@ -82,6 +83,7 @@ class SimpleTests : StaticWebTest() {
}
}
val ex2 = ex1.copy(url = testUrl("/huima"))
assertNotEquals(ex1.url, ex2.url)
}

}
55 changes: 27 additions & 28 deletions src/test/kotlin/io/mikael/ksoup/test/util.kt
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
package io.mikael.ksoup.test

import io.mikael.ksoup.getClassLogger
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import com.sun.net.httpserver.HttpHandler
import com.sun.net.httpserver.HttpServer
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import java.net.InetSocketAddress
import java.util.logging.LogManager

internal val resource = object {}::class.java.classLoader::getResource

internal val resourceAsStream = object {}::class.java.classLoader::getResourceAsStream

/**
* Handle MockHttpServer's requests by looking up and serving a static file from classpath.
*/
internal class StaticDispatcher(private val resolver: (String) -> String?): Dispatcher() {

private fun r(n: Int): MockResponse = MockResponse().setResponseCode(n)

override fun dispatch(request: RecordedRequest) =
resolver(request.path!!)
?.let { resource(it) }
?.let { r(200).setBody(it.readText()) }
?: r(404)

}

/**
* A base for all integration tests which need to connect to a mock HTTP server.
*/
open class StaticWebTest {

protected lateinit var server: MockWebServer
companion object {
private const val PORT = 45762
}

protected lateinit var server: HttpServer

protected lateinit var staticContentResolver: (String) -> String?

protected val log = getClassLogger()
private var lastRequest: String? = null

init {
resourceAsStream("logging.properties").use {
Expand All @@ -48,17 +35,29 @@ open class StaticWebTest {

@BeforeEach
fun before() {
server = MockWebServer().apply {
this.dispatcher = StaticDispatcher(staticContentResolver)
server = HttpServer.create(InetSocketAddress(PORT), 1).apply {
createContext("/").handler = HttpHandler { exchange ->
lastRequest = exchange.requestURI.path
when (val s = staticContentResolver(exchange.requestURI.path)) {
null -> {
exchange.sendResponseHeaders(404, 0)
}
else -> {
val bytes = resource(s)!!.readBytes()
exchange.sendResponseHeaders(200, bytes.size.toLong())
exchange.responseBody.write(bytes)
}
}
}
this.start()
}
}

@AfterEach
fun after() = server.close()
fun after() = server.stop(0)

protected fun testUrl(path: String) = server.url(path).toString()
protected fun testUrl(path: String) = "http://127.0.0.1:${PORT}${path}"

protected fun assertRequestPath(path: String) = assertEquals(path, server.takeRequest().path)
protected fun assertRequestPath(path: String) = assertEquals(path, lastRequest)

}

0 comments on commit a137dcd

Please sign in to comment.