Skip to content

Commit 10a9fe2

Browse files
HTTP client (#3483)
1 parent 18fd095 commit 10a9fe2

18 files changed

+1323
-294
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ _theme/tarantool/static/design.css.map
1515
.idea
1616
rocks
1717
.vscode/
18+
doc/code_snippets/.rocks
1819

1920
# Crowdin API key
2021
apikey.txt

doc/code_snippets/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Tarantool code examples
2+
3+
The `doc/code_snippets` folder of a Tarantool documentation repository contains runnable code examples that show how to work with various Tarantool modules. Code from these examples is [referenced](#referencing-code-snippets) in corresponding documentation sections.
4+
5+
## Prerequisites
6+
7+
First, install the [tt CLI utility](https://www.tarantool.io/en/doc/latest/reference/tooling/tt_cli/).
8+
Then, go to the `doc/code_snippets` folder and install the following libraries:
9+
10+
- Install [luatest](https://github.com/tarantool/luatest):
11+
```shell
12+
tt rocks install luatest
13+
```
14+
15+
- Install [luarapidxml](https://github.com/tarantool/luarapidxml):
16+
```shell
17+
tt rocks install luarapidxml
18+
```
19+
20+
21+
22+
## Running and testing examples
23+
24+
To test all the examples, go to the `doc/code_snippets` folder and execute the `luatest` command:
25+
26+
```shell
27+
.rocks/bin/luatest
28+
```
29+
30+
To run a specific example with tests, use the `luatest` command with the `-c` option, for example:
31+
32+
```shell
33+
.rocks/bin/luatest -c test/http_client/get_test.lua
34+
```
35+
36+
You can also run the `httpbin` service locally using Docker:
37+
38+
```shell
39+
docker run -p 80:80 kennethreitz/httpbin
40+
```
41+
42+
In this case, replace `https://httpbin.org` links with `http://127.0.0.1` to test the examples.
43+
44+
45+
## Referencing code snippets
46+
To display a specific source file in a topic, use the `literalinclude` directive as follows:
47+
```
48+
.. literalinclude:: /code_snippets/test/http_client/post_json_test.lua
49+
:language: lua
50+
:lines: 1-6
51+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:get('https://httpbin.org/cookies', {
3+
headers = {
4+
['Cookie'] = 'session_id=abc123; csrftoken=u32t4o;',
5+
}
6+
})
7+
print(response.body)
8+
9+
local luatest = require('luatest')
10+
local test_group = luatest.group()
11+
test_group.test_returns_cookie = function()
12+
luatest.assert_equals(response:decode()['cookies']['session_id'], 'abc123')
13+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:get('https://httpbin.org/headers', {
3+
headers = {
4+
['User-Agent'] = 'Tarantool HTTP client',
5+
['Authorization'] = 'Bearer abc123'
6+
}
7+
})
8+
print('Authorization: '..response:decode()['headers']['Authorization'])
9+
10+
local luatest = require('luatest')
11+
local test_group = luatest.group()
12+
test_group.test_returns_header = function()
13+
luatest.assert_equals(response:decode()['headers']['User-Agent'], 'Tarantool HTTP client')
14+
luatest.assert_equals(response:decode()['headers']['Authorization'], 'Bearer abc123')
15+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:get('https://httpbin.org/get', {
3+
params = { page = 1 },
4+
})
5+
print('URL: '..response.url)
6+
7+
local luatest = require('luatest')
8+
local test_group = luatest.group()
9+
test_group.test_returns_url_with_params = function()
10+
luatest.assert_equals(response:decode()['url'], 'https://httpbin.org/get?page=1')
11+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:get('https://httpbin.org/cookies/set?session_id=abc123&csrftoken=u32t4o&', {follow_location = false})
3+
print("'session_id' cookie value: "..response.cookies['session_id'][1])
4+
5+
local luatest = require('luatest')
6+
local test_group = luatest.group()
7+
test_group.test_returns_cookie = function()
8+
luatest.assert_equals(response.cookies['session_id'][1], 'abc123')
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:get('https://httpbin.org/gzip', {accept_encoding = "br, gzip, deflate"})
3+
print('Is response gzipped: '..tostring(response:decode()['gzipped']))
4+
5+
local luatest = require('luatest')
6+
local test_group = luatest.group()
7+
test_group.test_returns_status = function()
8+
luatest.assert_equals(response:decode()['gzipped'], true)
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:get('https://httpbin.org/etag/7c876b7e')
3+
print('ETag header value: '..response.headers['etag'])
4+
5+
local luatest = require('luatest')
6+
local test_group = luatest.group()
7+
test_group.test_returns_etag_header = function()
8+
luatest.assert_equals(response.headers['etag'], '7c876b7e')
9+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:get('https://httpbin.org/json')
3+
local document = response:decode()
4+
print("'title' value: "..document['slideshow']['title'])
5+
6+
local luatest = require('luatest')
7+
local test_group = luatest.group()
8+
test_group.test_returns_json = function()
9+
luatest.assert_equals(document['slideshow']['title'], 'Sample Slide Show')
10+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local http_client = require('http.client').new()
2+
local json = require('json')
3+
4+
local io = http_client:get('https://httpbin.org/stream/5', {chunked = true})
5+
local chunk_ids = ''
6+
while data ~= '' do
7+
local data = io:read('\n')
8+
if data == '' then break end
9+
local decoded_data = json.decode(data)
10+
chunk_ids = chunk_ids..decoded_data['id']..' '
11+
end
12+
print('IDs of received chunks: '..chunk_ids)
13+
io:finish()
14+
15+
local luatest = require('luatest')
16+
local test_group = luatest.group()
17+
test_group.test_returns_chunk_ids = function()
18+
luatest.assert_equals(chunk_ids, '0 1 2 3 4 ')
19+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:get('https://httpbin.org/get')
3+
print('Status: '..response.status..' '.. response.reason)
4+
5+
local luatest = require('luatest')
6+
local test_group = luatest.group()
7+
test_group.test_returns_status = function()
8+
luatest.assert_equals(response.status, 200)
9+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local http_client = require('http.client').new()
2+
local xml = require("luarapidxml")
3+
4+
http_client.decoders = {
5+
['application/xml'] = function(body, _content_type)
6+
return xml.decode(body)
7+
end,
8+
}
9+
10+
local response = http_client:get('https://httpbin.org/xml')
11+
local document = response:decode()
12+
print("'title' value: "..document['attr']['title'])
13+
14+
local luatest = require('luatest')
15+
local test_group = luatest.group()
16+
test_group.test_returns_json = function()
17+
luatest.assert_equals(document['attr']['title'], 'Sample Slide Show')
18+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:post('https://httpbin.org/anything', nil, {
3+
params = { user_id = 123, user_name = 'John Smith' },
4+
})
5+
print('User ID: '..response:decode()['form']['user_id'])
6+
7+
local luatest = require('luatest')
8+
local test_group = luatest.group()
9+
test_group.test_returns_form_data = function()
10+
luatest.assert_equals(response:decode()['form'], {user_id = "123", user_name = "John Smith"})
11+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:post('https://httpbin.org/anything', {
3+
user_id = 123,
4+
user_name = 'John Smith'
5+
})
6+
print('Posted data: '..response:decode()['data'])
7+
8+
local luatest = require('luatest')
9+
local test_group = luatest.group()
10+
test_group.test_returns_json = function()
11+
luatest.assert_equals(response:decode()['data'], '{"user_id":123,"user_name":"John Smith"}')
12+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local http_client = require('http.client').new()
2+
local json = require('json')
3+
4+
local io = http_client:post('https://httpbin.org/anything', nil, {chunked = true})
5+
io:write('Data part 1')
6+
io:write('Data part 2')
7+
io:finish()
8+
response = io:read('\r\n')
9+
decoded_data = json.decode(response)
10+
print('Posted data: '..decoded_data['data'])
11+
12+
local luatest = require('luatest')
13+
local test_group = luatest.group()
14+
test_group.test_returns_posted_data = function()
15+
luatest.assert_equals(decoded_data['data'], 'Data part 1Data part 2')
16+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:post('https://httpbin.org/anything', {
3+
user_id = 123,
4+
user_name = 'John Smith'
5+
}, {
6+
headers = {
7+
['Content-Type'] = 'application/yaml',
8+
}
9+
})
10+
print('Posted data:\n'..response:decode()['data'])
11+
12+
local luatest = require('luatest')
13+
local test_group = luatest.group()
14+
test_group.test_returns_yaml = function()
15+
luatest.assert_equals(response:decode()['data'], "---\
16+
user_id: 123\
17+
user_name: John Smith\
18+
...\
19+
")
20+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
local http_client = require('http.client').new()
2+
local response = http_client:request('GET', 'https://httpbin.org/get')
3+
print('Status: '..response.status..' '.. response.reason)
4+
5+
local luatest = require('luatest')
6+
local test_group = luatest.group()
7+
test_group.test_returns_status = function()
8+
luatest.assert_equals(response.status, 200)
9+
end

0 commit comments

Comments
 (0)