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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
'use strict';
var assert = require('assert');
var request = require('../');
request('GET', 'http://example.com', function (err, res) {
if (err) throw err;
console.log('response A');
assert(res.statusCode === 200);
res.body.resume();
});
request('GET', 'http://example.com:80', function (err, res) {
if (err) throw err;
console.log('response A1');
assert(res.statusCode === 200);
res.body.resume();
});
request('GET', 'https://www.promisejs.org', function (err, res) {
if (err) throw err;
console.log('response B');
assert(res.statusCode === 200);
res.body.resume();
});
request('GET', 'https://promisejs.org', {followRedirects: true}, function (err, res) {
if (err) throw err;
console.log('response C');
assert(res.statusCode === 200);
res.body.resume();
});
var CACHED = 'https://www.promisejs.org/polyfills/promise-done-1.0.0.js';
request('GET', CACHED, {cache: 'memory'}, function (err, res) {
if (err) throw err;
console.log('response D (populate cache)');
assert(res.statusCode === 200);
res.body.on('data', function () {});
res.body.on('end', function () {
request('GET', CACHED, {cache: 'memory'}, function (err, res) {
if (err) throw err;
console.log('response E (from cache)');
assert(res.fromCache === true);
assert(res.fromNotModified === false);
assert(res.statusCode === 200);
res.body.resume();
});
});
});
request('GET', CACHED, {cache: 'file'}, function (err, res) {
if (err) throw err;
console.log('response G (populate file cache)');
assert(res.statusCode === 200);
res.body.on('data', function () {});
res.body.on('end', function () {
setTimeout(function () {
request('GET', CACHED, {cache: 'file'}, function (err, res) {
if (err) throw err;
console.log('response H (from file cache)');
assert(res.fromCache === true);
assert(res.fromNotModified === false);
assert(res.statusCode === 200);
res.body.resume();
});
}, 1000);
});
});
request('GET', 'https://api.github.com/repos/isaacs/npm', {allowRedirectHeaders: ['User-Agent'], followRedirects: true, headers: {'User-Agent': 'http-basic'}}, function (err, res) {
if (err) throw err;
console.log('response I');
assert(res.statusCode === 200);
res.body.resume();
});
|