diff options
Diffstat (limited to 'node_modules/then-request/test')
| -rw-r--r-- | node_modules/then-request/test/browser.js | 53 | ||||
| -rw-r--r-- | node_modules/then-request/test/get-mock-response.js | 20 | ||||
| -rw-r--r-- | node_modules/then-request/test/index.js | 97 | ||||
| -rw-r--r-- | node_modules/then-request/test/server.js | 25 |
4 files changed, 195 insertions, 0 deletions
diff --git a/node_modules/then-request/test/browser.js b/node_modules/then-request/test/browser.js new file mode 100644 index 0000000..cd586c2 --- /dev/null +++ b/node_modules/then-request/test/browser.js @@ -0,0 +1,53 @@ +'use strict'; + +var assert = require('assert'); +var getResponse = require('./get-mock-response.js'); +var window = (global.window = {}); + +window.XMLHttpRequest = XMLHttpRequest; +function XMLHttpRequest() { + assert(arguments.length === 0); + this._headers = {}; + this._responseHeaders = ''; +}; +// XMLHttpRequest.prototype.onreadystatechange gets assigned by user +XMLHttpRequest.prototype.open = function (method, url, async) { + assert(async === true, 'All requests must be async'); + this._method = method; + this._url = url; +}; +XMLHttpRequest.prototype.setRequestHeader = function (name, value) { + this._headers[name] = value; +}; +XMLHttpRequest.prototype.send = function (body) { + assert(typeof body === 'string' || body === null, 'body must be a string or null'); + this.readyState = 3; + this.onreadystatechange(); + this.readyState = 4; + var res = getResponse(this._method, this._url, this._headers, body, {isClient: true}); + this.status = res.statusCode; + this._responseHeaders = Object.keys(res.headers).map(function (header) { + return header + ': ' + res.headers[header]; + }).join('\r\n'); + this.responseText = res.body; + this.onreadystatechange(); +}; +XMLHttpRequest.prototype.getAllResponseHeaders = function () { + return this._responseHeaders; +} +window.location = {}; +window.location.host = 'http://example.com'; + + +/* +if (xhr.readyState === 4) { + var headers = {}; + xhr.getAllResponseHeaders().split('\r\n').forEach(function (header) { + var h = header.split(':'); + if (h.length > 1) { + headers[h[0].toLowerCase()] = h.slice(1).join(':').trim(); + } + }); + resolve(new Response(xhr.status, headers, xhr.responseText)); +} +*/ diff --git a/node_modules/then-request/test/get-mock-response.js b/node_modules/then-request/test/get-mock-response.js new file mode 100644 index 0000000..cb58c4d --- /dev/null +++ b/node_modules/then-request/test/get-mock-response.js @@ -0,0 +1,20 @@ +'use strict'; + +var assert = require('assert'); +var Response = require('http-response-object'); + +module.exports = getResponse; +function getResponse(method, url, headers, body, options) { + var isClient = options.isClient; + if (method === 'GET' && url === 'http://example.com') { + return new Response(200, {FoO: 'bar'}, 'body'); + } + if (method === 'GET' && url === 'http://example.com?foo=baz') { + return new Response(200, {FoO: 'baz'}, 'body'); + } + if (method === 'POST' && url === 'http://example.com') { + assert(headers['Content-Type'] === 'application/json'); + assert(JSON.parse(body.toString()).foo === 'baz'); + return new Response(200, {}, 'json body'); + } +} diff --git a/node_modules/then-request/test/index.js b/node_modules/then-request/test/index.js new file mode 100644 index 0000000..019a2f8 --- /dev/null +++ b/node_modules/then-request/test/index.js @@ -0,0 +1,97 @@ +'use strict'; + +var assert = require('assert'); +var test = require('testit'); +var Promise = require('promise'); + +test('./lib/handle-qs.js', function () { + var handleQs = require('../lib/handle-qs.js'); + + assert(handleQs('http://example.com/', {}) === 'http://example.com/'); + assert(handleQs('http://example.com/?foo=bar', {}) === 'http://example.com/?foo=bar'); + assert(handleQs('http://example.com/', {foo: 'bar'}) === 'http://example.com/?foo=bar'); + assert(handleQs('http://example.com/', {foo: {bar: 'baz'}}) === 'http://example.com/?foo%5Bbar%5D=baz'); + assert(handleQs('http://example.com/', {foo: 'bar', bing: 'bong'}) === 'http://example.com/?foo=bar&bing=bong'); + assert(handleQs('http://example.com/?foo=bar', {bing: 'bong'}) === 'http://example.com/?foo=bar&bing=bong'); + assert(handleQs('http://example.com/?foo=bar#ding', {bing: 'bong'}) === 'http://example.com/?foo=bar&bing=bong#ding'); +}); + + +require('./browser.js'); +require('./server.js'); + +function testEnv(env) { + var request = require(env === 'browser' ? '../browser.js' : '../index.js'); + test(env + ' - GET', function () { + return request('GET', 'http://example.com').then(function (res) { + assert(res.statusCode === 200); + assert(res.headers['foo'] === 'bar'); + assert(res.body.toString() === 'body'); + }); + }); + test(env + ' - GET query', function () { + return request('GET', 'http://example.com', {qs: {foo: 'baz'}}).then(function (res) { + assert(res.statusCode === 200); + assert(res.headers['foo'] === 'baz'); + assert(res.body.toString() === 'body'); + }); + }); + test(env + ' - GET -> .getBody("utf8")', function () { + return request('GET', 'http://example.com').getBody('utf8').then(function (body) { + assert(body === 'body'); + }); + }); + test(env + ' - POST json', function () { + return request('POST', 'http://example.com', {json: {foo: 'baz'}}).then(function (res) { + assert(res.statusCode === 200); + assert(res.body.toString() === 'json body'); + }); + }); + + + test(env + ' - invalid method', function () { + return request({}, 'http://example.com').then(function (res) { + throw new Error('Expected an error'); + }, function (err) { + assert(err instanceof TypeError); + }); + }); + test(env + ' - invalid url', function () { + return request('GET', {}).then(function (res) { + throw new Error('Expected an error'); + }, function (err) { + assert(err instanceof TypeError); + }); + }); + test(env + ' - invalid options', function () { + return request('GET', 'http://example.com', 'options').then(function (res) { + throw new Error('Expected an error'); + }, function (err) { + assert(err instanceof TypeError); + }); + }); + test(env + ' - Callbacks', function () { + return new Promise(function (resolve, reject) { + return request('GET', 'http://example.com', function (err, res) { + if (err) return reject(err); + assert(res.statusCode === 200); + assert(res.headers['foo'] === 'bar'); + assert(res.body.toString() === 'body'); + resolve(null); + }); + }); + }); + test(env + ' - Callbacks', function () { + return new Promise(function (resolve, reject) { + return request('GET', 'http://example.com', {}, function (err, res) { + if (err) return reject(err); + assert(res.statusCode === 200); + assert(res.headers['foo'] === 'bar'); + assert(res.body.toString() === 'body'); + resolve(null); + }); + }); + }); +} +testEnv('browser'); +testEnv('server'); diff --git a/node_modules/then-request/test/server.js b/node_modules/then-request/test/server.js new file mode 100644 index 0000000..614bb39 --- /dev/null +++ b/node_modules/then-request/test/server.js @@ -0,0 +1,25 @@ +'use strict'; + +var assert = require('assert'); +var PassThrough = require('stream').PassThrough; +var getResponse = require('./get-mock-response.js'); + +require('../index.js')._request = function (method, url, options, callback) { + assert(typeof callback === 'function'); + var duplex = !(method === 'GET' || method === 'DELETE' || method === 'HEAD'); + if (duplex) { + return { + end: function (body) { + gotResponse(getResponse(method, url, options.headers, body, {isClient: false})); + } + }; + } else { + gotResponse(getResponse(method, url, options.headers, null, {isClient: false})); + } + function gotResponse(res) { + var stream = new PassThrough(); + stream.end(res.body); + res.body = stream; + callback(null, res); + } +}; |