Skip to content

Commit

Permalink
RawHttpCli: test for HTTP file with comment and variable.
Browse files Browse the repository at this point in the history
Tests fix for #59.
  • Loading branch information
renatoathaydes committed Aug 25, 2023
1 parent ab78004 commit 8642374
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
6 changes: 6 additions & 0 deletions rawhttp-cli-tests/src/test/kotlin/RawHttpCliTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,12 @@ class RawHttpCliTest : RawHttpCliTester() {
assertGetFooResponseThenPostFooResponse(handleTest, "{prod: false}")
}

@Test
fun canRunHttpFileWithEnvironmentAndComments() {
val handle = runCli("run", asClassPathFile("reqin-edit-tests/with-env/variables.http"), "-e", "prod")
assertPostMirrorHeadersAndBody(handle)
}

@Test
fun canRunHttpFileWithEnvironmentAndPrintStats() {
val handleProd = runCli(
Expand Down
32 changes: 28 additions & 4 deletions rawhttp-cli-tests/src/test/kotlin/RawHttpCliTester.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.junit.jupiter.api.BeforeAll
import rawhttp.core.EagerHttpResponse
import rawhttp.core.RawHttp
import rawhttp.core.RawHttpResponse
import rawhttp.core.body.BytesBody
import rawhttp.core.body.StringBody
import rawhttp.core.client.TcpRawHttpClient
import rawhttp.core.errors.InvalidHttpRequest
Expand Down Expand Up @@ -203,31 +204,41 @@ abstract class RawHttpCliTester {
"/saysomething" ->
http.parseResponse(SUCCESS_HTTP_RESPONSE)
.writeTo(client.getOutputStream())

"/foo" ->
when (request.method) {
"GET" -> http.parseResponse(SUCCESS_GET_FOO_HTTP_RESPONSE)
.writeTo(client.getOutputStream())

"POST" -> http.parseResponse(SUCCESS_POST_FOO_HTTP_RESPONSE)
.withBody(request.body.map {
StringBody(
it.decodeBodyToString(
Charsets.UTF_8
)
)
}
.orElse(null))
}.orElse(null))
.writeTo(client.getOutputStream())

else -> http.parseResponse(NOT_FOUND_HTTP_RESPONSE)
.writeTo(client.getOutputStream())
}

"/reply" -> http.parseResponse(SUCCESS_POST_FOO_HTTP_RESPONSE)
.withBody(StringBody(request.body.map {
"Received:" + it.decodeBodyToString(
Charsets.UTF_8
)
}
.orElse("Did not receive anything")))
}.orElse("Did not receive anything")))
.writeTo(client.getOutputStream())

"/mirror-headers-and-body" -> http.parseResponse("200 OK")
.withHeaders(request.headers.except("Content-Length"))
.withBody(request.body.map {
BytesBody(it.decodeBody())
}.orElse(StringBody("")))
.writeTo(client.getOutputStream())

else ->
http.parseResponse(NOT_FOUND_HTTP_RESPONSE)
.writeTo(client.getOutputStream())
Expand Down Expand Up @@ -357,6 +368,19 @@ abstract class RawHttpCliTester {
assertNoSysErrOutput(handle)
}

fun assertPostMirrorHeadersAndBody(handle: ProcessHandle) {
val expectedBody = "foo=bar&zort=false"
val postResponse = "HTTP/1.1 200 OK" +
"\r\nContent-Type: application/x-www-form-urlencoded" +
"\r\nHost: localhost" +
"\r\nContent-Length: ${expectedBody.length}" +
"\r\n\r\n$expectedBody$EOL"

handle.verifyProcessTerminatedWithExitCode(0)
handle.out shouldBe postResponse
assertNoSysErrOutput(handle)
}

private fun assertStatistics(output: String) {
output.lines().run {
size shouldBe (6)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"prod": {
"host": "localhost:8083",
"path": "/foo",
"body": "{prod: true}"
"body": "{prod: true}",
"mirror-path": "/mirror-headers-and-body"
},
"test": {
"host": "localhost:8083",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
POST {{host}}{{mirror-path}}
# Client Credentials flow POSTs a HTML form and receives JSON back
Content-Type: application/x-www-form-urlencoded

foo=bar&zort=false

0 comments on commit 8642374

Please sign in to comment.