Skip to content

Commit

Permalink
docs: more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
boltlessengineer committed Sep 11, 2024
1 parent 3d9255d commit b4353fb
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 22 deletions.
10 changes: 10 additions & 0 deletions spec/examples/basic_get.http
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions spec/examples/examples_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 0 additions & 9 deletions spec/examples/multi_line_url.http

This file was deleted.

5 changes: 0 additions & 5 deletions spec/examples/post_with_external_body.http

This file was deleted.

5 changes: 5 additions & 0 deletions spec/examples/request_body/external_body.http
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions spec/examples/request_body/form_urlencoded.http
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
29 changes: 29 additions & 0 deletions spec/examples/request_body/raw_body.http
Original file line number Diff line number Diff line change
@@ -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

<?xml version="1.0" encoding="utf-8"?>
<Request>
<Login>login</Login>
<Password>password</Password>
</Request>

### raw body
# all other bodies will be treated as raw body type
POST https://example.com HTTP/1.1

Hello world!
1 change: 1 addition & 0 deletions spec/examples/variables/dynamic_vars.http
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
### dynamic variables
POST https://reqres.in/api/users
Content-Type: application/json

Expand Down
2 changes: 1 addition & 1 deletion spec/examples/variables/prompt_variables.http
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### basic get statement
### prompt variables

# Basic syntax of prompt variable:

Expand Down
8 changes: 4 additions & 4 deletions spec/parser/http_parser_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
Expand Down

0 comments on commit b4353fb

Please sign in to comment.