mirror of https://github.com/lukechilds/got.git
Sindre Sorhus
10 years ago
13 changed files with 347 additions and 360 deletions
@ -1,57 +1,58 @@ |
|||
'use strict'; |
|||
var test = require('ava'); |
|||
var got = require('../'); |
|||
var server = require('./server.js'); |
|||
var s = server.createServer(); |
|||
import test from 'ava'; |
|||
import got from '../'; |
|||
import {createServer} from './server.js'; |
|||
|
|||
s.on('/', function (req, res) { |
|||
const s = createServer(); |
|||
|
|||
s.on('/', (req, res) => { |
|||
res.statusCode = 404; |
|||
res.end(); |
|||
}); |
|||
|
|||
s.on('/test', function (req, res) { |
|||
s.on('/test', (req, res) => { |
|||
res.end(req.url); |
|||
}); |
|||
|
|||
s.on('/?test=wow', function (req, res) { |
|||
s.on('/?test=wow', (req, res) => { |
|||
res.end(req.url); |
|||
}); |
|||
|
|||
test.before('arguments - setup', function (t) { |
|||
s.listen(s.port, function () { |
|||
t.end(); |
|||
}); |
|||
test.before('arguments - setup', t => { |
|||
s.listen(s.port, () => t.end()); |
|||
}); |
|||
|
|||
test('arguments - url argument is required', function (t) { |
|||
test('arguments - url argument is required', t => { |
|||
t.plan(2); |
|||
t.throws(function () { |
|||
got(undefined, function () {}); |
|||
t.throws(() => { |
|||
got(undefined, () => {}); |
|||
}, /Parameter `url` must be a string or object, not undefined/); |
|||
|
|||
got() |
|||
.catch(function (err) { |
|||
t.regexTest(/Parameter `url` must be a string or object, not undefined/, err.message); |
|||
}); |
|||
got().catch(err => { |
|||
t.regexTest(/Parameter `url` must be a string or object, not undefined/, err.message); |
|||
}); |
|||
}); |
|||
|
|||
test('arguments - accepts url.parse object as first argument', function (t) { |
|||
got({hostname: s.host, port: s.port, path: '/test'}, function (err, data) { |
|||
test('arguments - accepts url.parse object as first argument', t => { |
|||
got({ |
|||
hostname: s.host, |
|||
port: s.port, |
|||
path: '/test' |
|||
}, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, '/test'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test('arguments - overrides querystring from opts', function (t) { |
|||
got(s.url + '/?test=doge', {query: {test: 'wow'}}, function (err, data) { |
|||
test('arguments - overrides querystring from opts', t => { |
|||
got(`${s.url}/?test=doge`, {query: {test: 'wow'}}, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, '/?test=wow'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test.after('arguments - cleanup', function (t) { |
|||
test.after('arguments - cleanup', t => { |
|||
s.close(); |
|||
t.end(); |
|||
}); |
|||
|
@ -1,72 +1,70 @@ |
|||
'use strict'; |
|||
var test = require('ava'); |
|||
var got = require('../'); |
|||
var server = require('./server.js'); |
|||
var s = server.createServer(); |
|||
import test from 'ava'; |
|||
import got from '../'; |
|||
import {createServer} from './server.js'; |
|||
|
|||
s.on('/', function (req, res) { |
|||
const s = createServer(); |
|||
|
|||
s.on('/', (req, res) => { |
|||
res.end(JSON.stringify(req.headers)); |
|||
}); |
|||
|
|||
test.before('headers - setup', function (t) { |
|||
s.listen(s.port, function () { |
|||
t.end(); |
|||
}); |
|||
test.before('headers - setup', t => { |
|||
s.listen(s.port, () => t.end()); |
|||
}); |
|||
|
|||
test('headers - send user-agent header by default', function (t) { |
|||
got(s.url, function (err, data) { |
|||
test('headers - send user-agent header by default', t => { |
|||
got(s.url, (err, data) => { |
|||
t.ifError(err); |
|||
|
|||
var headers = JSON.parse(data); |
|||
const headers = JSON.parse(data); |
|||
|
|||
t.is(headers['user-agent'], 'https://github.com/sindresorhus/got'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test('headers - send accept-encoding header by default', function (t) { |
|||
got(s.url, function (err, data) { |
|||
test('headers - send accept-encoding header by default', t => { |
|||
got(s.url, (err, data) => { |
|||
t.ifError(err); |
|||
|
|||
var headers = JSON.parse(data); |
|||
const headers = JSON.parse(data); |
|||
|
|||
t.is(headers['accept-encoding'], 'gzip,deflate'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test('headers - send accept header with json option', function (t) { |
|||
got(s.url, {json: true}, function (err, headers) { |
|||
test('headers - send accept header with json option', t => { |
|||
got(s.url, {json: true}, (err, headers) => { |
|||
t.ifError(err); |
|||
t.is(headers.accept, 'application/json'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test('headers - send host header by default', function (t) { |
|||
got(s.url, function (err, data) { |
|||
test('headers - send host header by default', t => { |
|||
got(s.url, (err, data) => { |
|||
t.ifError(err); |
|||
|
|||
var headers = JSON.parse(data); |
|||
const headers = JSON.parse(data); |
|||
|
|||
t.is(headers.host, 'localhost:' + s.port); |
|||
t.is(headers.host, `localhost:${s.port}`); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test('headers - transform headers names to lowercase', function (t) { |
|||
got(s.url, {headers: {'USER-AGENT': 'test'}}, function (err, data) { |
|||
test('headers - transform headers names to lowercase', t => { |
|||
got(s.url, {headers: {'USER-AGENT': 'test'}}, (err, data) => { |
|||
t.ifError(err); |
|||
|
|||
var headers = JSON.parse(data); |
|||
const headers = JSON.parse(data); |
|||
|
|||
t.is(headers['user-agent'], 'test'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test.after('headers - cleanup', function (t) { |
|||
test.after('headers - cleanup', t => { |
|||
s.close(); |
|||
t.end(); |
|||
}); |
|||
|
@ -1,52 +1,47 @@ |
|||
'use strict'; |
|||
var test = require('ava'); |
|||
var got = require('../'); |
|||
var server = require('./server.js'); |
|||
var s = server.createServer(); |
|||
import test from 'ava'; |
|||
import got from '../'; |
|||
import {createServer} from './server.js'; |
|||
|
|||
s.on('/', function (req, res) { |
|||
const s = createServer(); |
|||
|
|||
s.on('/', (req, res) => { |
|||
res.end('ok'); |
|||
}); |
|||
|
|||
s.on('/404', function (req, res) { |
|||
s.on('/404', (req, res) => { |
|||
res.statusCode = 404; |
|||
res.end('not found'); |
|||
}); |
|||
|
|||
test.before('helpers - setup', function (t) { |
|||
s.listen(s.port, function () { |
|||
t.end(); |
|||
}); |
|||
test.before('helpers - setup', t => { |
|||
s.listen(s.port, () => t.end()); |
|||
}); |
|||
|
|||
test('helpers - callback mode', function (t) { |
|||
got.get(s.url, function (err, data) { |
|||
test('helpers - callback mode', t => { |
|||
got.get(s.url, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, 'ok'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test('helpers - promise mode', function (t) { |
|||
test('helpers - promise mode', t => { |
|||
t.plan(3); |
|||
|
|||
got.get(s.url) |
|||
.then(function (res) { |
|||
t.is(res.body, 'ok'); |
|||
}); |
|||
got.get(s.url).then(res => { |
|||
t.is(res.body, 'ok'); |
|||
}); |
|||
|
|||
got.get(s.url + '/404') |
|||
.catch(function (err) { |
|||
t.is(err.response.body, 'not found'); |
|||
}); |
|||
got.get(`${s.url}/404`).catch(err => { |
|||
t.is(err.response.body, 'not found'); |
|||
}); |
|||
|
|||
got.get('.com') |
|||
.catch(function (err) { |
|||
t.ok(err); |
|||
}); |
|||
got.get('.com').catch(err => { |
|||
t.ok(err); |
|||
}); |
|||
}); |
|||
|
|||
test.after('helpers - cleanup', function (t) { |
|||
test.after('helpers - cleanup', t => { |
|||
s.close(); |
|||
t.end(); |
|||
}); |
|||
|
@ -1,110 +1,141 @@ |
|||
'use strict'; |
|||
var test = require('ava'); |
|||
var intoStream = require('into-stream'); |
|||
var got = require('../'); |
|||
var server = require('./server.js'); |
|||
var s = server.createServer(); |
|||
|
|||
s.on('/', function (req, res) { |
|||
import test from 'ava'; |
|||
import intoStream from 'into-stream'; |
|||
import got from '../'; |
|||
import {createServer} from './server.js'; |
|||
|
|||
const s = createServer(); |
|||
|
|||
s.on('/', (req, res) => { |
|||
res.setHeader('method', req.method); |
|||
req.pipe(res); |
|||
}); |
|||
|
|||
s.on('/headers', function (req, res) { |
|||
s.on('/headers', (req, res) => { |
|||
res.end(JSON.stringify(req.headers)); |
|||
}); |
|||
|
|||
s.on('/empty', function (req, res) { |
|||
s.on('/empty', (req, res) => { |
|||
res.end(); |
|||
}); |
|||
|
|||
test.before('post - setup', function (t) { |
|||
s.listen(s.port, function () { |
|||
t.end(); |
|||
}); |
|||
test.before('post - setup', t => { |
|||
s.listen(s.port, () => t.end()); |
|||
}); |
|||
|
|||
test('post - GET can have body', function (t) { |
|||
test('post - GET can have body', t => { |
|||
t.plan(3); |
|||
|
|||
got.get(s.url, {body: 'hi'}, function (err, data, res) { |
|||
got.get(s.url, {body: 'hi'}, (err, data, res) => { |
|||
t.ifError(err); |
|||
t.is(data, 'hi'); |
|||
t.is(res.headers.method, 'GET'); |
|||
}); |
|||
}); |
|||
|
|||
test('post - send data from options with post request', function (t) { |
|||
test('post - send data from options with post request', t => { |
|||
t.plan(6); |
|||
|
|||
got(s.url, {body: 'wow'}, function (err, data) { |
|||
got(s.url, {body: 'wow'}, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, 'wow'); |
|||
}); |
|||
|
|||
got(s.url, {body: new Buffer('wow')}, function (err, data) { |
|||
got(s.url, {body: new Buffer('wow')}, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, 'wow'); |
|||
}); |
|||
|
|||
got(s.url, {body: intoStream(['wow'])}, function (err, data) { |
|||
got(s.url, {body: intoStream(['wow'])}, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, 'wow'); |
|||
}); |
|||
}); |
|||
|
|||
test('post - works with empty post response', function (t) { |
|||
got(s.url + '/empty', {body: 'wow'}, function (err, data) { |
|||
test('post - works with empty post response', t => { |
|||
got(`${s.url}/empty`, {body: 'wow'}, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, ''); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test('post - post have content-length header to string', function (t) { |
|||
test('post - post have content-length header to string', t => { |
|||
t.plan(10); |
|||
|
|||
got(s.url + '/headers', {body: 'wow', json: true}, function (err, headers) { |
|||
got(`${s.url}/headers`, { |
|||
body: 'wow', |
|||
json: true |
|||
}, (err, headers) => { |
|||
t.ifError(err); |
|||
t.is(headers['content-length'], '3'); |
|||
}); |
|||
|
|||
got(s.url + '/headers', {body: new Buffer('wow'), json: true}, function (err, headers) { |
|||
got(`${s.url}/headers`, { |
|||
body: new Buffer('wow'), |
|||
json: true |
|||
}, (err, headers) => { |
|||
t.ifError(err); |
|||
t.is(headers['content-length'], '3'); |
|||
}); |
|||
|
|||
got(s.url + '/headers', {body: intoStream(['wow']), json: true}, function (err, headers) { |
|||
got(`${s.url}/headers`, { |
|||
body: intoStream(['wow']), |
|||
json: true |
|||
}, (err, headers) => { |
|||
t.ifError(err); |
|||
t.is(headers['content-length'], undefined); |
|||
}); |
|||
|
|||
got(s.url + '/headers', {body: 'wow', json: true, headers: {'content-length': '10'}}, function (err, headers) { |
|||
got(`${s.url}/headers`, { |
|||
body: 'wow', |
|||
json: true, |
|||
headers: { |
|||
'content-length': '10' |
|||
} |
|||
}, (err, headers) => { |
|||
t.ifError(err); |
|||
t.is(headers['content-length'], '10'); |
|||
}); |
|||
|
|||
got(s.url + '/headers', {body: '3\r\nwow\r\n0\r\n', json: true, headers: {'transfer-encoding': 'chunked'}}, function (err, headers) { |
|||
got(`${s.url}/headers`, { |
|||
body: '3\r\nwow\r\n0\r\n', |
|||
json: true, |
|||
headers: { |
|||
'transfer-encoding': 'chunked' |
|||
} |
|||
}, (err, headers) => { |
|||
t.ifError(err); |
|||
t.is(headers['content-length'], undefined); |
|||
}); |
|||
}); |
|||
|
|||
test('post - works with plain object in body', function (t) { |
|||
test('post - works with plain object in body', t => { |
|||
t.plan(4); |
|||
|
|||
got(s.url, {body: {such: 'wow'}}, function (err, data) { |
|||
got(s.url, { |
|||
body: { |
|||
such: 'wow' |
|||
} |
|||
}, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, 'such=wow'); |
|||
}); |
|||
|
|||
got(s.url + '/headers', {headers: {'content-type': 'doge'}, body: {such: 'wow'}, json: true}, function (err, headers) { |
|||
got(`${s.url}/headers`, { |
|||
headers: { |
|||
'content-type': 'doge' |
|||
}, |
|||
body: { |
|||
such: 'wow' |
|||
}, |
|||
json: true |
|||
}, (err, headers) => { |
|||
t.ifError(err); |
|||
t.is(headers['content-type'], 'doge'); |
|||
}); |
|||
}); |
|||
|
|||
test.after('post - cleanup', function (t) { |
|||
test.after('post - cleanup', t => { |
|||
s.close(); |
|||
t.end(); |
|||
}); |
|||
|
@ -1,45 +1,42 @@ |
|||
'use strict'; |
|||
var tempfile = require('tempfile'); |
|||
var format = require('util').format; |
|||
var test = require('ava'); |
|||
var got = require('../'); |
|||
var server = require('./server.js'); |
|||
var s = server.createServer(); |
|||
import {format} from 'util'; |
|||
import tempfile from 'tempfile'; |
|||
import test from 'ava'; |
|||
import got from '../'; |
|||
import {createServer} from './server.js'; |
|||
|
|||
var socketPath = tempfile('.socket'); |
|||
const s = createServer(); |
|||
const socketPath = tempfile('.socket'); |
|||
|
|||
s.on('/', function (req, res) { |
|||
s.on('/', (req, res) => { |
|||
res.end('ok'); |
|||
}); |
|||
|
|||
test.before('unix-socket - setup', function (t) { |
|||
s.listen(socketPath, function () { |
|||
t.end(); |
|||
}); |
|||
test.before('unix-socket - setup', t => { |
|||
s.listen(socketPath, () => t.end()); |
|||
}); |
|||
|
|||
test('unix-socket - request via unix socket', function (t) { |
|||
test('unix-socket - request via unix socket', t => { |
|||
// borrow unix domain socket url format from request module
|
|||
var url = format('http://unix:%s:%s', socketPath, '/'); |
|||
const url = format('http://unix:%s:%s', socketPath, '/'); |
|||
|
|||
got(url, function (err, data) { |
|||
got(url, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, 'ok'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test('unix-socket - protocol-less request', function (t) { |
|||
var url = format('unix:%s:%s', socketPath, '/'); |
|||
test('unix-socket - protocol-less request', t => { |
|||
const url = format('unix:%s:%s', socketPath, '/'); |
|||
|
|||
got(url, function (err, data) { |
|||
got(url, (err, data) => { |
|||
t.ifError(err); |
|||
t.is(data, 'ok'); |
|||
t.end(); |
|||
}); |
|||
}); |
|||
|
|||
test.after('unix-socket - cleanup', function (t) { |
|||
test.after('unix-socket - cleanup', t => { |
|||
s.close(); |
|||
t.end(); |
|||
}); |
|||
|
Loading…
Reference in new issue