Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CFC: Feature/mockk integration #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions kohttp-mockk/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies {
implementation(kotlin("stdlib-jdk8"))
api(project(":kohttp"))

implementation("io.mockk:mockk:1.10.0")

testImplementation(kotlin("test-junit"))
testImplementation("org.assertj:assertj-core:3.12.2")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.rybalkinsd.kohttp.mockk

import io.github.rybalkinsd.kohttp.dsl.context.HttpContext
import io.github.rybalkinsd.kohttp.dsl.context.HttpGetContext
import io.github.rybalkinsd.kohttp.dsl.context.Method
import io.mockk.MockKMatcherScope
import io.mockk.mockkStatic

/**
* The only purpose of Matchers class here is to enable a static init block.
* It'll be possible to clean this API after
* https://youtrack.jetbrains.com/issue/KT-13486
*/
class Matchers {
companion object {
init {
Method.values()
.map { it.name.toLowerCase().capitalize() }
.forEach {
mockkStatic("io.github.rybalkinsd.kohttp.dsl.Http${it}DslKt")
}
}

inline fun <reified T : HttpContext> MockKMatcherScope.matching(crossinline init: T.() -> Unit): T.() -> Unit = match {
with(T::class.java) {
newInstance().apply(init) == newInstance().apply(it)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.rybalkinsd.kohttp.mockk


/**
* It's an open question how `Response { }` should look like.
*/
fun Response(init: okhttp3.Response.Builder.() -> Unit): okhttp3.Response {
return okhttp3.Response.Builder().also(init).build()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No newline

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.github.rybalkinsd.kohttp.mockk

import io.github.rybalkinsd.kohttp.dsl.httpGet
import io.github.rybalkinsd.kohttp.dsl.httpPost
import io.github.rybalkinsd.kohttp.ext.url
import io.github.rybalkinsd.kohttp.mockk.Matchers.Companion.matching
import io.mockk.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class MatchersTest {

@Test
fun `get request matcher`() {
every {
httpGet(init = matching {
url("https://unknown.site")
param {
"x" to 123
"y" to "no"
}
})
} returns Response {
request(mockk())
protocol(mockk())
code(42)
message("")
}


val r = httpGet {
url("https://unknown.site")
param {
"y" to "no"
"x" to 123
}
}
assertThat(r.code()).isEqualTo(42)
}

@Test
fun `post request matcher`() {
every {
httpPost(init = matching {
url("https://github.com")
body {
json {
"abc" to 123
}
}
})
} returns Response {
request(mockk())
protocol(mockk())
code(101)
message("mockk")
}

val r = httpPost {
url("https://github.com")
body {
json {
"abc" to 123
}
}
}
with(r) {
assertThat(message()).isEqualTo("mockk")
assertThat(code()).isEqualTo(101)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@ class HeaderContext {
}

internal fun forEach(action: (k: String, v: Any) -> Unit) = map.forEach { (k, v) -> action(k, v) }

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as HeaderContext

if (map != other.map) return false

return true
}

override fun hashCode(): Int {
return map.hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ sealed class HttpContext(private val method: Method = GET) : IHttpContext {
scheme(scheme)
host(host)
port?.let { port(it) }
path?.let { encodedPath(it) }
if (!path.isNullOrEmpty()) { encodedPath(path) }
paramContext.forEach { k, v ->
when (v) {
null -> addQueryParameter(k, null)
Expand All @@ -68,6 +68,34 @@ sealed class HttpContext(private val method: Method = GET) : IHttpContext {

open fun makeBody(): RequestBody = throw UnsupportedOperationException("Request body is not supported for [$method] Method.")

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as HttpContext

if (method != other.method) return false
if (paramContext != other.paramContext) return false
if (headerContext != other.headerContext) return false
if (scheme != other.scheme) return false
if (host != other.host) return false
if (port != other.port) return false
if (path != other.path) return false

return true
}

override fun hashCode(): Int {
var result = method.hashCode()
result = 31 * result + paramContext.hashCode()
result = 31 * result + headerContext.hashCode()
result = 31 * result + scheme.hashCode()
result = 31 * result + host.hashCode()
result = 31 * result + (port ?: 0)
result = 31 * result + (path?.hashCode() ?: 0)
return result
}

}

open class HttpPostContext(method: Method = POST) : HttpContext(method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@ class ParamContext {
action(k, if (v.size == 1) v.first() else v)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ParamContext

if (params != other.params) return false

return true
}

override fun hashCode(): Int {
return params.hashCode()
}

}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ rootProject.name = "kohttp"

include("kohttp-jackson")
include("kohttp")
include("kohttp-mockk")
include("kohttp-test")