diff --git a/spec/examples/basic_get.http b/spec/examples/basic_get.http index dd85f3f..66f3b79 100644 --- a/spec/examples/basic_get.http +++ b/spec/examples/basic_get.http @@ -4,3 +4,13 @@ user-agent: neovim ### get statement for localhost GET localhost:9999 + +### multi-line url +// reference: https://www.jetbrains.com/help/idea/exploring-http-syntax.html#break-long-requests-into-several-lines +// You can span url to multiple lines by adding whitespace indents +GET http://example.com:8080 + /api + /html + /get + ?id=123 + &value=content diff --git a/spec/examples/examples_spec.lua b/spec/examples/examples_spec.lua index 46f84c2..fc5bbf3 100644 --- a/spec/examples/examples_spec.lua +++ b/spec/examples/examples_spec.lua @@ -13,9 +13,9 @@ end describe("multi-line-url", function() it("line breaks should be ignored", function() - local source = open("spec/examples/multi_line_url.http") + local source = open("spec/examples/basic_get.http") local _, tree = utils.ts_parse_source(source) - local req_node = assert(tree:root():child(0)) + local req_node = assert(tree:root():child(2)) local req = parser.parse(req_node, source) assert.not_nil(req) ---@cast req rest.Request @@ -107,7 +107,7 @@ end) describe("builtin request hooks", function() describe("set_content_type", function() it("with external body", function() - local source = open("spec/examples/post_with_external_body.http") + local source = open("spec/examples/request_body/external_body.http") local _, tree = utils.ts_parse_source(source) local req_node = assert(tree:root():child(0)) local req = assert(parser.parse(req_node, source)) diff --git a/spec/examples/multi_line_url.http b/spec/examples/multi_line_url.http deleted file mode 100644 index 4597de0..0000000 --- a/spec/examples/multi_line_url.http +++ /dev/null @@ -1,9 +0,0 @@ -### multi-line url -// reference: https://www.jetbrains.com/help/idea/exploring-http-syntax.html#break-long-requests-into-several-lines -// Using line breaks with indent -GET http://example.com:8080 - /api - /html - /get - ?id=123 - &value=content diff --git a/spec/examples/post_with_external_body.http b/spec/examples/post_with_external_body.http deleted file mode 100644 index 8a41c32..0000000 --- a/spec/examples/post_with_external_body.http +++ /dev/null @@ -1,5 +0,0 @@ -### The request body is read from a file -POST https://example.com:8080/api/html/post -# rest.nvim can guess the Content-Type from file path - -< ./input.json diff --git a/spec/examples/request_body/external_body.http b/spec/examples/request_body/external_body.http new file mode 100644 index 0000000..7bb0be0 --- /dev/null +++ b/spec/examples/request_body/external_body.http @@ -0,0 +1,5 @@ +### External body +POST https://example.com:8080/api/html/post +# rest.nvim can guess and fill the Content-Type header from file path + +< ./input.json diff --git a/spec/examples/request_body/form_urlencoded.http b/spec/examples/request_body/form_urlencoded.http new file mode 100644 index 0000000..8a70d09 --- /dev/null +++ b/spec/examples/request_body/form_urlencoded.http @@ -0,0 +1,19 @@ +# form-urlencoded type is recognized with `Content-Type` header + +### form-urlencoded +POST https://ijhttp-examples.jetbrains.com/post +Content-Type: application/x-www-form-urlencoded + +key1=value1&key2=value2&key3=value3&key4=value4&key5=value5 + +### form-urlencoded (multiline) +# you can put some whitespaces in urlencoded form +# rest.nvim will recognize whitespaces and remove them before request +POST https://ijhttp-examples.jetbrains.com/post +Content-Type: application/x-www-form-urlencoded + +key1 = value1 & +key2 = value2 & +key3 = value3 & +key4 = value4 & +key5 = value5 diff --git a/spec/examples/graphql.http b/spec/examples/request_body/graphql.http similarity index 100% rename from spec/examples/graphql.http rename to spec/examples/request_body/graphql.http diff --git a/spec/examples/request_body/raw_body.http b/spec/examples/request_body/raw_body.http new file mode 100644 index 0000000..e5d3fc9 --- /dev/null +++ b/spec/examples/request_body/raw_body.http @@ -0,0 +1,29 @@ +# all bodies including json are treated as raw body + +### json body +# request body starting with `{\n` will have json syntax injection via tree-sitter +# rest.nvim provides opt-out json validation feature +POST https://example.com HTTP/1.1 +Content-Type: application/json + +{ + "foo": 123 +} + +### xml body +# request body starting with `<.*` will have xml syntax injection via tree-sitter +# rest.nvim provides opt-out xml validation feature +POST https://example.com HTTP/1.1 +Content-Type: application/xml + + + + login + password + + +### raw body +# all other bodies will be treated as raw body type +POST https://example.com HTTP/1.1 + +Hello world! diff --git a/spec/examples/variables/dynamic_vars.http b/spec/examples/variables/dynamic_vars.http index 59b6b4d..beadee1 100644 --- a/spec/examples/variables/dynamic_vars.http +++ b/spec/examples/variables/dynamic_vars.http @@ -1,3 +1,4 @@ +### dynamic variables POST https://reqres.in/api/users Content-Type: application/json diff --git a/spec/examples/variables/prompt_variables.http b/spec/examples/variables/prompt_variables.http index 6c15bcb..b47ac7c 100644 --- a/spec/examples/variables/prompt_variables.http +++ b/spec/examples/variables/prompt_variables.http @@ -1,4 +1,4 @@ -### basic get statement +### prompt variables # Basic syntax of prompt variable: diff --git a/spec/parser/http_parser_spec.lua b/spec/parser/http_parser_spec.lua index bcbe6ba..9f6de30 100644 --- a/spec/parser/http_parser_spec.lua +++ b/spec/parser/http_parser_spec.lua @@ -184,7 +184,7 @@ key5 = value5 end) it("parse external body", function() -- external body can be only sourced when - local source = open("spec/examples/post_with_external_body.http") + local source = open("spec/examples/request_body/external_body.http") local _, tree = utils.ts_parse_source(source) local req_node = assert(tree:root():child(0)) assert.same({ @@ -193,17 +193,17 @@ key5 = value5 headers = {}, cookies = {}, handlers = {}, - name = "The request body is read from a file", + name = "External body", body = { __TYPE = "external", data = { - path = "spec/examples/input.json", + path = "spec/examples/request_body/input.json", }, }, }, parser.parse(req_node, source)) end) it("parse graphql body", function() - local source = open("spec/examples/graphql.http") + local source = open("spec/examples/request_body/graphql.http") local _, tree = utils.ts_parse_source(source) local req_node = assert(tree:root():child(1)) local req = parser.parse(req_node, source)