Created
July 21, 2018 17:21
-
-
Save AOrobator/7dcee3829bc0eb60d20a8f5525c4494f to your computer and use it in GitHub Desktop.
OkHttpExtensions
This file contains 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
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