Skip to content

Commit

Permalink
Fixed integration tests relying on postman-echo.com/get API (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
rybalkinsd authored May 13, 2020
1 parent 14adae4 commit ccf2994
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ class ToJsonExtTest {
@Test
fun `gets response as json # ext`() {
val response = getUrl.httpGet().toJson().toString()
val expectedRegex = json {
"args" to json { }
"headers" to json {
"x-forwarded-proto" to "https"
"host" to "postman-echo.com"
"accept-encoding" to "gzip"
"user-agent" to "okhttp/[0-9]*.[0-9]*.[0-9]*"
"x-forwarded-port" to "443"
}
"url" to getUrl
}.escape()

assertThat(response).matches(expectedRegex)

assertThat(response)
.containsPattern(""""user-agent":"okhttp/[0-9]*.[0-9]*.[0-9]*"""")
.containsPattern(""""x-forwarded-proto":"https"""")
.containsPattern(""""x-forwarded-port":"443"""")
.containsPattern(""""host":"postman-echo.com"""")
.containsPattern(""""accept-encoding":"gzip"""")
.containsPattern(""""url":"$getUrl"""")
}

@Test
Expand Down Expand Up @@ -131,7 +126,3 @@ class ToJsonExtTest {


}

private fun String.escape(): String = replace("/", "\\/")
.replace("{", "\\{")
.replace("}", "\\}")
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.rybalkinsd.kohttp.ext

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.util.regex.Pattern

/**
* @author sergey
Expand All @@ -13,62 +14,22 @@ class ResponseExtKtTest {
@Test
fun `gets response as string # ext`() {
val response = getUrl.httpGet().asString()!!
val expectedRegex = """{
|"args":{},
|"headers":{
| "x-forwarded-proto":"https",
| "host":"postman-echo.com",
| "accept-encoding":"gzip",
| "user-agent":"okhttp/[0-9]*.[0-9]*.[0-9]*",
| "x-forwarded-port":"443"
| },
|"url":"https://postman-echo.com/get"
|}"""
.trimMargin("|")
.replace(Regex("\\s"),"")
.escape()


assertThat(response).matches(expectedRegex)
assertThat(response)
.containsPattern(""""user-agent":"okhttp/[0-9]*.[0-9]*.[0-9]*"""")
.containsPattern(""""x-forwarded-proto":"https"""")
.containsPattern(""""x-forwarded-port":"443"""")
.containsPattern(""""host":"postman-echo.com"""")
.containsPattern(""""accept-encoding":"gzip"""")
.containsPattern(""""url":"https://postman-echo.com/get"""")
}

@Test
fun `streams response # ext`() {
val response = "https://postman-echo.com/stream/2".httpGet().asStream()
?.readBytes()?.let { String(it) }

val expectedRegex = """{
| "args": {
| "n": "2"
| },
| "headers": {
| "x-forwarded-proto": "https",
| "host": "postman-echo.com",
| "accept-encoding": "gzip",
| "user-agent": "okhttp/[0-9]*.[0-9]*.[0-9]*",
| "x-forwarded-port": "443"
| },
| "url": "https://postman-echo.com/stream/2"
|}{
| "args": {
| "n": "2"
| },
| "headers": {
| "x-forwarded-proto": "https",
| "host": "postman-echo.com",
| "accept-encoding": "gzip",
| "user-agent": "okhttp/[0-9]*.[0-9]*.[0-9*]",
| "x-forwarded-port": "443"
| },
| "url": "https://postman-echo.com/stream/2"
|}"""
.trimMargin("|")
.escape()

assertThat(response).matches(expectedRegex)
// Ensure we get two pairs of arguments as declared in our request
assertThat(response).containsPattern(Pattern.compile("args.*args", Pattern.DOTALL))
}
}

private fun String.escape(): String = replace("/", "\\/")
.replace("{", "\\{")
.replace("}", "\\}")

0 comments on commit ccf2994

Please sign in to comment.