forked from simov/grant
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
67 lines (57 loc) · 2.05 KB
/
Copy pathclient.js
File metadata and controls
67 lines (57 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var t = require('assert')
var http = require('http')
var qs = require('qs')
var request = require('../lib/client')
var compose = require('request-compose')
describe('client', () => {
describe('parse', () => {
var server
before((done) => {
server = http.createServer()
server.on('request', (req, res) => {
if (req.url === '/json') {
res.writeHead(200, {'content-type': 'application/json'})
res.end(JSON.stringify({json: true}))
}
if (req.url === '/qs') {
res.writeHead(200, {'content-type': 'application/x-www-form-urlencoded'})
res.end(qs.stringify({nested: {querystring: true}}))
}
if (req.url === '/jsontext') {
res.writeHead(200, {'content-type': 'text/plain'})
res.end(JSON.stringify({json: true}))
}
if (req.url === '/qstext') {
res.writeHead(200, {'content-type': 'text/html'})
res.end(qs.stringify({nested: {querystring: true}}))
}
})
server.listen(5000, done)
})
it('json', async () => {
var {body} = await request({url: 'http://localhost:5000/json'})
t.deepStrictEqual(body, {json: true})
})
it('querystring', async () => {
var {body} = await request({url: 'http://localhost:5000/qs'})
t.deepStrictEqual(body, {nested: {querystring: 'true'}})
})
it('json as text', async () => {
var {body} = await request({url: 'http://localhost:5000/jsontext'})
t.deepStrictEqual(body, {json: true})
})
it('querystring as text', async () => {
var {body} = await request({url: 'http://localhost:5000/qstext'})
t.deepStrictEqual(body, {nested: {querystring: 'true'}})
})
it('extend', async () => {
var {body} = await request({url: 'http://localhost:5000/qstext'})
t.deepStrictEqual(body, {nested: {querystring: 'true'}})
var {body} = await compose.client({url: 'http://localhost:5000/qstext'})
t.equal(body, 'nested%5Bquerystring%5D=true')
})
after((done) => {
server.close(done)
})
})
})