diff --git a/rawhttp-req-in-edit/src/main/java/com/athaydes/rawhttp/reqinedit/ReqInEditParser.java b/rawhttp-req-in-edit/src/main/java/com/athaydes/rawhttp/reqinedit/ReqInEditParser.java index 456f009..e094663 100644 --- a/rawhttp-req-in-edit/src/main/java/com/athaydes/rawhttp/reqinedit/ReqInEditParser.java +++ b/rawhttp-req-in-edit/src/main/java/com/athaydes/rawhttp/reqinedit/ReqInEditParser.java @@ -50,9 +50,6 @@ public List parse(Stream lines) { Iterator iter = lines.iterator(); while (iter.hasNext()) { String line = iter.next(); - if (!line.startsWith(" ") && requestBuilder.length() > 0) { - requestBuilder.append('\n'); - } if (isComment(line)) { if (isSeparator(line)) { if (requestBuilder.length() > 0) { @@ -66,6 +63,7 @@ public List parse(Stream lines) { if (parsingStartLine) { if (!line.isEmpty()) { requestBuilder.append(startLine(line)); + requestBuilder.append('\n'); parsingStartLine = false; } } else if (line.isEmpty()) { @@ -75,6 +73,7 @@ public List parse(Stream lines) { } } else { requestBuilder.append(line); + requestBuilder.append('\n'); } } diff --git a/rawhttp-req-in-edit/src/test/kotlin/com/athaydes/rawhttp/reqinedit/ReqInEditParserTest.kt b/rawhttp-req-in-edit/src/test/kotlin/com/athaydes/rawhttp/reqinedit/ReqInEditParserTest.kt index 56bbcfb..91bd207 100644 --- a/rawhttp-req-in-edit/src/test/kotlin/com/athaydes/rawhttp/reqinedit/ReqInEditParserTest.kt +++ b/rawhttp-req-in-edit/src/test/kotlin/com/athaydes/rawhttp/reqinedit/ReqInEditParserTest.kt @@ -21,6 +21,7 @@ class ReqInEditParserTest { entries[0].run { request shouldBe """ GET http://example.org + """.trimIndent() requestBody shouldHaveSize 0 script.isPresent shouldBe false @@ -33,26 +34,26 @@ class ReqInEditParserTest { val parser = ReqInEditParser() val fileLines = listOf( - "### here start my requests", - "GET /something HTTP/1.1", - "Host: example.org", - "Accept: text/html", - "", - "### ", - "POST /resource/some-id HTTP/1.1", - "Host: example.org", - "Content-Type: application/json", - "", - "{\"example\": \"value\", \"count\": 1}", - "", - "###", - "", - "GET /resource/some-id HTTP/1.1", - "Host: example.org", - "Accept: application/json", - "", - "### done", - "" + "### here start my requests", + "GET /something HTTP/1.1", + "Host: example.org", + "Accept: text/html", + "", + "### ", + "POST /resource/some-id HTTP/1.1", + "Host: example.org", + "Content-Type: application/json", + "", + "{\"example\": \"value\", \"count\": 1}", + "", + "###", + "", + "GET /resource/some-id HTTP/1.1", + "Host: example.org", + "Accept: application/json", + "", + "### done", + "" ) val entries = parser.parse(fileLines.stream()) @@ -100,16 +101,17 @@ class ReqInEditParserTest { fun canParseRequestsWithResponseRef() { val parser = ReqInEditParser() - val fileLines = listOf("http://example.org", - "", - "<> first-response", - "", - "###", - "# another request", - "http://another.com", - "", - "<> second-response", - "" + val fileLines = listOf( + "http://example.org", + "", + "<> first-response", + "", + "###", + "# another request", + "http://another.com", + "", + "<> second-response", + "" ) val entries = parser.parse(fileLines.stream()) @@ -154,14 +156,14 @@ class ReqInEditParserTest { val parser = ReqInEditParser() val fileLines = listOf( - "POST /resource/some-id HTTP/1.1", - "Host: example.org", - "Content-Type: application/json", - "", - "< ./simple/body.json", - "", - "###", - "" + "POST /resource/some-id HTTP/1.1", + "Host: example.org", + "Content-Type: application/json", + "", + "< ./simple/body.json", + "", + "###", + "" ) val entries = parser.parse(fileLines.stream()) @@ -186,16 +188,16 @@ class ReqInEditParserTest { val parser = ReqInEditParser() val fileLines = listOf( - "POST /resource/some-id HTTP/1.1", - "Host: example.org", - "Content-Type: application/json", - "", - "", - "{", - "< ./entries.json", - " \"extra\": \"entry\"", - "}", - " " + "POST /resource/some-id HTTP/1.1", + "Host: example.org", + "Content-Type: application/json", + "", + "", + "{", + "< ./entries.json", + " \"extra\": \"entry\"", + "}", + " " ) val entries = parser.parse(fileLines.stream()) @@ -209,10 +211,12 @@ class ReqInEditParserTest { Content-Type: application/json """.trimIndent() - requestBody shouldBe listOf(StringOrFile.ofString("{"), - StringOrFile.ofFile("./entries.json"), - StringOrFile.ofString(" \"extra\": \"entry\""), - StringOrFile.ofString("}")) + requestBody shouldBe listOf( + StringOrFile.ofString("{"), + StringOrFile.ofFile("./entries.json"), + StringOrFile.ofString(" \"extra\": \"entry\""), + StringOrFile.ofString("}") + ) script.isPresent shouldBe false responseRef.isPresent shouldBe false } @@ -223,16 +227,16 @@ class ReqInEditParserTest { val parser = ReqInEditParser() val fileLines = listOf( - "GET /resource/some-id HTTP/1.1", - "Host: example.org", - "Accept: application/json", - "", - "> {% ", - " client.test(\"Request executed successfully\", function() {", - " client.assert(response.status === 200, \"Response status is not 200\");", - " });", - " %} ", - "" + "GET /resource/some-id HTTP/1.1", + "Host: example.org", + "Accept: application/json", + "", + "> {% ", + " client.test(\"Request executed successfully\", function() {", + " client.assert(response.status === 200, \"Response status is not 200\");", + " });", + " %} ", + "" ) val entries = parser.parse(fileLines.stream()) @@ -247,9 +251,11 @@ class ReqInEditParserTest { """.trimIndent() script.isPresent shouldBe true - script.get() shouldBe StringOrFile.ofString("\n client.test(\"Request executed successfully\", function() {\n" + - " client.assert(response.status === 200, \"Response status is not 200\");\n" + - " });\n ") + script.get() shouldBe StringOrFile.ofString( + "\n client.test(\"Request executed successfully\", function() {\n" + + " client.assert(response.status === 200, \"Response status is not 200\");\n" + + " });\n " + ) responseRef.isPresent shouldBe false } } @@ -259,12 +265,12 @@ class ReqInEditParserTest { val parser = ReqInEditParser() val fileLines = listOf( - "GET /resource/some-id HTTP/1.1", - "Host: example.org", - "Accept: application/json", - "", - "> my_response_handler.js", - "" + "GET /resource/some-id HTTP/1.1", + "Host: example.org", + "Accept: application/json", + "", + "> my_response_handler.js", + "" ) val entries = parser.parse(fileLines.stream()) @@ -284,4 +290,32 @@ class ReqInEditParserTest { } } + @Test + fun canParseRequestWithCommentsImmediatelyAfterStartLine() { + val parser = ReqInEditParser() + + val fileLines = listOf( + "POST http://example.org/dev/oauth/token", + "# Client Credentials flow POSTs a HTML form and receives JSON back", + "Content-Type: application/x-www-form-urlencoded", + "", + "client_id=client&client_secret=Secret&grant_type=client_credentials" + ) + + val entries = parser.parse(fileLines.stream()) + + entries.size shouldBe 1 + + entries[0].run { + request shouldBe """ + POST http://example.org/dev/oauth/token + Content-Type: application/x-www-form-urlencoded + + """.trimIndent() + requestBody.size shouldBe 1 + requestBody[0].match({ s -> s }, { f -> "" }) shouldBe + "client_id=client&client_secret=Secret&grant_type=client_credentials" + } + } + } \ No newline at end of file