Skip to content

Commit

Permalink
Fix #59 (ReqInEdit) parse comments without losing headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes committed Aug 22, 2023
1 parent 97d94ef commit 7a40d1b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ public List<ReqInEditEntry> parse(Stream<String> lines) {
Iterator<String> 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) {
Expand All @@ -66,6 +63,7 @@ public List<ReqInEditEntry> parse(Stream<String> lines) {
if (parsingStartLine) {
if (!line.isEmpty()) {
requestBuilder.append(startLine(line));
requestBuilder.append('\n');
parsingStartLine = false;
}
} else if (line.isEmpty()) {
Expand All @@ -75,6 +73,7 @@ public List<ReqInEditEntry> parse(Stream<String> lines) {
}
} else {
requestBuilder.append(line);
requestBuilder.append('\n');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ReqInEditParserTest {
entries[0].run {
request shouldBe """
GET http://example.org
""".trimIndent()
requestBody shouldHaveSize 0
script.isPresent shouldBe false
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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
}
Expand All @@ -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())
Expand All @@ -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
}
}
Expand All @@ -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())
Expand All @@ -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 -> "<FILE>" }) shouldBe
"client_id=client&client_secret=Secret&grant_type=client_credentials"
}
}

}

0 comments on commit 7a40d1b

Please sign in to comment.