source: trip-planner-front/node_modules/http-deceiver/test/api-test.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[6a3a178]1var assert = require('assert');
2var net = require('net');
3var http = require('http');
4var streamPair = require('stream-pair');
5var thing = require('handle-thing');
6
7var httpDeceiver = require('../');
8
9describe('HTTP Deceiver', function() {
10 var handle;
11 var pair;
12 var socket;
13 var deceiver;
14
15 beforeEach(function() {
16 pair = streamPair.create();
17 handle = thing.create(pair.other);
18 socket = new net.Socket({ handle: handle });
19
20 // For v0.8
21 socket.readable = true;
22 socket.writable = true;
23
24 deceiver = httpDeceiver.create(socket);
25 });
26
27 it('should emit request', function(done) {
28 var server = http.createServer();
29 server.emit('connection', socket);
30
31 server.on('request', function(req, res) {
32 assert.equal(req.method, 'PUT');
33 assert.equal(req.url, '/hello');
34 assert.deepEqual(req.headers, { a: 'b' });
35
36 done();
37 });
38
39 deceiver.emitRequest({
40 method: 'PUT',
41 path: '/hello',
42 headers: {
43 a: 'b'
44 }
45 });
46 });
47
48 it('should emit response', function(done) {
49 var agent = new http.Agent();
50 agent.createConnection = function createConnection() {
51 return socket;
52 };
53 var client = http.request({
54 method: 'POST',
55 path: '/ok',
56 agent: agent
57 }, function(res) {
58 assert.equal(res.statusCode, 421);
59 assert.deepEqual(res.headers, { a: 'b' });
60
61 done();
62 });
63
64 process.nextTick(function() {
65 deceiver.emitResponse({
66 status: 421,
67 reason: 'F',
68 headers: {
69 a: 'b'
70 }
71 });
72 });
73 });
74
75 it('should override .execute and .finish', function(done) {
76 var server = http.createServer();
77 server.emit('connection', socket);
78
79 server.on('request', function(req, res) {
80 assert.equal(req.method, 'PUT');
81 assert.equal(req.url, '/hello');
82 assert.deepEqual(req.headers, { a: 'b' });
83
84 var actual = '';
85 req.on('data', function(chunk) {
86 actual += chunk;
87 });
88 req.once('end', function() {
89 assert.equal(actual, 'hello world');
90 done();
91 });
92 });
93
94 deceiver.emitRequest({
95 method: 'PUT',
96 path: '/hello',
97 headers: {
98 a: 'b'
99 }
100 });
101
102 pair.write('hello');
103 pair.end(' world');
104 });
105
106 it('should work with reusing parser', function(done) {
107 var server = http.createServer();
108 server.emit('connection', socket);
109
110 function secondRequest() {
111 pair = streamPair.create();
112 handle = thing.create(pair.other);
113 socket = new net.Socket({ handle: handle });
114
115 // For v0.8
116 socket.readable = true;
117 socket.writable = true;
118
119 server.emit('connection', socket);
120
121 pair.end('PUT /second HTTP/1.1\r\nContent-Length:11\r\n\r\nhello world');
122 }
123
124 server.on('request', function(req, res) {
125 var actual = '';
126 req.on('data', function(chunk) {
127 actual += chunk;
128 });
129 req.once('end', function() {
130 assert.equal(actual, 'hello world');
131
132 if (req.url === '/first')
133 secondRequest();
134 else
135 done();
136 });
137 });
138
139 deceiver.emitRequest({
140 method: 'PUT',
141 path: '/first',
142 headers: {
143 a: 'b'
144 }
145 });
146
147 pair.write('hello');
148 pair.end(' world');
149 });
150
151 it('should emit CONNECT request', function(done) {
152 var server = http.createServer();
153 server.emit('connection', socket);
154
155 server.on('connect', function(req, socket, bodyHead) {
156 assert.equal(req.method, 'CONNECT');
157 assert.equal(req.url, '/hello');
158
159 done();
160 });
161
162 deceiver.emitRequest({
163 method: 'CONNECT',
164 path: '/hello',
165 headers: {
166 }
167 });
168 });
169
170 it('should emit Upgrade request', function(done) {
171 var server = http.createServer();
172 server.emit('connection', socket);
173
174 server.on('upgrade', function(req, socket, bodyHead) {
175 assert.equal(req.method, 'POST');
176 assert.equal(req.url, '/hello');
177
178 socket.on('data', function(chunk) {
179 assert.equal(chunk + '', 'hm');
180 done();
181 });
182 });
183
184 deceiver.emitRequest({
185 method: 'POST',
186 path: '/hello',
187 headers: {
188 'upgrade': 'websocket'
189 }
190 });
191
192 pair.write('hm');
193 });
194
195 it('should emit Upgrade response', function(done) {
196 var agent = new http.Agent();
197 agent.createConnection = function createConnection() {
198 return socket;
199 };
200 var client = http.request({
201 method: 'POST',
202 path: '/ok',
203 headers: {
204 connection: 'upgrade',
205 upgrade: 'websocket'
206 },
207 agent: agent
208 }, function(res) {
209 assert(false);
210 });
211 client.on('upgrade', function(res, socket) {
212 assert.equal(res.statusCode, 421);
213 done();
214 });
215
216 process.nextTick(function() {
217 deceiver.emitResponse({
218 status: 421,
219 reason: 'F',
220 headers: {
221 upgrade: 'websocket'
222 }
223 });
224 });
225 });
226});
Note: See TracBrowser for help on using the repository browser.