first commit

This commit is contained in:
2026-04-09 13:05:27 +02:00
commit 3bbd7d6413
3084 changed files with 84284 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`http://httpbin.org/post 1`] = `
Object {
"args": Object {},
"data": "<body/>",
"files": Object {},
"form": Object {},
"headers": Object {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "7",
"Host": "httpbin.org",
},
"json": null,
"url": "http://httpbin.org/post",
}
`;
exports[`http://httpbin.org/post form 1`] = `
Object {
"args": Object {},
"data": "",
"files": Object {},
"form": Object {
"foo": "bar",
},
"headers": Object {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "161",
"Host": "httpbin.org",
},
"json": null,
"url": "http://httpbin.org/post",
}
`;
exports[`http://httpbin.org/post json 1`] = `
Object {
"args": Object {},
"data": "{\\"foo\\":\\"bar\\"}",
"files": Object {},
"form": Object {},
"headers": Object {
"Accept-Encoding": "gzip,deflate",
"Connection": "close",
"Content-Length": "13",
"Content-Type": "application/json",
"Host": "httpbin.org",
},
"json": Object {
"foo": "bar",
},
"url": "http://httpbin.org/post",
}
`;

View File

@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`DELETE request 1`] = `"ok"`;
exports[`GET request 1`] = `"ok"`;
exports[`POST request 1`] = `"ok"`;
exports[`PUT request 1`] = `"ok"`;

View File

@@ -0,0 +1,7 @@
'use strict';
const http = require('http');
http.createServer(function (req, res, next) {
res.end('Hello World');
}).listen(3045);

View File

@@ -0,0 +1,42 @@
'use strict';
const spawn = require('child_process').spawn;
const spawnSync = require('child_process').spawnSync;
const thenRequest = require('then-request');
const syncRequest = require('../');
const server = spawn(process.execPath, [require.resolve('./benchmark-server.js')]);
setTimeout(() => {
let asyncDuration, syncDuration;
let ready = Promise.resolve(null);
const startAsync = Date.now();
for (let i = 0; i < 1000; i++) {
ready = ready.then(function () {
return thenRequest('get', 'http://localhost:3045');
});
}
ready.then(function () {
const endAsync = Date.now();
asyncDuration = endAsync - startAsync;
console.log('1000 async requests in: ' + asyncDuration);
const startSync = Date.now();
for (let i = 0; i < 500; i++) {
syncRequest('get', 'http://localhost:3045');
}
const endSync = Date.now();
syncDuration = endSync - startSync;
console.log('1000 sync requests in: ' + syncDuration);
}).then(() => {
server.kill();
if (syncDuration > (asyncDuration * 10)) {
console.error('This is more than 10 times slower than using async requests, that is not good enough.');
process.exit(1);
}
process.exit(0);
}, function (err) {
console.error(err.stack);
process.exit(1);
});
ready = null;
}, 1000);

View File

@@ -0,0 +1,54 @@
var request = require('../');
var FormData = request.FormData;
// Test GET request
test('http://nodejs.org', () => {
var res = request('GET', 'http://nodejs.org');
expect(res.statusCode).toBe(200);
expect(res.url).toBe('https://nodejs.org/en/');
});
test('http://httpbin.org/post', () => {
var res = JSON.parse(
request('POST', 'http://httpbin.org/post', {
body: '<body/>',
}).getBody('utf8')
);
delete res.origin;
expect(res).toMatchSnapshot();
});
test('http://httpbin.org/post json', () => {
var res = JSON.parse(
request('POST', 'http://httpbin.org/post', {
json: {foo: 'bar'},
}).getBody('utf8')
);
delete res.origin;
expect(res).toMatchSnapshot();
});
test('http://httpbin.org/post form', () => {
var fd = new FormData();
fd.append('foo', 'bar');
var res = JSON.parse(
request('POST', 'http://httpbin.org/post', {
form: fd,
}).getBody('utf8')
);
delete res.headers['Content-Type'];
delete res.origin;
expect(res).toMatchSnapshot();
});
test('https://expired.badssl.com', () => {
var errored = false;
try {
// Test unauthorized HTTPS GET request
var res = request('GET', 'https://expired.badssl.com');
} catch (ex) {
return;
}
throw new Error('Should have rejected unauthorized https get request');
});

View File

@@ -0,0 +1,42 @@
'use strict';
var express = require('express'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
PORT = 3030;
var app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}));
// parse application/json
app.use(bodyParser.json());
// configure log
app.use(morgan('dev'));
var started = false;
exports.isStarted = function() {
return started;
};
var server;
process.on('message', function(m) {
if (m === 'start') {
server = app.listen(PORT, function() {
started = true;
return process.send('started');
});
} else {
server.close(function() {
started = false;
return process.send('closed') && process.exit(0);
});
}
});
['get', 'post', 'put', 'delete'].forEach(function(method) {
app.route('/internal-test')[method](function(req, res) {
res.send('ok');
});
});

View File

@@ -0,0 +1,61 @@
'use strict';
if (process.env.SYNC_REQUEST_LEGACY) {
// break PATH so running `nc` will fail.
process.env.PATH = '';
}
var request = require('../');
var FormData = request.FormData;
const fork = require('child_process').fork;
var server = fork(__dirname + '/fake-server', {stdio: 'pipe'});
test('start server', () => {
return new Promise(resolve => {
server.on('message', m => {
if (m === 'started') {
resolve();
}
});
server.send('start');
});
});
test('GET request', () => {
var res = request('GET', 'http://localhost:3030/internal-test', {
timeout: 2000,
});
expect(res.statusCode).toBe(200);
expect(res.getBody('utf8')).toMatchSnapshot();
});
test('POST request', () => {
var res = request('POST', 'http://localhost:3030/internal-test', {
timeout: 2000,
body: '<body/>',
});
expect(res.statusCode).toBe(200);
expect(res.getBody('utf8')).toMatchSnapshot();
});
test('PUT request', () => {
var res = request('PUT', 'http://localhost:3030/internal-test', {
timeout: 2000,
body: '<body/>',
});
expect(res.statusCode).toBe(200);
expect(res.getBody('utf8')).toMatchSnapshot();
});
test('DELETE request', () => {
var res = request('DELETE', 'http://localhost:3030/internal-test', {
timeout: 2000,
});
expect(res.statusCode).toBe(200);
expect(res.getBody('utf8')).toMatchSnapshot();
});
test('stop server', () => {
server.send('stop');
});