Skip to content

Instantly share code, notes, and snippets.

@AOrobator
Created July 21, 2018 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AOrobator/7dcee3829bc0eb60d20a8f5525c4494f to your computer and use it in GitHub Desktop.
Save AOrobator/7dcee3829bc0eb60d20a8f5525c4494f to your computer and use it in GitHub Desktop.
OkHttpExtensions
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import org.amshove.kluent.`should equal`
/**
* OkHttp3 extensions for syntactic sugar while testing
*/
enum class HttpMethod {
DELETE,
GET,
PATCH,
POST,
PUT
}
enum class ContentType(val type: String) {
FORM_URL_ENCODED("application/x-www-form-urlencoded"),
JSON("application/json; charset=UTF-8"),
}
data class ExpectedRequest(
val authorization: String?,
val method: HttpMethod,
val contentType: ContentType?,
val path: String,
val body: String
)
infix fun MockWebServer.`received request`(expectedRequest: ExpectedRequest) {
val actualRequest = takeRequest()
actualRequest `has authorization` expectedRequest.authorization
actualRequest `has method` expectedRequest.method.name
actualRequest `has content type` expectedRequest.contentType?.type
actualRequest `has path` expectedRequest.path
actualRequest `has body` expectedRequest.body
}
infix fun RecordedRequest.`has body`(body: String) {
this.body.readUtf8() `should equal` body
}
infix fun RecordedRequest.`has path`(path: String) {
this.path `should equal` path
}
infix fun RecordedRequest.`has content type`(contentType: String?) {
getHeader("Content-Type") `should equal` contentType
}
infix fun RecordedRequest.`has authorization`(authorization: String?) {
getHeader("Authorization") `should equal` authorization
}
infix fun RecordedRequest.`has method`(method: String) {
this.method `should equal` method
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment