Created
July 15, 2017 12:30
-
-
Save mikaelhg/102071035292be16c81b9b4cf119a7b3 to your computer and use it in GitHub Desktop.
JVM embedded web server for integration tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
modelVersion: 4.0.0 | |
groupId: io.mikael | |
artifactId: ksoup | |
name: ksoup | |
version: 1.0-SNAPSHOT | |
packaging: jar | |
parent: {groupId: org.sonatype.oss, artifactId: oss-parent, version: 9} | |
properties: {java.version: '1.8', kotlin.version: '1.1.3-2', jsoup.version: '1.10.3'} | |
dependencies: | |
- {groupId: org.jsoup, artifactId: jsoup, version: "${jsoup.version}"} | |
- {groupId: org.jetbrains.kotlin, artifactId: kotlin-stdlib-jre8, version: "${kotlin.version}"} | |
- {scope: test, groupId: junit, artifactId: junit, version: 4.12} | |
- {scope: test, groupId: com.squareup.okhttp3, artifactId: mockwebserver, version: 3.8.1, exclusions: [{groupId: org.bouncycastle, artifactId: '*'}]} | |
build: | |
sourceDirectory: src/main/kotlin | |
testSourceDirectory: src/test/kotlin | |
plugins: | |
- groupId: org.jetbrains.kotlin | |
artifactId: kotlin-maven-plugin | |
version: "${kotlin.version}" | |
executions: | |
- {id: compile, phase: compile, goals: ['compile']} | |
- {id: test-compile, phase: test-compile, goals: ['test-compile']} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mikael.ksoup.test | |
import io.mikael.ksoup.KSoup | |
import org.jsoup.nodes.Element | |
import org.junit.Assert.assertEquals | |
import org.junit.Test | |
class SimpleTests : StaticWebTest() { | |
init { | |
staticContentResolver = { | |
when (it) { | |
"/mikaelhg" -> "github-mikaelhg.html" | |
"/huima" -> "github-huima.html" | |
else -> null | |
} | |
} | |
} | |
@Test | |
fun `property reference`() { | |
val gh = KSoup.extract<GitHubPage> { | |
result { GitHubPage() } | |
url = testUrl("/mikaelhg") | |
element(".p-nickname", Element::text, GitHubPage::username) | |
text(".p-name", GitHubPage::fullName) | |
} | |
assertEquals("mikaelhg", gh.username) | |
assertEquals("Mikael Gueck", gh.fullName) | |
assertRequestPath("/mikaelhg") | |
} | |
@Test | |
fun `simple one page extraction`() { | |
val gh = KSoup.extract<GitHubPage> { | |
result { GitHubPage() } | |
url = testUrl("/mikaelhg") | |
userAgent = "Mozilla/5.0 UnitTesting/1.0" | |
headers["Accept-Encoding"] = "gzip" | |
element(".p-nickname") { element, page -> | |
page.username = element.text() | |
} | |
text(".p-name") { text, page -> | |
page.fullName = text | |
} | |
} | |
assertEquals("mikaelhg", gh.username) | |
assertEquals("Mikael Gueck", gh.fullName) | |
assertRequestPath("/mikaelhg") | |
} | |
@Test | |
fun `copy extractors`() { | |
val ex1 = KSoup.simple<GitHubPage> { | |
result { GitHubPage() } | |
url = "https://github.com/mikaelhg" | |
element(".p-nickname") { element, page -> | |
page.username = element.text() | |
} | |
text(".p-name") { text, page -> | |
page.fullName = text | |
} | |
} | |
val ex2 = ex1.copy(url = testUrl("/huima")) | |
val gh = ex2.extract() | |
assertEquals("huima", gh.username) | |
assertEquals("Heimo Laukkanen", gh.fullName) | |
assertRequestPath("/huima") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mikael.ksoup.test | |
import okhttp3.mockwebserver.Dispatcher | |
import okhttp3.mockwebserver.MockResponse | |
import okhttp3.mockwebserver.MockWebServer | |
import okhttp3.mockwebserver.RecordedRequest | |
import org.junit.After | |
import org.junit.Assert.assertEquals | |
import org.junit.Before | |
import java.util.logging.LogManager | |
data class GitHubPage(var username: String = "", var fullName: String = "") | |
internal val resource = StaticWebTest::class.java.classLoader::getResource | |
internal val resourceAsStream = StaticWebTest::class.java.classLoader::getResourceAsStream | |
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) | |
} | |
open class StaticWebTest { | |
protected lateinit var server: MockWebServer | |
protected lateinit var staticContentResolver: (String) -> String? | |
init { | |
resourceAsStream("logging.properties").use { | |
LogManager.getLogManager().readConfiguration(it) | |
} | |
} | |
@Before | |
fun before() { | |
server = MockWebServer().apply { | |
this.setDispatcher(StaticDispatcher(staticContentResolver)) | |
this.start() | |
} | |
} | |
@After | |
fun after(): Unit = server.close() | |
protected fun testUrl(path: String) = server.url(path)!!.toString() | |
protected fun assertRequestPath(path: String) = assertEquals(path, server.takeRequest().path) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment