node_modules

This commit is contained in:
saeid
2019-07-15 13:07:31 +04:30
parent d3735c6d30
commit ce1a3ebf9c
35713 changed files with 3639219 additions and 1 deletions
+147
View File
@@ -0,0 +1,147 @@
var needle = require('../'),
sinon = require('sinon'),
should = require('should'),
http = require('http'),
Emitter = require('events').EventEmitter,
helpers = require('./helpers');
var get_catch = function(url, opts) {
var err;
try {
needle.get(url, opts);
} catch(e) {
err = e;
}
return err;
}
describe('errors', function(){
describe('null URL', function(){
it('throws', function(){
var ex = get_catch(); // null
should.exist(ex);
ex.message.should.include('Cannot call method');
})
})
/*
describe('invalid protocol', function(){
var url = 'foo://www.google.com/what'
it('throws', function(){
var ex = get_catch(url);
should.exist(ex);
})
})
describe('invalid host', function(){
var url = 'http://s1\\\2.com/'
it('throws', function(){
var ex = get_catch(url);
should.exist(ex);
})
})
describe('invalid path', function(){
var url = 'http://www.google.com\\\/x\\\ /x2.com/'
it('throws', function(){
var ex = get_catch(url);
should.exist(ex);
})
})
*/
describe('when host does not exist', function(){
var url = 'http://unexistinghost/foo';
it('does not throw', function(){
var ex = get_catch(url);
should.not.exist(ex);
})
it('callbacks an error', function(done){
needle.get(url, function(err){
err.should.an.instanceOf(Error);
done();
})
})
it('error should be ENOTFOUND', function(done){
needle.get(url, function(err){
err.code.should.match(/ENOTFOUND|EADDRINFO/)
done();
})
})
it('does not callback a response', function(done){
needle.get(url, function(err, resp){
should.not.exist(resp);
done();
})
})
})
describe('when request timeouts', function(){
var server,
url = 'http://localhost:3333/foo';
before(function(){
server = helpers.server({ port: 3333, wait: 1000 });
})
after(function(){
server.close();
})
it('aborts the request', function(done){
var time = new Date();
needle.get(url, { timeout: 200 }, function(err){
var timediff = (new Date() - time);
timediff.should.be.within(200, 250);
done();
})
})
it('callbacks an error', function(done){
needle.get(url, { timeout: 200 }, function(err){
err.should.an.instanceOf(Error);
done();
})
})
it('error should be ECONNRESET', function(done){
needle.get(url, { timeout: 200 }, function(err){
err.code.should.equal('ECONNRESET')
done();
})
})
it('does not callback a response', function(done){
needle.get(url, { timeout: 200 }, function(err, resp){
should.not.exist(resp);
done();
})
})
})
})
+27
View File
@@ -0,0 +1,27 @@
var http = require('http');
var helpers = {};
helpers.server = function(opts) {
var default_headers = {'Content-Type': 'text/html'};
var finish = function(req, res) {
res.writeHead(opts.code || 200, opts.headers || default_headers);
res.end(opts.response || 'Hello there.');
}
var server = http.createServer(function(req, res){
setTimeout(function(){
finish(req, res);
}, opts.wait || 0);
})
server.listen(opts.port);
return server;
}
module.exports = helpers;
+48
View File
@@ -0,0 +1,48 @@
var needle = require('./..'),
should = require('should'),
xml2js_present;
try {
require('xml2js');
xml2js_present = true;
} catch(e) {
xml2js_present = false;
}
describe('parsing', function() {
var url, opts = {};
if (!xml2js_present)
return;
describe('when xml2js is present', function(){
describe('and parse option is true', function(){
before(function() {
opts.parse = true;
})
describe('and a valid XML is requested', function(){
url = 'https://news.ycombinator.com/rss';
it('parses it', function(done) {
needle.get(url, opts, function(err, resp, body){
resp.statusCode.should.equal(200);
should.exist(resp.body.rss);
done();
})
});
});
});
});
})
+17
View File
@@ -0,0 +1,17 @@
var formidable = require('formidable'),
http = require('http'),
util = require('util');
var port = process.argv[2] || 8888;
http.createServer(function(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
console.log(util.inspect({fields: fields, files: files}))
res.end(util.inspect({fields: fields, files: files}));
});
}).listen(port);
console.log('HTTP server listening on port ' + port);
+62
View File
@@ -0,0 +1,62 @@
var http = require('http'),
https = require('https'),
url = require('url');
var port = 1234,
log = true,
request_auth = false;
http.createServer(function(request, response) {
console.log(request.headers);
console.log("Got request: " + request.url);
console.log("Forwarding request to " + request.headers['host']);
if (request_auth) {
if (!request.headers['proxy-authorization']) {
response.writeHead(407, {'Proxy-Authenticate': 'Basic realm="proxy.com"'})
return response.end('Hello.');
}
}
var remote = url.parse(request.url);
var protocol = remote.protocol == 'https:' ? https : http;
var opts = {
host: request.headers['host'],
port: remote.port || (remote.protocol == 'https:' ? 443 : 80),
method: request.method,
path: remote.pathname,
headers: request.headers
}
var proxy_request = protocol.request(opts, function(proxy_response){
proxy_response.on('data', function(chunk) {
if (log) console.log(chunk.toString());
response.write(chunk, 'binary');
});
proxy_response.on('end', function() {
response.end();
});
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
request.on('data', function(chunk) {
if (log) console.log(chunk.toString());
proxy_request.write(chunk, 'binary');
});
request.on('end', function() {
proxy_request.end();
});
}).listen(port);
process.on('uncaughtException', function(err){
console.log('Uncaught exception!');
console.log(err);
});
console.log("Proxy server listening on port " + port);
+104
View File
@@ -0,0 +1,104 @@
// TODO: write specs. :)
var fs = require('fs'),
client = require('./../../');
process.env.DEBUG = true;
var response_callback = function(err, resp, body){
console.log(err);
if(resp) console.log("Got status code " + resp.statusCode)
console.log(body);
}
function simple_head(){
client.head('http://www.amazon.com', response_callback);
}
function simple_get(){
client.get('http://www.nodejs.org', response_callback);
}
function proxy_get(){
client.get('https://www.google.com/search?q=nodejs', {proxy: 'http://localhost:1234'}, response_callback);
}
function auth_get(){
client.get('https://www.twitter.com', {username: 'asd', password: '123'}, response_callback);
}
function simple_post(url){
var data = {
foo: 'bar',
baz: {
nested: 'attribute'
}
}
client.post(url, data, response_callback);
}
function multipart_post(url){
var filename = 'test_file.txt';
var data = 'Plain text data.\nLorem ipsum dolor sit amet.\nBla bla bla.\n';
fs.writeFileSync(filename, data);
var black_pixel = new Buffer("data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=".replace(/^data:image\/\w+;base64,/, ""), "base64");
var data = {
foo: 'bar',
bar: 'baz',
nested: {
my_document: { file: filename, content_type: 'text/plain' },
even: {
more: 'nesting'
}
},
pixel: { filename: 'black_pixel.gif', buffer: black_pixel, content_type: 'image/gif' },
field2: {value: JSON.stringify({"json":[ {"one":1}, {"two":2} ]}), content_type: 'application/json' }
}
client.post(url, data, {multipart: true}, function(err, resp, body){
console.log(err);
console.log("Got status code " + resp.statusCode)
console.log(body);
fs.unlink(filename);
});
}
switch(process.argv[2]){
case 'head':
simple_head();
break;
case 'get':
simple_get();
break;
case 'auth':
auth_get();
break;
case 'proxy':
proxy_get();
break;
case 'post':
simple_post(process.argv[3] || 'http://posttestserver.com/post.php');
break;
case 'multipart':
multipart_post(process.argv[3] || 'http://posttestserver.com/post.php?dir=example');
break;
case 'all':
simple_head();
simple_get();
auth_get();
proxy_get();
simple_post(process.argv[3] || 'http://posttestserver.com/post.php');
multipart_post(process.argv[3] || 'http://posttestserver.com/post.php?dir=example');
break;
default:
console.log("Usage: ./test.js [head|get|auth|proxy|multipart]")
}