Best JavaScript code snippet using sinon
http.js
Source:http.js
...23 delete process.env.no_proxy;24 }25 });26 it('should respect the timeout property', function (done) {27 server = http.createServer(function (req, res) {28 setTimeout(function () {29 res.end();30 }, 1000);31 }).listen(4444, function () {32 var success = false, failure = false;33 var error;34 axios.get('http://localhost:4444/', {35 timeout: 25036 }).then(function (res) {37 success = true;38 }).catch(function (err) {39 error = err;40 failure = true;41 });42 setTimeout(function () {43 assert.equal(success, false, 'request should not succeed');44 assert.equal(failure, true, 'request should fail');45 assert.equal(error.code, 'ECONNABORTED');46 assert.equal(error.message, 'timeout of 250ms exceeded');47 done();48 }, 300);49 });50 });51 it('should allow passing JSON', function (done) {52 var data = {53 firstName: 'Fred',54 lastName: 'Flintstone',55 emailAddr: 'fred@example.com'56 };57 server = http.createServer(function (req, res) {58 res.setHeader('Content-Type', 'application/json;charset=utf-8');59 res.end(JSON.stringify(data));60 }).listen(4444, function () {61 axios.get('http://localhost:4444/').then(function (res) {62 assert.deepEqual(res.data, data);63 done();64 });65 });66 });67 it('should redirect', function (done) {68 var str = 'test response';69 server = http.createServer(function (req, res) {70 var parsed = url.parse(req.url);71 if (parsed.pathname === '/one') {72 res.setHeader('Location', '/two');73 res.statusCode = 302;74 res.end();75 } else {76 res.end(str);77 }78 }).listen(4444, function () {79 axios.get('http://localhost:4444/one').then(function (res) {80 assert.equal(res.data, str);81 assert.equal(res.request.path, '/two');82 done();83 });84 });85 });86 it('should not redirect', function (done) {87 server = http.createServer(function (req, res) {88 res.setHeader('Location', '/foo');89 res.statusCode = 302;90 res.end();91 }).listen(4444, function () {92 axios.get('http://localhost:4444/', {93 maxRedirects: 0,94 validateStatus: function () {95 return true;96 }97 }).then(function (res) {98 assert.equal(res.status, 302);99 assert.equal(res.headers['location'], '/foo');100 done();101 });102 });103 });104 it('should support max redirects', function (done) {105 var i = 1;106 server = http.createServer(function (req, res) {107 res.setHeader('Location', '/' + i);108 res.statusCode = 302;109 res.end();110 i++;111 }).listen(4444, function () {112 axios.get('http://localhost:4444/', {113 maxRedirects: 3114 }).catch(function (error) {115 done();116 });117 });118 });119 it('should preserve the HTTP verb on redirect', function (done) {120 server = http.createServer(function (req, res) {121 if (req.method.toLowerCase() !== "head") {122 res.statusCode = 400;123 res.end();124 return;125 }126 var parsed = url.parse(req.url);127 if (parsed.pathname === '/one') {128 res.setHeader('Location', '/two');129 res.statusCode = 302;130 res.end();131 } else {132 res.end();133 }134 }).listen(4444, function () {135 axios.head('http://localhost:4444/one').then(function (res) {136 assert.equal(res.status, 200);137 done();138 }).catch(function (err) {139 done(err);140 });141 });142 });143 it('should support transparent gunzip', function (done) {144 var data = {145 firstName: 'Fred',146 lastName: 'Flintstone',147 emailAddr: 'fred@example.com'148 };149 zlib.gzip(JSON.stringify(data), function (err, zipped) {150 server = http.createServer(function (req, res) {151 res.setHeader('Content-Type', 'application/json;charset=utf-8');152 res.setHeader('Content-Encoding', 'gzip');153 res.end(zipped);154 }).listen(4444, function () {155 axios.get('http://localhost:4444/').then(function (res) {156 assert.deepEqual(res.data, data);157 done();158 });159 });160 });161 });162 it('should support gunzip error handling', function (done) {163 server = http.createServer(function (req, res) {164 res.setHeader('Content-Type', 'application/json;charset=utf-8');165 res.setHeader('Content-Encoding', 'gzip');166 res.end('invalid response');167 }).listen(4444, function () {168 axios.get('http://localhost:4444/').catch(function (error) {169 done();170 });171 });172 });173 it('should support UTF8', function (done) {174 var str = Array(100000).join('ж');175 server = http.createServer(function (req, res) {176 res.setHeader('Content-Type', 'text/html; charset=UTF-8');177 res.end(str);178 }).listen(4444, function () {179 axios.get('http://localhost:4444/').then(function (res) {180 assert.equal(res.data, str);181 done();182 });183 });184 });185 it('should support basic auth', function (done) {186 server = http.createServer(function (req, res) {187 res.end(req.headers.authorization);188 }).listen(4444, function () {189 var user = 'foo';190 var headers = { Authorization: 'Bearer 1234' };191 axios.get('http://' + user + '@localhost:4444/', { headers: headers }).then(function (res) {192 var base64 = Buffer.from(user + ':', 'utf8').toString('base64');193 assert.equal(res.data, 'Basic ' + base64);194 done();195 });196 });197 });198 it('should support basic auth with a header', function (done) {199 server = http.createServer(function (req, res) {200 res.end(req.headers.authorization);201 }).listen(4444, function () {202 var auth = { username: 'foo', password: 'bar' };203 var headers = { Authorization: 'Bearer 1234' };204 axios.get('http://localhost:4444/', { auth: auth, headers: headers }).then(function (res) {205 var base64 = Buffer.from('foo:bar', 'utf8').toString('base64');206 assert.equal(res.data, 'Basic ' + base64);207 done();208 });209 });210 });211 it('should support max content length', function (done) {212 var str = Array(100000).join('ж');213 server = http.createServer(function (req, res) {214 res.setHeader('Content-Type', 'text/html; charset=UTF-8');215 res.end(str);216 }).listen(4444, function () {217 var success = false, failure = false, error;218 axios.get('http://localhost:4444/', {219 maxContentLength: 2000220 }).then(function (res) {221 success = true;222 }).catch(function (err) {223 error = err;224 failure = true;225 });226 setTimeout(function () {227 assert.equal(success, false, 'request should not succeed');228 assert.equal(failure, true, 'request should fail');229 assert.equal(error.message, 'maxContentLength size of 2000 exceeded');230 done();231 }, 100);232 });233 });234 it.skip('should support sockets', function (done) {235 server = net.createServer(function (socket) {236 socket.on('data', function () {237 socket.end('HTTP/1.1 200 OK\r\n\r\n');238 });239 }).listen('./test.sock', function () {240 axios({241 socketPath: './test.sock',242 url: '/'243 })244 .then(function (resp) {245 assert.equal(resp.status, 200);246 assert.equal(resp.statusText, 'OK');247 done();248 })249 .catch(function (error) {250 assert.ifError(error);251 done();252 });253 });254 });255 it('should support streams', function (done) {256 server = http.createServer(function (req, res) {257 req.pipe(res);258 }).listen(4444, function () {259 axios.post('http://localhost:4444/',260 fs.createReadStream(__filename), {261 responseType: 'stream'262 }).then(function (res) {263 var stream = res.data;264 var string = '';265 stream.on('data', function (chunk) {266 string += chunk.toString('utf8');267 });268 stream.on('end', function () {269 assert.equal(string, fs.readFileSync(__filename, 'utf8'));270 done();271 });272 });273 });274 });275 it('should pass errors for a failed stream', function (done) {276 server = http.createServer(function (req, res) {277 req.pipe(res);278 }).listen(4444, function () {279 axios.post('http://localhost:4444/',280 fs.createReadStream('/does/not/exist')281 ).then(function (res) {282 assert.fail();283 }).catch(function (err) {284 assert.equal(err.message, 'ENOENT: no such file or directory, open \'/does/not/exist\'');285 done();286 });287 });288 });289 it('should support buffers', function (done) {290 var buf = Buffer.alloc(1024, 'x'); // Unsafe buffer < Buffer.poolSize (8192 bytes)291 server = http.createServer(function (req, res) {292 assert.equal(req.headers['content-length'], buf.length.toString());293 req.pipe(res);294 }).listen(4444, function () {295 axios.post('http://localhost:4444/',296 buf, {297 responseType: 'stream'298 }).then(function (res) {299 var stream = res.data;300 var string = '';301 stream.on('data', function (chunk) {302 string += chunk.toString('utf8');303 });304 stream.on('end', function () {305 assert.equal(string, buf.toString());306 done();307 });308 });309 });310 });311 it('should support HTTP proxies', function (done) {312 server = http.createServer(function (req, res) {313 res.setHeader('Content-Type', 'text/html; charset=UTF-8');314 res.end('12345');315 }).listen(4444, function () {316 proxy = http.createServer(function (request, response) {317 var parsed = url.parse(request.url);318 var opts = {319 host: parsed.hostname,320 port: parsed.port,321 path: parsed.path322 };323 http.get(opts, function (res) {324 var body = '';325 res.on('data', function (data) {326 body += data;327 });328 res.on('end', function () {329 response.setHeader('Content-Type', 'text/html; charset=UTF-8');330 response.end(body + '6789');331 });332 });333 }).listen(4000, function () {334 axios.get('http://localhost:4444/', {335 proxy: {336 host: 'localhost',337 port: 4000338 }339 }).then(function (res) {340 assert.equal(res.data, '123456789', 'should pass through proxy');341 done();342 });343 });344 });345 });346 it('should not pass through disabled proxy', function (done) {347 // set the env variable348 process.env.http_proxy = 'http://does-not-exists.example.com:4242/';349 server = http.createServer(function (req, res) {350 res.setHeader('Content-Type', 'text/html; charset=UTF-8');351 res.end('123456789');352 }).listen(4444, function () {353 axios.get('http://localhost:4444/', {354 proxy: false355 }).then(function (res) {356 assert.equal(res.data, '123456789', 'should not pass through proxy');357 done();358 });359 });360 });361 it('should support proxy set via env var', function (done) {362 server = http.createServer(function (req, res) {363 res.setHeader('Content-Type', 'text/html; charset=UTF-8');364 res.end('4567');365 }).listen(4444, function () {366 proxy = http.createServer(function (request, response) {367 var parsed = url.parse(request.url);368 var opts = {369 host: parsed.hostname,370 port: parsed.port,371 path: parsed.path372 };373 http.get(opts, function (res) {374 var body = '';375 res.on('data', function (data) {376 body += data;377 });378 res.on('end', function () {379 response.setHeader('Content-Type', 'text/html; charset=UTF-8');380 response.end(body + '1234');381 });382 });383 }).listen(4000, function () {384 // set the env variable385 process.env.http_proxy = 'http://localhost:4000/';386 axios.get('http://localhost:4444/').then(function (res) {387 assert.equal(res.data, '45671234', 'should use proxy set by process.env.http_proxy');388 done();389 });390 });391 });392 });393 it('should not use proxy for domains in no_proxy', function (done) {394 server = http.createServer(function (req, res) {395 res.setHeader('Content-Type', 'text/html; charset=UTF-8');396 res.end('4567');397 }).listen(4444, function () {398 proxy = http.createServer(function (request, response) {399 var parsed = url.parse(request.url);400 var opts = {401 host: parsed.hostname,402 port: parsed.port,403 path: parsed.path404 };405 http.get(opts, function (res) {406 var body = '';407 res.on('data', function (data) {408 body += data;409 });410 res.on('end', function () {411 response.setHeader('Content-Type', 'text/html; charset=UTF-8');412 response.end(body + '1234');413 });414 });415 }).listen(4000, function () {416 // set the env variable417 process.env.http_proxy = 'http://localhost:4000/';418 process.env.no_proxy = 'foo.com, localhost,bar.net , , quix.co';419 axios.get('http://localhost:4444/').then(function (res) {420 assert.equal(res.data, '4567', 'should not use proxy for domains in no_proxy');421 done();422 });423 });424 });425 });426 it('should use proxy for domains not in no_proxy', function (done) {427 server = http.createServer(function (req, res) {428 res.setHeader('Content-Type', 'text/html; charset=UTF-8');429 res.end('4567');430 }).listen(4444, function () {431 proxy = http.createServer(function (request, response) {432 var parsed = url.parse(request.url);433 var opts = {434 host: parsed.hostname,435 port: parsed.port,436 path: parsed.path437 };438 http.get(opts, function (res) {439 var body = '';440 res.on('data', function (data) {441 body += data;442 });443 res.on('end', function () {444 response.setHeader('Content-Type', 'text/html; charset=UTF-8');445 response.end(body + '1234');446 });447 });448 }).listen(4000, function () {449 // set the env variable450 process.env.http_proxy = 'http://localhost:4000/';451 process.env.no_proxy = 'foo.com, ,bar.net , quix.co';452 axios.get('http://localhost:4444/').then(function (res) {453 assert.equal(res.data, '45671234', 'should use proxy for domains not in no_proxy');454 done();455 });456 });457 });458 });459 it('should support HTTP proxy auth', function (done) {460 server = http.createServer(function (req, res) {461 res.end();462 }).listen(4444, function () {463 proxy = http.createServer(function (request, response) {464 var parsed = url.parse(request.url);465 var opts = {466 host: parsed.hostname,467 port: parsed.port,468 path: parsed.path469 };470 var proxyAuth = request.headers['proxy-authorization'];471 http.get(opts, function (res) {472 var body = '';473 res.on('data', function (data) {474 body += data;475 });476 res.on('end', function () {477 response.setHeader('Content-Type', 'text/html; charset=UTF-8');478 response.end(proxyAuth);479 });480 });481 }).listen(4000, function () {482 axios.get('http://localhost:4444/', {483 proxy: {484 host: 'localhost',485 port: 4000,486 auth: {487 username: 'user',488 password: 'pass'489 }490 }491 }).then(function (res) {492 var base64 = Buffer.from('user:pass', 'utf8').toString('base64');493 assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy');494 done();495 });496 });497 });498 });499 it('should support proxy auth from env', function (done) {500 server = http.createServer(function (req, res) {501 res.end();502 }).listen(4444, function () {503 proxy = http.createServer(function (request, response) {504 var parsed = url.parse(request.url);505 var opts = {506 host: parsed.hostname,507 port: parsed.port,508 path: parsed.path509 };510 var proxyAuth = request.headers['proxy-authorization'];511 http.get(opts, function (res) {512 var body = '';513 res.on('data', function (data) {514 body += data;515 });516 res.on('end', function () {517 response.setHeader('Content-Type', 'text/html; charset=UTF-8');518 response.end(proxyAuth);519 });520 });521 }).listen(4000, function () {522 process.env.http_proxy = 'http://user:pass@localhost:4000/';523 axios.get('http://localhost:4444/').then(function (res) {524 var base64 = Buffer.from('user:pass', 'utf8').toString('base64');525 assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy set by process.env.http_proxy');526 done();527 });528 });529 });530 });531 it('should support proxy auth with header', function (done) {532 server = http.createServer(function (req, res) {533 res.end();534 }).listen(4444, function () {535 proxy = http.createServer(function (request, response) {536 var parsed = url.parse(request.url);537 var opts = {538 host: parsed.hostname,539 port: parsed.port,540 path: parsed.path541 };542 var proxyAuth = request.headers['proxy-authorization'];543 http.get(opts, function (res) {544 var body = '';545 res.on('data', function (data) {546 body += data;547 });548 res.on('end', function () {549 response.setHeader('Content-Type', 'text/html; charset=UTF-8');550 response.end(proxyAuth);551 });552 });553 }).listen(4000, function () {554 axios.get('http://localhost:4444/', {555 proxy: {556 host: 'localhost',557 port: 4000,558 auth: {559 username: 'user',560 password: 'pass'561 }562 },563 headers: {564 'Proxy-Authorization': 'Basic abc123'565 }566 }).then(function (res) {567 var base64 = Buffer.from('user:pass', 'utf8').toString('base64');568 assert.equal(res.data, 'Basic ' + base64, 'should authenticate to the proxy');569 done();570 });571 });572 });573 });574 it('should support cancel', function (done) {575 var source = axios.CancelToken.source();576 server = http.createServer(function (req, res) {577 // call cancel() when the request has been sent, but a response has not been received578 source.cancel('Operation has been canceled.');579 }).listen(4444, function () {580 axios.get('http://localhost:4444/', {581 cancelToken: source.token582 }).catch(function (thrown) {583 assert.ok(thrown instanceof axios.Cancel, 'Promise must be rejected with a Cancel obejct');584 assert.equal(thrown.message, 'Operation has been canceled.');585 done();586 });587 });588 });589 it('should combine baseURL and url', function (done) {590 server = http.createServer(function (req, res) {591 res.end();592 }).listen(4444, function () {593 axios.get('/foo', {594 baseURL: 'http://localhost:4444/',595 }).then(function (res) {596 assert.equal(res.config.baseURL, 'http://localhost:4444/');597 assert.equal(res.config.url, '/foo');598 done();599 });600 });601 });...
httpSpec.js
Source:httpSpec.js
...14 headers: test_headers15 };16 });17 it('createServer should return a Server', function(done) {18 expect(http.createServer() instanceof http.Server).toBeTruthy();19 helper.testComplete(true);20 });21 it('should fire a listening event', function() {22 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);23 var server = http.createServer();24 server.listen(test_options.port, function() {25 server.close(function() { helper.testComplete(true); });26 });27 });28 it('should fire a request event', function() {29 waitsFor(helper.testComplete, "waiting for request event to fire", 5);30 var server = http.createServer(function() {31 // getting here means it worked32 server.close(function() { helper.testComplete(true); });33 });34 // simulate a request event35 server.emit('request');36 });37 it('should be able to request with no callback', function() {38 waitsFor(helper.testComplete, "waiting for http request", 5);39 var server = http.createServer(function(request, response) {40 request.on('data', function(data) {41 expect('crispy bacon').toBe(data.toString());42 response.end();43 server.on('close', function() { helper.testComplete(true); });44 server.close();45 });46 });47 test_options.method = 'POST';48 server.listen(test_options.port, function() {49 var request = http.request(test_options);50 request.end('crispy bacon');51 });52 });53 it('should response.write', function() {54 waitsFor(helper.testComplete, "waiting for http response.write", 5);55 var server = http.createServer(function(request, response) {56 expect(response.headersSent).toBe(false);57 response.write('crunchy bacon');58 expect(response.headersSent).toBe(true);59 response.end();60 });61 server.listen(test_options.port, function() {62 var request = http.request(test_options, function(response) {63 response.on('data', function(message) {64 expect('crunchy bacon').toBe(message);65 server.close(function() { helper.testComplete(true); });66 });67 });68 request.end();69 });70 });71 it('should response.write end', function() {72 waitsFor(helper.testComplete, "waiting for http response.end", 5);73 var server = http.createServer(function(request, response) {74 expect(response.headersSent).toBe(false);75 response.end('crunchy bacon');76 expect(response.headersSent).toBe(true);77 });78 server.listen(test_options.port, function() {79 var request = http.request(test_options, function(response) {80 response.on('data', function(message) {81 expect('crunchy bacon').toBe(message);82 server.close(function() { helper.testComplete(true); });83 });84 });85 request.end();86 });87 });88});89describe('http request and response', function() {90 beforeEach(function() {91 helper.testComplete(false);92 test_headers = {93 'x-custom-header': 'A custom header'94 };95 test_options = {96 port: 9999,97 path: '/some/path?with=a+query+string',98 headers: test_headers99 };100 });101 it('should have message headers', function() {102 waitsFor(helper.testComplete, "waiting for message headers test", 5);103 var server = http.createServer(function(request, response) {104 expect(test_headers['x-custom-header']).toBe(request.headers['x-custom-header']);105 var body = 'crunchy bacon';106 response.writeHead(201, { 'Content-Length': body.length });107 expect(body.length.toString()).toBe(response.getHeader('Content-Length'));108 expect(response.headersSent).toEqual(true);109 response.setHeader('Content-Type', 'text/plain');110 expect('text/plain').toBe(response.getHeader('Content-Type'));111 response.removeHeader('x-something-else');112 expect(response.getHeader('x-something-else')).toBe(undefined);113 response.setHeader("Set-Cookie", ["type=ninja", "language=javascript"]);114 response.end();115 });116 server.listen(test_options.port, function() {117 var request = http.request(test_options, function(response) {118 expect(response.statusCode).toBe(201);119 expect(response.headers['Content-Type']).toBe('text/plain');120 expect(response.headers.Date).not.toBeNull();121 expect(response.headers.Date).not.toBe(undefined);122 expect(response.headers['Set-Cookie']).toBe('type=ninja,language=javascript');123 server.close(function() { helper.testComplete(true); });124 });125 request.end();126 });127 });128 it('should be able to add trailers', function() {129 waitsFor(helper.testComplete, "waiting for http trailers test", 5);130 var server = http.createServer(function(request, response) {131 var body = 'crunchy bacon';132 response.writeHead(200, {'Content-Type': 'text/plain',133 'Trailers': 'X-Custom-Trailer'});134 response.write(body);135 response.addTrailers({'X-Custom-Trailer': 'a trailer'});136 response.end();137 });138 server.listen(test_options.port, function() {139 var request = http.request(test_options, function(response) {140 expect(response.headers['Content-Type']).toBe('text/plain');141 expect(response.headers.Trailers).toBe('X-Custom-Trailer');142 response.on('end', function() {143 expect(response.trailers['X-Custom-Trailer']).toBe('a trailer');144 server.close(function() { helper.testComplete(true); });145 });146 });147 request.end();148 });149 });150 it('should have message encoding', function() {151 var expected = 'This is a unicode text: سÙاÙ
';152 var result = '';153 waitsFor(helper.testComplete, "waiting for http message encoding test", 5);154 var server = http.createServer(function(req, res) {155 req.setEncoding('utf8');156 req.on('data', function(chunk) {157 result += chunk;158 });159 res.writeHead(200);160 res.end('hello world\n');161 });162 server.listen(test_options.port, function() {163 test_options.method = 'POST';164 test_options.path = '/unicode/test';165 var request = http.request(test_options, function(res) {166 res.resume();167 res.on('end', function() {168 server.close(function() {169 expect(expected).toBe(result);170 helper.testComplete(true);171 });172 });173 }).end(expected);174 });175 });176 it('should pause and resume', function() {177 var expectedServer = 'Request Body from Client';178 var resultServer = '';179 var expectedClient = 'Response Body from Server';180 var resultClient = '';181 waitsFor(helper.testComplete, "waiting for http pause and resume test", 5);182 var server = http.createServer(function(req, res) {183 req.pause();184 setTimeout(function() {185 req.resume();186 req.setEncoding('utf8');187 req.on('data', function(chunk) {188 resultServer += chunk;189 });190 req.on('end', function() {191 res.writeHead(200);192 res.end(expectedClient);193 });194 }, 100);195 });196 server.listen(test_options.port, function() {197 test_options.method = 'POST';198 var req = http.request(test_options, function(res) {199 res.pause();200 setTimeout(function() {201 res.resume();202 res.on('data', function(chunk) {203 resultClient += chunk;204 });205 res.on('end', function() {206 expect(expectedServer).toBe(resultServer);207 expect(expectedClient).toBe(resultClient);208 server.close(function() { helper.testComplete(true); });209 });210 }, 100);211 });212 req.end(expectedServer);213 });214 });215 it('should have a status code', function() {216 waitsFor(helper.testComplete, "waiting for http status code test", 5);217 var server = http.createServer(function(request, response) {218 response.end("OK");219 });220 server.listen(test_options.port, function() {221 var request = http.request(test_options, function(response) {222 expect(response.statusCode.toString()).toBe("200");223 server.close(function() { helper.testComplete(true); });224 });225 request.end();226 });227 });228 it('should return a request.url', function() {229 waitsFor(helper.testComplete, "waiting for http request.url test", 5);230 var server = http.createServer(function(request, response) {231 expect(request.url).toBe('/some/path?with=a+query+string');232 response.end();233 });234 server.listen(test_options.port, function() {235 var request = http.request(test_options, function(response) {236 server.close(function() { helper.testComplete(true); });237 });238 request.end();239 });240 });241 it('should have the HTTP version in the request', function() {242 waitsFor(helper.testComplete, "waiting for http version test", 5);243 var server = http.createServer(function(request, response) {244 expect(request.httpVersion).toBe('1.1');245 expect(request.httpMajorVersion).toEqual(1);246 expect(request.httpMinorVersion).toEqual(1);247 response.end();248 });249 server.listen(test_options.port, function() {250 http.request(test_options, function(){251 server.close(function() { helper.testComplete(true); });252 }).end();253 });254 });255 it('should request.write', function() {256 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);257 var server = http.createServer(function(request, response) {258 request.on('data', function(data) {259 expect(data.toString()).toBe("cheese muffins");260 response.end();261 server.close(function() { helper.testComplete(true); });262 });263 });264 server.listen(test_options.port, function() {265 var request = http.request(test_options, function() {266 });267 request.write("cheese muffins");268 request.end('bye'); // TODO: This should not be necessary269 // TODO: But if we don't write something270 // TODO: Vert.x inexplicably throws NPE271 });272 });273 it('should have a request.method', function() {274 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);275 var server = http.createServer(function(request, response) {276 expect(request.method).toBe('HEAD');277 response.end();278 });279 server.listen(test_options.port, function() {280 test_options.method = 'HEAD';281 http.request(test_options, function() {282 server.close(function() { helper.testComplete(true); });283 }).end();284 });285 });286 it('should have a GET method', function() {287 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);288 var server = http.createServer(function(request, response) {289 expect(request.method).toBe('GET');290 response.end();291 server.close(function() { helper.testComplete(true); });292 });293 server.listen(test_options.port, function() {294 test_options.method = null;295 http.get(test_options);296 });297 });298 it('should have a request setTimeout', function() {299 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);300 var server = http.createServer(function(request, response) {301 // do nothing - we want the connection to timeout302 });303 server.listen(test_options.port, function() {304 var request = http.request(test_options);305 request.setTimeout(10, function() {306 server.close(function() { helper.testComplete(true); });307 });308 });309 });310 it('should have a request event called', function() {311 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);312 var called = false;313 var server = http.createServer(function(request, response) {314 // node.js request listener315 // called when a 'request' event is emitted316 called = true;317 expect(request instanceof http.IncomingMessage);318 expect(response instanceof http.ServerResponse);319 response.statusCode = 200;320 response.end();321 });322 server.listen(test_options.port, function() {323 var request = http.request(test_options, function(response) {324 expect(response).not.toBeNull();325 expect(called).toEqual(true);326 server.close(function() { helper.testComplete(true); });327 });328 request.end();329 });330 });331 it('should have a close', function() {332 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);333 http.createServer().close(function() {334 helper.testComplete(true);335 });336 });337 it('should have a default timeout', function() {338 var server = http.createServer();339 expect(server.timeout).toEqual(120000);340 helper.testComplete(true);341 });342 it('should setTimeout', function() {343 var timedOut = false;344 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);345 http.createServer().setTimeout(10, function(sock) {346 timedOut = true;347 sock.close();348 });349 timer.setTimer(100, function() {350 expect(timedOut).toEqual(true);351 helper.testComplete(true);352 });353 });354 it('should have a close event', function() {355 var closed = false;356 var server = http.createServer();357 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);358 server.on('close', function() {359 closed = true;360 });361 server.close(function() {362 expect(closed).toEqual(true);363 helper.testComplete(true);364 });365 });366 it('should have a continue event', function() {367 var server = http.createServer();368 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);369 server.on('checkContinue', function(request, response) {370 response.writeContinue();371 response.end();372 });373 server.listen(test_options.port, function() {374 var headers = {375 'Expect': '100-Continue'376 };377 test_options.headers = headers;378 var request = http.request(test_options, function(response) {});379 request.on('continue', function() {380 server.close();381 helper.testComplete(true);382 });383 request.end();384 });385 });386 it('should have a connect fired event', function() {387 var server = http.createServer();388 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);389 server.on('request', function(request, response) {390 this.fail(Error("CONNECT requests should not issue 'request' events"));391 helper.testComplete(true);392 }.bind(this));393 server.on('connect', function(request, clientSock, head) {394 expect(clientSock !== null);395 expect(clientSock !== undefined);396 expect(head !== null);397 expect(head !== undefined);398 clientSock.write('HTTP/1.1 200 Connection Established\r\n' +399 'Proxy-agent: Nodyn-Proxy\r\n' +400 '\r\n');401 clientSock.on('data', function(buffer) {402 expect(buffer.toString()).toBe('Bonjour');403 clientSock.write('Au revoir');404 clientSock.end();405 });406 });407 server.listen(test_options.port, function() {408 test_options.method = 'CONNECT';409 var clientRequest = http.request(test_options, function() {410 fail(Error("CONNECT requests should not emit 'response' events"));411 }.bind(this));412 clientRequest.on('connect', function(res, socket, head) {413 expect(socket !== null);414 expect(socket !== undefined);415 expect(head !== null);416 expect(head !== undefined);417 socket.write('Bonjour');418 socket.on('data', function(buffer) {419 expect(buffer.toString()).toBe('Au revoir');420 server.close();421 helper.testComplete(true);422 });423 });424 clientRequest.end();425 });426 });427 it('should do a connection upgrade', function() {428 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);429 var server = http.createServer(function(req, resp) {430 resp.writeHead(200, {'Content-Type': 'text/plain'});431 resp.end('later!');432 });433 server.on('upgrade', function(req, socket, head) {434 expect(req.headers.Connection).toBe('Upgrade');435 socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +436 'Upgrade: WebSocket\r\n' +437 'Connection: Upgrade\r\n' +438 '\r\n');439 socket.write('fajitas');440 });441 server.listen(test_options.port, function() {442 test_options.headers = {443 'Connection': 'Upgrade',444 'Upgrade': 'websocket'445 };446 var request = http.request(test_options);447 request.end();448 request.on('upgrade', function(resp, socket, head) {449 expect(resp.headers.Connection).toBe('Upgrade');450 // TODO: pending https://github.com/vert-x/vert.x/issues/610451 //socket.on('data', function(buffer) {452 // vassert.assertEquals('object', typeof buffer);453 // vassert.assertEquals("fajitas", buffer.toString());454 // socket.destroy();455 server.close();456 helper.testComplete(true);457 //});458 });459 });460 });461 it('should have a Server Max Headers Count Default value', function() {462 var server = http.createServer();463 expect(server.maxHeadersCount).toEqual(1000);464 server.maxHeadersCount = 50000;465 expect(server.maxHeadersCount).toEqual(50000);466 helper.testComplete();467 });468 it('should send Response Headers', function() {469 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);470 var server = http.createServer(function(request, response) {471 expect(response.headersSent).toEqual(false);472 response.writeHead(201);473 expect(response.headersSent).toEqual(true);474 response.end();475 });476 server.listen(test_options.port, function() {477 var request = http.request(test_options, function(response) {478 server.close();479 helper.testComplete(true);480 });481 request.end();482 });483 });484 it('should return a ClientRequest on a Request', function() {485 waitsFor(helper.testComplete, "waiting for .listen(handler) to fire", 5);486 var server = http.createServer(function(request, response) {487 response.writeHead(200);488 response.end();489 });490 server.listen(test_options.port, function() {491 var request = http.request(test_options, function(response) {492 server.close();493 helper.testComplete(true);494 });495 expect(request instanceof http.ClientRequest);496 request.end();497 });498 });...
manoow.js
Source:manoow.js
1var http = require('http');2http.createServer(function (req, res) {3 res.writeHead(200, {'Content-Type': 'text/plain'});4 res.end('Hello World!');5}).listen(8080);var http = require('http');6http.createServer(function (req, res) {7 res.writeHead(200, {'Content-Type': 'text/plain'});8 res.end('Hello World!');9}).listen(8080);var http = require('http');10http.createServer(function (req, res) {11 res.writeHead(200, {'Content-Type': 'text/plain'});12 res.end('Hello World!');13}).listen(8080);var http = require('http');14http.createServer(function (req, res) {15 res.writeHead(200, {'Content-Type': 'text/plain'});16 res.end('Hello World!');17}).listen(8080);var http = require('http');18http.createServer(function (req, res) {19 res.writeHead(200, {'Content-Type': 'text/plain'});20 res.end('Hello World!');21}).listen(8080);var http = require('http');22http.createServer(function (req, res) {23 res.writeHead(200, {'Content-Type': 'text/plain'});24 res.end('Hello World!');25}).listen(8080);var http = require('http');26http.createServer(function (req, res) {27 res.writeHead(200, {'Content-Type': 'text/plain'});28 res.end('Hello World!');29}).listen(8080);var http = require('http');30http.createServer(function (req, res) {31 res.writeHead(200, {'Content-Type': 'text/plain'});32 res.end('Hello World!');33}).listen(8080);var http = require('http');34http.createServer(function (req, res) {35 res.writeHead(200, {'Content-Type': 'text/plain'});36 res.end('Hello World!');37}).listen(8080);var http = require('http');38http.createServer(function (req, res) {39 res.writeHead(200, {'Content-Type': 'text/plain'});40 res.end('Hello World!');41}).listen(8080);var http = require('http');42http.createServer(function (req, res) {43 res.writeHead(200, {'Content-Type': 'text/plain'});44 res.end('Hello World!');45}).listen(8080);var http = require('http');46http.createServer(function (req, res) {47 res.writeHead(200, {'Content-Type': 'text/plain'});48 res.end('Hello World!');49}).listen(8080);var http = require('http');50http.createServer(function (req, res) {51 res.writeHead(200, {'Content-Type': 'text/plain'});52 res.end('Hello World!');53}).listen(8080);var http = require('http');54http.createServer(function (req, res) {55 res.writeHead(200, {'Content-Type': 'text/plain'});56 res.end('Hello World!');57}).listen(8080);var http = require('http');58http.createServer(function (req, res) {59 res.writeHead(200, {'Content-Type': 'text/plain'});60 res.end('Hello World!');61}).listen(8080);var http = require('http');62http.createServer(function (req, res) {63 res.writeHead(200, {'Content-Type': 'text/plain'});64 res.end('Hello World!');65}).listen(8080);var http = require('http');66http.createServer(function (req, res) {67 res.writeHead(200, {'Content-Type': 'text/plain'});68 res.end('Hello World!');69}).listen(8080);var http = require('http');70http.createServer(function (req, res) {71 res.writeHead(200, {'Content-Type': 'text/plain'});72 res.end('Hello World!');73}).listen(8080);var http = require('http');74http.createServer(function (req, res) {75 res.writeHead(200, {'Content-Type': 'text/plain'});76 res.end('Hello World!');77}).listen(8080);var http = require('http');78http.createServer(function (req, res) {79 res.writeHead(200, {'Content-Type': 'text/plain'});80 res.end('Hello World!');81}).listen(8080);var http = require('http');82http.createServer(function (req, res) {83 res.writeHead(200, {'Content-Type': 'text/plain'});84 res.end('Hello World!');85}).listen(8080);var http = require('http');86http.createServer(function (req, res) {87 res.writeHead(200, {'Content-Type': 'text/plain'});88 res.end('Hello World!');89}).listen(8080);var http = require('http');90http.createServer(function (req, res) {91 res.writeHead(200, {'Content-Type': 'text/plain'});92 res.end('Hello World!');93}).listen(8080);var http = require('http');94http.createServer(function (req, res) {95 res.writeHead(200, {'Content-Type': 'text/plain'});96 res.end('Hello World!');97}).listen(8080);var http = require('http');98http.createServer(function (req, res) {99 res.writeHead(200, {'Content-Type': 'text/plain'});100 res.end('Hello World!');101}).listen(8080);var http = require('http');102http.createServer(function (req, res) {103 res.writeHead(200, {'Content-Type': 'text/plain'});104 res.end('Hello World!');105}).listen(8080);var http = require('http');106http.createServer(function (req, res) {107 res.writeHead(200, {'Content-Type': 'text/plain'});108 res.end('Hello World!');109}).listen(8080);var http = require('http');110http.createServer(function (req, res) {111 res.writeHead(200, {'Content-Type': 'text/plain'});112 res.end('Hello World!');113}).listen(8080);var http = require('http');114http.createServer(function (req, res) {115 res.writeHead(200, {'Content-Type': 'text/plain'});116 res.end('Hello World!');117}).listen(8080);var http = require('http');118http.createServer(function (req, res) {119 res.writeHead(200, {'Content-Type': 'text/plain'});120 res.end('Hello World!');121}).listen(8080);var http = require('http');122http.createServer(function (req, res) {123 res.writeHead(200, {'Content-Type': 'text/plain'});124 res.end('Hello World!');125}).listen(8080);var http = require('http');126http.createServer(function (req, res) {127 res.writeHead(200, {'Content-Type': 'text/plain'});128 res.end('Hello World!');129}).listen(8080);var http = require('http');130http.createServer(function (req, res) {131 res.writeHead(200, {'Content-Type': 'text/plain'});132 res.end('Hello World!');133}).listen(8080);var http = require('http');134http.createServer(function (req, res) {135 res.writeHead(200, {'Content-Type': 'text/plain'});136 res.end('Hello World!');137}).listen(8080);var http = require('http');138http.createServer(function (req, res) {139 res.writeHead(200, {'Content-Type': 'text/plain'});140 res.end('Hello World!');141}).listen(8080);var http = require('http');142http.createServer(function (req, res) {143 res.writeHead(200, {'Content-Type': 'text/plain'});144 res.end('Hello World!');145}).listen(8080);var http = require('http');146http.createServer(function (req, res) {147 res.writeHead(200, {'Content-Type': 'text/plain'});148 res.end('Hello World!');149}).listen(8080);var http = require('http');150http.createServer(function (req, res) {151 res.writeHead(200, {'Content-Type': 'text/plain'});152 res.end('Hello World!');153}).listen(8080);var http = require('http');154http.createServer(function (req, res) {155 res.writeHead(200, {'Content-Type': 'text/plain'});156 res.end('Hello World!');157}).listen(8080);var http = require('http');158http.createServer(function (req, res) {159 res.writeHead(200, {'Content-Type': 'text/plain'});160 res.end('Hello World!');161}).listen(8080);var http = require('http');162http.createServer(function (req, res) {163 res.writeHead(200, {'Content-Type': 'text/plain'});164 res.end('Hello World!');165}).listen(8080);var http = require('http');166http.createServer(function (req, res) {167 res.writeHead(200, {'Content-Type': 'text/plain'});168 res.end('Hello World!');169}).listen(8080);var http = require('http');170http.createServer(function (req, res) {171 res.writeHead(200, {'Content-Type': 'text/plain'});172 res.end('Hello World!');173}).listen(8080);var http = require('http');174http.createServer(function (req, res) {175 res.writeHead(200, {'Content-Type': 'text/plain'});176 res.end('Hello World!');177}).listen(8080);var http = require('http');178http.createServer(function (req, res) {179 res.writeHead(200, {'Content-Type': 'text/plain'});180 res.end('Hello World!');181}).listen(8080);var http = require('http');182http.createServer(function (req, res) {183 res.writeHead(200, {'Content-Type': 'text/plain'});184 res.end('Hello World!');185}).listen(8080);var http = require('http');186http.createServer(function (req, res) {187 res.writeHead(200, {'Content-Type': 'text/plain'});188 res.end('Hello World!');189}).listen(8080);var http = require('http');190http.createServer(function (req, res) {191 res.writeHead(200, {'Content-Type': 'text/plain'});192 res.end('Hello World!');193}).listen(8080);var http = require('http');194http.createServer(function (req, res) {195 res.writeHead(200, {'Content-Type': 'text/plain'});196 res.end('Hello World!');197}).listen(8080);var http = require('http');198http.createServer(function (req, res) {199 res.writeHead(200, {'Content-Type': 'text/plain'});200 res.end('Hello World!');201}).listen(8080);var http = require('http');202http.createServer(function (req, res) {203 res.writeHead(200, {'Content-Type': 'text/plain'});204 res.end('Hello World!');205}).listen(8080);var http = require('http');206http.createServer(function (req, res) {207 res.writeHead(200, {'Content-Type': 'text/plain'});208 res.end('Hello World!');209}).listen(8080);var http = require('http');210http.createServer(function (req, res) {211 res.writeHead(200, {'Content-Type': 'text/plain'});212 res.end('Hello World!');213}).listen(8080);var http = require('http');214http.createServer(function (req, res) {215 res.writeHead(200, {'Content-Type': 'text/plain'});216 res.end('Hello World!');217}).listen(8080);var http = require('http');218http.createServer(function (req, res) {219 res.writeHead(200, {'Content-Type': 'text/plain'});220 res.end('Hello World!');221}).listen(8080);var http = require('http');222http.createServer(function (req, res) {223 res.writeHead(200, {'Content-Type': 'text/plain'});224 res.end('Hello World!');225}).listen(8080);var http = require('http');226http.createServer(function (req, res) {227 res.writeHead(200, {'Content-Type': 'text/plain'});228 res.end('Hello World!');229}).listen(8080);var http = require('http');230http.createServer(function (req, res) {231 res.writeHead(200, {'Content-Type': 'text/plain'});232 res.end('Hello World!');233}).listen(8080);var http = require('http');234http.createServer(function (req, res) {235 res.writeHead(200, {'Content-Type': 'text/plain'});236 res.end('Hello World!');237}).listen(8080);var http = require('http');238http.createServer(function (req, res) {239 res.writeHead(200, {'Content-Type': 'text/plain'});240 res.end('Hello World!');241}).listen(8080);var http = require('http');242http.createServer(function (req, res) {243 res.writeHead(200, {'Content-Type': 'text/plain'});244 res.end('Hello World!');245}).listen(8080);var http = require('http');246http.createServer(function (req, res) {247 res.writeHead(200, {'Content-Type': 'text/plain'});248 res.end('Hello World!');249}).listen(8080);var http = require('http');250http.createServer(function (req, res) {251 res.writeHead(200, {'Content-Type': 'text/plain'});252 res.end('Hello World!');253}).listen(8080);var http = require('http');254http.createServer(function (req, res) {255 res.writeHead(200, {'Content-Type': 'text/plain'});256 res.end('Hello World!');257}).listen(8080);var http = require('http');258http.createServer(function (req, res) {259 res.writeHead(200, {'Content-Type': 'text/plain'});260 res.end('Hello World!');261}).listen(8080);var http = require('http');262http.createServer(function (req, res) {263 res.writeHead(200, {'Content-Type': 'text/plain'});264 res.end('Hello World!');265}).listen(8080);var http = require('http');266http.createServer(function (req, res) {267 res.writeHead(200, {'Content-Type': 'text/plain'});268 res.end('Hello World!');269}).listen(8080);var http = require('http');270http.createServer(function (req, res) {271 res.writeHead(200, {'Content-Type': 'text/plain'});272 res.end('Hello World!');273}).listen(8080);var http = require('http');274http.createServer(function (req, res) {275 res.writeHead(200, {'Content-Type': 'text/plain'});276 res.end('Hello World!');277}).listen(8080);var http = require('http');278http.createServer(function (req, res) {279 res.writeHead(200, {'Content-Type': 'text/plain'});280 res.end('Hello World!');...
Using AI Code Generation
1var http = require('http');2var sinon = require('sinon');3var assert = require('assert');4var server = http.createServer(function(req, res) {5 res.writeHead(200, {'Content-Type': 'text/plain'});6 res.end('Hello World\n');7});8server.listen(1337, '
Using AI Code Generation
1var http = require('http');2var sinon = require('sinon');3var server = http.createServer(function (req, res) {4 res.writeHead(200, {'Content-Type': 'text/plain'});5 res.end('Hello World');6});7server.listen(3000);8var http = require('http');9var sinon = require('sinon');10var server = http.createServer(function (req, res) {11 res.writeHead(200, {'Content-Type': 'text/plain'});12 res.end('Hello World');13});14server.listen(3000);15var http = require('http');16var sinon = require('sinon');17var server = http.createServer(function (req, res) {18 res.writeHead(200, {'Content-Type': 'text/plain'});19 res.end('Hello World');20});21server.listen(3000);22var http = require('http');23var sinon = require('sinon');24var server = http.createServer(function (req, res) {25 res.writeHead(200, {'Content-Type': 'text/plain'});26 res.end('Hello World');27});28server.listen(3000);29var http = require('http');30var sinon = require('sinon');31var server = http.createServer(function (req, res) {32 res.writeHead(200, {'Content-Type': 'text/plain'});33 res.end('Hello World');34});35server.listen(3000);36var http = require('http');37var sinon = require('sinon');38var server = http.createServer(function (req, res) {39 res.writeHead(200, {'Content-Type': 'text/plain'});40 res.end('Hello World');41});42server.listen(3000);43var http = require('http');44var sinon = require('sinon');45var server = http.createServer(function (req, res) {46 res.writeHead(200, {'Content-Type': '
Using AI Code Generation
1var http = require('http');2var sinon = require('sinon');3var assert = require('assert');4var server = http.createServer(function(req, res) {5 res.writeHead(200);6 res.end('Hello World');7});8server.listen(3000);9var http = require('http');10var sinon = require('sinon');11var assert = require('assert');12var server = http.createServer(function(req, res) {13 res.writeHead(200);14 res.end('Hello World');15});16var spy = sinon.spy(server, 'listen');17server.listen(3000);18assert(spy.calledWith(3000));19var http = require('http');20var sinon = require('sinon');21var assert = require('assert');22var server = http.createServer(function(req, res) {23 res.writeHead(200);24 res.end('Hello World');25});26var stub = sinon.stub(server, 'listen');27server.listen(3000);28assert(stub.calledWith(3000));29var http = require('http');30var sinon = require('sinon');31var assert = require('assert');32var server = http.createServer(function(req, res) {33 res.writeHead(200);34 res.end('Hello World');35});36var stub = sinon.stub(server, 'listen');37server.listen(3000);38assert(stub.calledWith(3000));39var http = require('http');40var sinon = require('sinon');41var assert = require('assert');42var server = http.createServer(function(req, res) {43 res.writeHead(200);44 res.end('Hello World');45});46var stub = sinon.stub(server, 'listen');47server.listen(3000);48assert(stub.calledWith(3000));49var http = require('http');50var sinon = require('sinon');51var assert = require('assert');52var server = http.createServer(function(req, res) {53 res.writeHead(200);54 res.end('Hello World');55});56var stub = sinon.stub(server, 'listen');57server.listen(3000);58assert(stub.calledWith(3000));59var http = require('http');60var sinon = require('sinon
Using AI Code Generation
1var http = require('http');2var sinon = require('sinon');3var server = http.createServer();4var spy = sinon.spy(server, 'listen');5server.listen(8080, 'localhost');6console.log(spy.called);7console.log(spy.callCount);8console.log(spy.args);9console.log(spy.firstCall.args);10console.log(spy.secondCall.args);11console.log(spy.withArgs(8080, 'localhost').called);12var http = require('http');13var sinon = require('sinon');14var spy = sinon.spy();15spy(1, 2, 3);16console.log(spy.called);17console.log(spy.callCount);18console.log(spy.args);19console.log(spy.firstCall.args);20console.log(spy.secondCall.args);21console.log(spy.withArgs(1, 2, 3).called);22var http = require('http');23var sinon = require('sinon');24var spy = sinon.spy();25spy(1, 2, 3);26console.log(spy.called);27console.log(spy.callCount);28console.log(spy.args);29console.log(spy.firstCall.args);30console.log(spy.secondCall.args);31console.log(spy.withArgs(1, 2, 3).called);32var http = require('http');33var sinon = require('sinon');34var spy = sinon.spy();35spy(1, 2, 3);36console.log(spy.called);37console.log(spy.callCount);38console.log(spy.args);39console.log(spy.firstCall.args);40console.log(spy.secondCall.args);41console.log(spy.withArgs(1, 2, 3).called);42var http = require('http');43var sinon = require('sinon');44var spy = sinon.spy();45spy(1, 2, 3);46console.log(spy.called);47console.log(spy.callCount);48console.log(spy.args);49console.log(spy.firstCall.args);50console.log(spy.secondCall.args);51console.log(spy.withArgs(1, 2, 3).called);52var http = require('http');
Using AI Code Generation
1const sinon = require('sinon');2const http = require('http');3const https = require('https');4const request = require('request');5const fs = require('fs');6const assert = require('assert');7describe('Server', function () {8 var server;9 var requestStub;10 var writeStub;11 var endStub;12 var createServerStub;13 var createServerResponseStub;14 var requestGetStub;15 var requestOnStub;16 var requestWriteStub;17 var requestEndStub;18 var requestPipeStub;19 var requestSetEncodingStub;20 var requestOnDataStub;21 var requestOnEndStub;22 var requestOnCloseStub;23 var requestOnErrStub;24 var requestOnResponseStub;25 var requestOnRequestStub;26 var requestOnUpgradeStub;27 var requestOnConnectStub;28 var requestOnCheckContinueStub;29 var requestOnCheckExpectationStub;30 var requestOnContinueStub;31 var requestOnSocketStub;32 var requestOnTimeoutStub;33 var requestOnConnectStub;
Using AI Code Generation
1var http = require('http');2var sinon = require('sinon');3var assert = require('assert');4var server = http.createServer(function(req, res) {5 res.writeHead(200, { 'Content-Type': 'text/plain' });6 res.end('Hello World');7});8describe('http', function() {9 it('should return 200', function() {10 var request = sinon.stub(http, 'request');11 request.yields(null, { statusCode: 200 }, 'Hello World');12 server.listen(3000);13 assert.equal(200, res.statusCode);14 done();15 });16 });17});18Your name to display (optional):19Your name to display (optional):20var http = require('http');21var sinon = require('sinon');22var assert = require('assert');23var server = http.createServer(function(req, res) {24 res.writeHead(200, { 'Content-Type': 'text/plain' });25 res.end('Hello World');26});27describe('http', function() {28 it('should return 200', function(done) {29 var request = sinon.stub(http, 'request');30 request.yields(null, { statusCode: 200 }, 'Hello World');31 server.listen(3000);32 assert.equal(200, res.statusCode);33 done();34 });35 });36});37Your name to display (optional):38var http = require('http');39var sinon = require('sinon');40var assert = require('assert');41var server = http.createServer(function(req, res) {42 res.writeHead(200, { 'Content-Type': 'text/plain' });43 res.end('Hello World');44});45describe('http', function() {46 it('should return 200', function(done) {
Using AI Code Generation
1var http = require('http');2var sinon = require('sinon');3var assert = require('assert');4var request = require('request');5var server = require('../server.js');6describe('server', function() {7 var serverStub;8 beforeEach(function() {9 serverStub = sinon.stub(http, 'createServer').yields({10 }, {11 writeHead: function() {},12 end: function() {}13 });14 });15 afterEach(function() {16 serverStub.restore();17 });18 it('should call http.createServer method', function() {19 assert(serverStub.calledOnce);20 });21 });22});23var http = require('http');24var server = http.createServer(function(request, response) {25 response.writeHead(200);26 response.end("Hello World!");27});28server.listen(8080);29var express = require('express');30var app = express();31var router = express.Router();32var passport = require('passport');33var LocalStrategy = require('passport-local').Strategy;34var bcrypt = require('bcryptjs');35var User = require('../models/user');36router.get('/register', function(req, res){37 res.render('register');38});39router.get('/login', function(req, res){40 res.render('login');41});42router.post('/register', function(req, res){43 var name = req.body.name;44 var email = req.body.email;45 var username = req.body.username;46 var password = req.body.password;47 var password2 = req.body.password2;48 req.checkBody('name', 'Name is required').notEmpty();49 req.checkBody('email', 'Email is required').notEmpty();50 req.checkBody('email', 'Email is not valid').isEmail();51 req.checkBody('username', 'Username is required').notEmpty();52 req.checkBody('password', 'Password
Using AI Code Generation
1var http = require('http');2var sinon = require('sinon');3sinon.stub(http, 'createServer', function (requestListener) {4 return {5 listen: function (port, host, callback) {6 callback();7 }8 };9});10sinon.stub(console, 'log', function (message) {11 return message;12});13var test = require('./test.js');14sinon.restore();15console.log('console.log message: ' + console.log.getCall(0).args[0]);16console.log('http.createServer called: ' + http.createServer.calledOnce);17console.log('http.createServer listen called: ' + http.createServer().listen.calledOnce);18console.log('http.createServer listen port: ' + http.createServer().listen.getCall(0).args[0]);19console.log('http.createServer listen host: ' + http.createServer().listen.getCall(0).args[1]);20console.log('http.createServer listen callback: ' + http.createServer().listen.getCall(0).args[2]);21console.log('http.createServer listen callback called: ' + http.createServer().listen().callback.calledOnce);22console.log('http.createServer listen callback message: ' + http.createServer().listen().callback.getCall(0).args[0]);23console.log('http.createServer listen callback message: ' + http.createServer().listen().callback.getCall(0).args[1]);24console.log('http.createServer listen callback message: ' + http.createServer().listen().callback.getCall(0).args[2]);25console.log('http.createServer listen callback message: ' + http.createServer().listen().callback.getCall(0).args[3]);26console.log('http.createServer listen callback message: ' + http.createServer().listen().callback.getCall(0).args[4]);
Using AI Code Generation
1const sinon = require('sinon');2const http = require('http');3const sandbox = sinon.createSandbox();4const server = http.createServer();5const request = http.request();6const response = http.response();7const req = http.request();8const res = http.response();9sandbox.stub(req, 'request').callsFake(function(){10 return 'request';11});12sandbox.stub(res, 'response').callsFake(function(){13 return 'response';14});15sandbox.stub(server, 'createServer').callsFake(function(){16 return 'createServer';17});18sandbox.stub(request, 'request').callsFake(function(){19 return 'request';20});21sandbox.stub(response, 'response').callsFake(function(){22 return 'response';23});24sandbox.stub(http, 'get').callsFake(function(){25 return 'get';26});27sandbox.stub(http, 'request').callsFake(function(){28 return 'request';29});30sandbox.stub(http, 'response').callsFake(function(){31 return 'response';32});33sandbox.stub(http, 'createServer').callsFake(function(){34 return 'createServer';35});36sandbox.stub(http, 'get').callsFake(function(){37 return 'get';38});39sandbox.stub(http, 'request').callsFake(function(){40 return 'request';41});42sandbox.stub(http, 'response').callsFake(function(){43 return 'response';44});45sandbox.stub(http, 'createServer').callsFake(function(){46 return 'createServer';47});48sandbox.stub(http, 'get').callsFake(function(){49 return 'get';50});51sandbox.stub(http, 'request').callsFake(function(){52 return 'request';53});
Using AI Code Generation
1const sinon = require('sinon');2const http = require('http');3const assert = require('assert');4const server = http.createServer();5const request = require('request');6describe('server', function(){7 beforeEach(function(){8 sinon.spy(http, 'createServer');9 });10 afterEach(function(){11 http.createServer.restore();12 });13 it('should create a server', function(){14 assert(http.createServer.calledOnce);15 });16});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!