How to use shouldIncludeStackWithThisFile method in supertest

Best JavaScript code snippet using supertest

supertest.test.js

Source:supertest.test.js Github

copy

Full Screen

...6import * as bodyParser from 'body-parser'7import cookieParser from 'cookie-parser'8import * as nock from 'nock'9process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';10function shouldIncludeStackWithThisFile(err) {11 expect(err.stack).toMatch(/test\/supertest.test.js:/)12 expect(err.stack.startsWith(err.name + ':')).toBeTruthy()13}14describe('request(url)', function () {15 it('should be supported', function (done) {16 const app = express();17 let s;18 app.get('/', function (req, res) {19 res.send('hello');20 });21 s = app.listen(function () {22 const url = 'http://localhost:' + s.address().port;23 request(url)24 .get('/')25 .expect('hello', done);26 });27 });28 describe('.end(cb)', function () {29 it('should set `this` to the test object when calling cb', function (done) {30 const app = express();31 let s;32 app.get('/', function (req, res) {33 res.send('hello');34 });35 s = app.listen(function () {36 const url = 'http://localhost:' + s.address().port;37 const test = request(url).get('/');38 test.end(function (err, res) {39 expect(this).toEqual(test)40 done();41 });42 });43 });44 });45});46describe('request(app)', function () {47 it('should fire up the app on an ephemeral port', function (done) {48 const app = express();49 app.get('/', function (req, res) {50 res.send('hey');51 });52 request(app)53 .get('/')54 .end(function (err, res) {55 expect(res.status).toEqual(200)56 expect(res.text).toEqual('hey')57 done();58 });59 });60 it('should work with an active server', function (done) {61 const app = express();62 let server;63 app.get('/', function (req, res) {64 res.send('hey');65 });66 server = app.listen(4000, function () {67 request(server)68 .get('/')69 .end(function (err, res) {70 expect(res.status).toEqual(200)71 expect(res.text).toEqual('hey')72 done();73 });74 });75 });76 it('should work with remote server', function (done) {77 const app = express();78 app.get('/', function (req, res) {79 res.send('hey');80 });81 app.listen(4001, function () {82 request('http://localhost:4001')83 .get('/')84 .end(function (err, res) {85 expect(res.status).toEqual(200)86 expect(res.text).toEqual('hey')87 done();88 });89 });90 });91 it('should work with a https server', function (done) {92 const app = express();93 const fixtures = path.join(__dirname, 'fixtures');94 const server = https.createServer({95 key: fs.readFileSync(path.join(fixtures, 'test_key.pem')),96 cert: fs.readFileSync(path.join(fixtures, 'test_cert.pem'))97 }, app);98 app.get('/', function (req, res) {99 res.send('hey');100 });101 request(server)102 .get('/')103 .end(function (err, res) {104 if (err) return done(err);105 expect(res.status).toEqual(200)106 expect(res.text).toEqual('hey')107 done();108 });109 });110 it('should work with .send() etc', function (done) {111 const app = express();112 app.use(bodyParser.json());113 app.post('/', function (req, res) {114 res.send(req.body.name);115 });116 request(app)117 .post('/')118 .send({ name: 'john' })119 .expect('john', done);120 });121 it('should work when unbuffered', function (done) {122 const app = express();123 app.get('/', function (req, res) {124 res.end('Hello');125 });126 request(app)127 .get('/')128 .expect('Hello', done);129 });130 it('should default redirects to 0', function (done) {131 const app = express();132 app.get('/', function (req, res) {133 res.redirect('/login');134 });135 request(app)136 .get('/')137 .expect(302, done);138 });139 it('should handle redirects', function (done) {140 const app = express();141 app.get('/login', function (req, res) {142 res.end('Login');143 });144 app.get('/', function (req, res) {145 res.redirect('/login');146 });147 request(app)148 .get('/')149 .redirects(1)150 .end(function (err, res) {151 expect(res).toBeTruthy()152 expect(res.status).toEqual(200)153 expect(res.text).toEqual('Login')154 done();155 });156 });157 it('should handle socket errors', function (done) {158 const app = express();159 app.get('/', function (req, res) {160 res.destroy();161 });162 request(app)163 .get('/')164 .end(function (err) {165 expect(err).toBeTruthy()166 done();167 });168 });169 describe('.end(fn)', function () {170 it('should close server', function (done) {171 const app = express();172 let test;173 app.get('/', function (req, res) {174 res.send('supertest FTW!');175 });176 test = request(app)177 .get('/')178 .end(function () {179 });180 test._server.on('close', function () {181 done();182 });183 });184 it('should wait for server to close before invoking fn', function (done) {185 const app = express();186 let closed = false;187 let test;188 app.get('/', function (req, res) {189 res.send('supertest FTW!');190 });191 test = request(app)192 .get('/')193 .end(function () {194 expect(closed).toBeTruthy()195 done();196 });197 test._server.on('close', function () {198 closed = true;199 });200 });201 it('should support nested requests', function (done) {202 const app = express();203 const test = request(app);204 app.get('/', function (req, res) {205 res.send('supertest FTW!');206 });207 test208 .get('/')209 .end(function () {210 test211 .get('/')212 .end(function (err, res) {213 expect(err === null).toBeTruthy()214 expect(res.status).toEqual(200)215 expect(res.text).toEqual('supertest FTW!')216 done();217 });218 });219 });220 it('should include the response in the error callback', function (done) {221 const app = express();222 app.get('/', function (req, res) {223 res.send('whatever');224 });225 request(app)226 .get('/')227 .expect(function () {228 throw new Error('Some error');229 })230 .end(function (err, res) {231 expect(err).toBeTruthy()232 expect(res).toBeTruthy()233 // Duck-typing response, just in case.234 expect(res.status).toEqual(200)235 done();236 });237 });238 it('should set `this` to the test object when calling the error callback', function (done) {239 const app = express();240 let test;241 app.get('/', function (req, res) {242 res.send('whatever');243 });244 test = request(app).get('/');245 test.expect(function () {246 throw new Error('Some error');247 }).end(function (err, res) {248 expect(err).toBeTruthy()249 expect(this).toEqual(test)250 done();251 });252 });253 it('should handle an undefined Response', function (done) {254 const app = express();255 let server;256 app.get('/', function (req, res) {257 setTimeout(function () {258 res.end();259 }, 20);260 });261 server = app.listen(function () {262 const url = 'http://localhost:' + server.address().port;263 request(url)264 .get('/')265 .timeout(1)266 .expect(200, function (err) {267 expect(err instanceof Error).toBeTruthy()268 return done();269 });270 });271 });272 it('should handle error returned when server goes down', function (done) {273 const app = express();274 let server;275 app.get('/', function (req, res) {276 res.end();277 });278 server = app.listen(function () {279 const url = 'http://localhost:' + server.address().port;280 server.close();281 request(url)282 .get('/')283 .expect(200, function (err) {284 expect(err.__proto__.name).toEqual('Error')285 return done();286 });287 });288 });289 });290 describe('.expect(status[, fn])', function () {291 it('should assert the response status', function (done) {292 const app = express();293 app.get('/', function (req, res) {294 res.send('hey');295 });296 request(app)297 .get('/')298 .expect(404)299 .end(function (err, res) {300 expect(err.message).toEqual('expected 404 "Not Found", got 200 "OK"')301 shouldIncludeStackWithThisFile(err);302 done();303 });304 });305 });306 describe('.expect(status)', function () {307 it('should handle connection error', function (done) {308 const req = new request.agent('http://localhost:1234');309 req310 .get('/')311 .expect(200)312 .end(function (err, res) {313 expect(err.message).toEqual('connect ECONNREFUSED 127.0.0.1:1234')314 done();315 });316 });317 });318 describe('.expect(status)', function () {319 it('should assert only status', function (done) {320 const app = express();321 app.get('/', function (req, res) {322 res.send('hey');323 });324 request(app)325 .get('/')326 .expect(200)327 .end(done);328 });329 });330 describe('.expect(status, body[, fn])', function () {331 it('should assert the response body and status', function (done) {332 const app = express();333 app.get('/', function (req, res) {334 res.send('foo');335 });336 request(app)337 .get('/')338 .expect(200, 'foo', done);339 });340 describe('when the body argument is an empty string', function () {341 it('should not quietly pass on failure', function (done) {342 const app = express();343 app.get('/', function (req, res) {344 res.send('foo');345 });346 request(app)347 .get('/')348 .expect(200, '')349 .end(function (err, res) {350 expect(err.message).toEqual('expected \'\' response body, got \'foo\'')351 shouldIncludeStackWithThisFile(err);352 done();353 });354 });355 });356 });357 describe('.expect(body[, fn])', function () {358 it('should assert the response body', function (done) {359 const app = express();360 app.set('json spaces', 0);361 app.get('/', function (req, res) {362 res.send({ foo: 'bar' });363 });364 request(app)365 .get('/')366 .expect('hey')367 .end(function (err, res) {368 expect(err.message).toEqual('expected \'hey\' response body, got \'{"foo":"bar"}\'')369 shouldIncludeStackWithThisFile(err);370 done();371 });372 });373 it('should assert the status before the body', function (done) {374 const app = express();375 app.set('json spaces', 0);376 app.get('/', function (req, res) {377 res.status(500).send({ message: 'something went wrong' });378 });379 request(app)380 .get('/')381 .expect(200)382 .expect('hey')383 .end(function (err, res) {384 expect(err.message).toEqual('expected 200 "OK", got 500 "Internal Server Error"')385 shouldIncludeStackWithThisFile(err);386 done();387 });388 });389 it('should assert the response text', function (done) {390 const app = express();391 app.set('json spaces', 0);392 app.get('/', function (req, res) {393 res.send({ foo: 'bar' });394 });395 request(app)396 .get('/')397 .expect('{"foo":"bar"}', done);398 });399 it('should assert the parsed response body', function (done) {400 const app = express();401 app.set('json spaces', 0);402 app.get('/', function (req, res) {403 res.send({ foo: 'bar' });404 });405 request(app)406 .get('/')407 .expect({ foo: 'baz' })408 .end(function (err, res) {409 expect(err.message).toEqual('expected { foo: \'baz\' } response body, got { foo: \'bar\' }')410 shouldIncludeStackWithThisFile(err);411 request(app)412 .get('/')413 .expect({ foo: 'bar' })414 .end(done);415 });416 });417 it('should test response object types', function (done) {418 const app = express();419 app.get('/', function (req, res) {420 res.status(200).json({ stringValue: 'foo', numberValue: 3 });421 });422 request(app)423 .get('/')424 .expect({ stringValue: 'foo', numberValue: 3 }, done);425 });426 it('should deep test response object types', function (done) {427 const app = express();428 app.get('/', function (req, res) {429 res.status(200)430 .json({ stringValue: 'foo', numberValue: 3, nestedObject: { innerString: '5' } });431 });432 request(app)433 .get('/')434 .expect({ stringValue: 'foo', numberValue: 3, nestedObject: { innerString: 5 } })435 .end(function (err, res) {436 expect(err.message.replace(/[^a-zA-Z]/g, '')).toEqual('expected {\n stringValue: \'foo\',\n numberValue: 3,\n nestedObject: { innerString: 5 }\n} response body, got {\n stringValue: \'foo\',\n numberValue: 3,\n nestedObject: { innerString: \'5\' }\n}'.replace(/[^a-zA-Z]/g, ''))437 shouldIncludeStackWithThisFile(err);438 request(app)439 .get('/')440 .expect({ stringValue: 'foo', numberValue: 3, nestedObject: { innerString: '5' } })441 .end(done);442 });443 });444 it('should support regular expressions', function (done) {445 const app = express();446 app.get('/', function (req, res) {447 res.send('foobar');448 });449 request(app)450 .get('/')451 .expect(/^bar/)452 .end(function (err, res) {453 expect(err.message).toEqual('expected body \'foobar\' to match /^bar/')454 shouldIncludeStackWithThisFile(err);455 done();456 });457 });458 it('should assert response body multiple times', function (done) {459 const app = express();460 app.get('/', function (req, res) {461 res.send('hey tj');462 });463 request(app)464 .get('/')465 .expect(/tj/)466 .expect('hey')467 .expect('hey tj')468 .end(function (err, res) {469 expect(err.message).toEqual("expected 'hey' response body, got 'hey tj'")470 shouldIncludeStackWithThisFile(err);471 done();472 });473 });474 it('should assert response body multiple times with no exception', function (done) {475 const app = express();476 app.get('/', function (req, res) {477 res.send('hey tj');478 });479 request(app)480 .get('/')481 .expect(/tj/)482 .expect(/^hey/)483 .expect('hey tj', done);484 });485 });486 describe('.expect(field, value[, fn])', function () {487 it('should assert the header field presence', function (done) {488 const app = express();489 app.get('/', function (req, res) {490 res.send({ foo: 'bar' });491 });492 request(app)493 .get('/')494 .expect('Content-Foo', 'bar')495 .end(function (err, res) {496 expect(err.message).toEqual('expected "Content-Foo" header field')497 shouldIncludeStackWithThisFile(err);498 done();499 });500 });501 it('should assert the header field value', function (done) {502 const app = express();503 app.get('/', function (req, res) {504 res.send({ foo: 'bar' });505 });506 request(app)507 .get('/')508 .expect('Content-Type', 'text/html')509 .end(function (err, res) {510 expect(err.message).toEqual('expected "Content-Type" of "text/html", '511 + 'got "application/json; charset=utf-8"')512 shouldIncludeStackWithThisFile(err);513 done();514 });515 });516 it('should assert multiple fields', function (done) {517 const app = express();518 app.get('/', function (req, res) {519 res.send('hey');520 });521 request(app)522 .get('/')523 .expect('Content-Type', 'text/html; charset=utf-8')524 .expect('Content-Length', '3')525 .end(done);526 });527 it('should support regular expressions', function (done) {528 const app = express();529 app.get('/', function (req, res) {530 res.send('hey');531 });532 request(app)533 .get('/')534 .expect('Content-Type', /^application/)535 .end(function (err) {536 expect(err.message).toEqual('expected "Content-Type" matching /^application/, '537 + 'got "text/html; charset=utf-8"')538 shouldIncludeStackWithThisFile(err);539 done();540 });541 });542 it('should support numbers', function (done) {543 const app = express();544 app.get('/', function (req, res) {545 res.send('hey');546 });547 request(app)548 .get('/')549 .expect('Content-Length', 4)550 .end(function (err) {551 expect(err.message).toEqual('expected "Content-Length" of "4", got "3"')552 shouldIncludeStackWithThisFile(err);553 done();554 });555 });556 describe('handling arbitrary expect functions', function () {557 let app;558 let get;559 beforeAll(function () {560 app = express();561 app.get('/', function (req, res) {562 res.send('hey');563 });564 });565 beforeEach(function () {566 get = request(app).get('/');567 });568 it('reports errors', function (done) {569 get570 .expect(function (res) {571 throw new Error('failed');572 })573 .end(function (err) {574 expect(err.message).toEqual('failed')575 shouldIncludeStackWithThisFile(err);576 done();577 });578 });579 it(580 'ensures truthy non-errors returned from asserts are not promoted to errors',581 function (done) {582 get583 .expect(function (res) {584 return 'some descriptive error';585 })586 .end(function (err) {587 expect(err).toBeFalsy()588 done();589 });590 }591 );592 it('ensures truthy errors returned from asserts are throw to end', function (done) {593 get594 .expect(function (res) {595 return new Error('some descriptive error');596 })597 .end(function (err) {598 expect(err.message).toEqual('some descriptive error')599 shouldIncludeStackWithThisFile(err);600 expect(err instanceof Error).toBeTruthy()601 done();602 });603 });604 it("doesn't create false negatives", function (done) {605 get606 .expect(function (res) {607 })608 .end(done);609 });610 it("doesn't create false negatives on non error objects", function (done) {611 const handler = {612 get: function(target, prop, receiver) {613 throw Error('Should not be called for non Error objects');614 }615 };616 const proxy = new Proxy({}, handler); // eslint-disable-line no-undef617 get618 .expect(() => proxy)619 .end(done);620 });621 it('handles multiple asserts', function (done) {622 const calls = [];623 get624 .expect(function (res) {625 calls[0] = 1;626 })627 .expect(function (res) {628 calls[1] = 1;629 })630 .expect(function (res) {631 calls[2] = 1;632 })633 .end(function () {634 const callCount = [0, 1, 2].reduce(function (count, i) {635 return count + calls[i];636 }, 0);637 expect(callCount).toEqual(3)638 done();639 });640 });641 it('plays well with normal assertions - no false positives', function (done) {642 get643 .expect(function (res) {644 })645 .expect('Content-Type', /json/)646 .end(function (err) {647 expect(err.message).toMatch(/Content-Type/)648 shouldIncludeStackWithThisFile(err);649 done();650 });651 });652 it('plays well with normal assertions - no false negatives', function (done) {653 get654 .expect(function (res) {655 })656 .expect('Content-Type', /html/)657 .expect(function (res) {658 })659 .expect('Content-Type', /text/)660 .end(done);661 });662 });663 describe('handling multiple assertions per field', function () {664 it('should work', function (done) {665 const app = express();666 app.get('/', function (req, res) {667 res.send('hey');668 });669 request(app)670 .get('/')671 .expect('Content-Type', /text/)672 .expect('Content-Type', /html/)673 .end(done);674 });675 it('should return an error if the first one fails', function (done) {676 const app = express();677 app.get('/', function (req, res) {678 res.send('hey');679 });680 request(app)681 .get('/')682 .expect('Content-Type', /bloop/)683 .expect('Content-Type', /html/)684 .end(function (err) {685 expect(err.message).toEqual('expected "Content-Type" matching /bloop/, '686 + 'got "text/html; charset=utf-8"')687 shouldIncludeStackWithThisFile(err);688 done();689 });690 });691 it('should return an error if a middle one fails', function (done) {692 const app = express();693 app.get('/', function (req, res) {694 res.send('hey');695 });696 request(app)697 .get('/')698 .expect('Content-Type', /text/)699 .expect('Content-Type', /bloop/)700 .expect('Content-Type', /html/)701 .end(function (err) {702 expect(err.message).toEqual('expected "Content-Type" matching /bloop/, '703 + 'got "text/html; charset=utf-8"')704 shouldIncludeStackWithThisFile(err);705 done();706 });707 });708 it('should return an error if the last one fails', function (done) {709 const app = express();710 app.get('/', function (req, res) {711 res.send('hey');712 });713 request(app)714 .get('/')715 .expect('Content-Type', /text/)716 .expect('Content-Type', /html/)717 .expect('Content-Type', /bloop/)718 .end(function (err) {719 expect(err.message).toEqual('expected "Content-Type" matching /bloop/, '720 + 'got "text/html; charset=utf-8"')721 shouldIncludeStackWithThisFile(err);722 done();723 });724 });725 });726 });727});728describe('request.agent(app)', function () {729 const app = express();730 const agent = new request.agent(app)731 .set('header', 'hey');732 app.use(cookieParser());733 app.get('/', function (req, res) {734 res.cookie('cookie', 'hey');735 res.send();736 });737 app.get('/return_cookies', function (req, res) {738 if (req.cookies.cookie) res.send(req.cookies.cookie);739 else res.send(':(');740 });741 app.get('/return_headers', function (req, res) {742 if (req.get('header')) res.send(req.get('header'));743 else res.send(':(');744 });745 it('should save cookies', function (done) {746 agent747 .get('/')748 .expect('set-cookie', 'cookie=hey; Path=/', done);749 });750 it('should send cookies', function (done) {751 agent752 .get('/return_cookies')753 .expect('hey', done);754 });755 it('should send global agent headers', function (done) {756 agent757 .get('/return_headers')758 .expect('hey', done);759 });760});761describe('agent.host(host)', function () {762 it('should set request hostname', function (done) {763 const app = express();764 const agent = new request.agent(app);765 app.get('/', function (req, res) {766 res.send({ hostname: req.hostname });767 });768 agent769 .host('something.test')770 .get('/')771 .end(function (err, res) {772 if (err) return done(err);773 expect(res.body.hostname).toEqual('something.test')774 done();775 });776 });777});778describe('.<http verb> works as expected', function () {779 it('.delete should work', function (done) {780 const app = express();781 app.delete('/', function (req, res) {782 res.sendStatus(200);783 });784 request(app)785 .delete('/')786 .expect(200, done);787 });788 it('.del should work', function (done) {789 const app = express();790 app.delete('/', function (req, res) {791 res.sendStatus(200);792 });793 request(app)794 .del('/')795 .expect(200, done);796 });797 it('.get should work', function (done) {798 const app = express();799 app.get('/', function (req, res) {800 res.sendStatus(200);801 });802 request(app)803 .get('/')804 .expect(200, done);805 });806 it('.post should work', function (done) {807 const app = express();808 app.post('/', function (req, res) {809 res.sendStatus(200);810 });811 request(app)812 .post('/')813 .expect(200, done);814 });815 it('.put should work', function (done) {816 const app = express();817 app.put('/', function (req, res) {818 res.sendStatus(200);819 });820 request(app)821 .put('/')822 .expect(200, done);823 });824 it('.head should work', function (done) {825 const app = express();826 app.head('/', function (req, res) {827 res.statusCode = 200;828 res.set('Content-Encoding', 'gzip');829 res.set('Content-Length', '1024');830 res.status(200);831 res.end();832 });833 request(app)834 .head('/')835 .set('accept-encoding', 'gzip, deflate')836 .end(function (err, res) {837 if (err) return done(err);838 expect(res.statusCode).toEqual(200)839 expect(res.headers['content-length']).toEqual('1024')840 done();841 });842 });843});844describe('assert ordering by call order', function () {845 it('should assert the body before status', function (done) {846 const app = express();847 app.set('json spaces', 0);848 app.get('/', function (req, res) {849 res.status(500).json({ message: 'something went wrong' });850 });851 request(app)852 .get('/')853 .expect('hey')854 .expect(200)855 .end(function (err, res) {856 expect(err.message).toEqual('expected \'hey\' response body, '857 + 'got \'{"message":"something went wrong"}\'')858 shouldIncludeStackWithThisFile(err);859 done();860 });861 });862 it('should assert the status before body', function (done) {863 const app = express();864 app.set('json spaces', 0);865 app.get('/', function (req, res) {866 res.status(500).json({ message: 'something went wrong' });867 });868 request(app)869 .get('/')870 .expect(200)871 .expect('hey')872 .end(function (err, res) {873 expect(err.message).toEqual('expected 200 "OK", got 500 "Internal Server Error"');874 shouldIncludeStackWithThisFile(err);875 done();876 });877 });878 it('should assert the fields before body and status', function (done) {879 const app = express();880 app.set('json spaces', 0);881 app.get('/', function (req, res) {882 res.status(200).json({ hello: 'world' });883 });884 request(app)885 .get('/')886 .expect('content-type', /html/)887 .expect('hello')888 .end(function (err, res) {889 expect(err.message).toEqual('expected "content-type" matching /html/, '890 + 'got "application/json; charset=utf-8"');891 shouldIncludeStackWithThisFile(err);892 done();893 });894 });895 it('should call the expect function in order', function (done) {896 const app = express();897 app.get('/', function (req, res) {898 res.status(200).json({});899 });900 request(app)901 .get('/')902 .expect(function (res) {903 res.body.first = 1;904 })905 .expect(function (res) {906 expect(res.body.first === 1).toBeTruthy()907 res.body.second = 2;908 })909 .end(function (err, res) {910 if (err) {911 return done(err);912 }913 expect(res.body.first === 1).toBeTruthy()914 expect(res.body.second === 2).toBeTruthy()915 done();916 });917 });918 it('should call expect(fn) and expect(status, fn) in order', function (done) {919 const app = express();920 app.get('/', function (req, res) {921 res.status(200).json({});922 });923 request(app)924 .get('/')925 .expect(function (res) {926 res.body.first = 1;927 })928 .expect(200, function (err, res) {929 expect(err === null).toBeTruthy()930 expect(res.body.first === 1).toBeTruthy()931 done();932 });933 });934 it('should call expect(fn) and expect(header,value) in order', function (done) {935 const app = express();936 app.get('/', function (req, res) {937 res938 .set('X-Some-Header', 'Some value')939 .send();940 });941 request(app)942 .get('/')943 .expect('X-Some-Header', 'Some value')944 .expect(function (res) {945 res.headers['x-some-header'] = '';946 })947 .expect('X-Some-Header', '')948 .end(done);949 });950 it('should call expect(fn) and expect(body) in order', function (done) {951 const app = express();952 app.get('/', function (req, res) {953 res.json({ somebody: 'some body value' });954 });955 request(app)956 .get('/')957 .expect(/some body value/)958 .expect(function (res) {959 res.body.somebody = 'nobody';960 })961 .expect(/some body value/) // res.text should not be modified.962 .expect({ somebody: 'nobody' })963 .expect(function (res) {964 res.text = 'gone';965 })966 .expect('gone')967 .expect(/gone/)968 .expect({ somebody: 'nobody' }) // res.body should not be modified969 .expect('gone', done);970 });971});972describe('request.get(url).query(vals) works as expected', function () {973 it('normal single query string value works', function (done) {974 const app = express();975 app.get('/', function (req, res) {976 res.status(200).send(req.query.val);977 });978 request(app)979 .get('/')980 .query({ val: 'Test1' })981 .expect(200, function (err, res) {982 expect(res.text).toEqual('Test1')983 done();984 });985 });986 it('array query string value works', function (done) {987 const app = express();988 app.get('/', function (req, res) {989 res.status(200).send(Array.isArray(req.query.val));990 });991 request(app)992 .get('/')993 .query({ 'val[]': ['Test1', 'Test2'] })994 .expect(200, function (err, res) {995 expect(res.req.path).toEqual('/?val%5B%5D=Test1&val%5B%5D=Test2')996 expect(res.text).toEqual('true')997 done();998 });999 });1000 it('array query string value work even with single value', function (done) {1001 const app = express();1002 app.get('/', function (req, res) {1003 res.status(200).send(Array.isArray(req.query.val));1004 });1005 request(app)1006 .get('/')1007 .query({ 'val[]': ['Test1'] })1008 .expect(200, function (err, res) {1009 expect(res.req.path).toEqual('/?val%5B%5D=Test1');1010 expect(res.text).toEqual('true');1011 done();1012 });1013 });1014 it('object query string value works', function (done) {1015 const app = express();1016 app.get('/', function (req, res) {1017 res.status(200).send(req.query.val.test);1018 });1019 request(app)1020 .get('/')1021 .query({ val: { test: 'Test1' } })1022 .expect(200, function (err, res) {1023 expect(res.text).toEqual('Test1');1024 done();1025 });1026 });1027 it('handles unknown errors', function (done) {1028 const app = express();1029 nock.disableNetConnect();1030 app.get('/', function (req, res) {1031 res.status(200).send('OK');1032 });1033 request(app)1034 .get('/')1035 // This expect should never get called, but exposes this issue with other1036 // errors being obscured by the response assertions1037 // https://github.com/visionmedia/supertest/issues/3521038 .expect(200)1039 .end(function (err, res) {1040 expect(err.__proto__.name).toEqual('Error')1041 expect(err.message).toMatch(/Nock: Disallowed net connect/)1042 shouldIncludeStackWithThisFile(err);1043 done();1044 });1045 nock.restore();1046 });1047 it('should assert using promises', function (done) {1048 const app = express();1049 app.get('/', function (req, res) {1050 res.status(400).send({ promise: true });1051 });1052 request(app)1053 .get('/')1054 .expect(400)1055 .then((res) => {1056 expect(res.body.promise).toBeTruthy()...

Full Screen

Full Screen

supertest.js

Source:supertest.js Github

copy

Full Screen

...8const bodyParser = require('body-parser');9const cookieParser = require('cookie-parser');10const nock = require('nock');11process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';12function shouldIncludeStackWithThisFile(err) {13 err.stack.should.match(/test\/supertest.js:/);14 err.stack.should.startWith(err.name + ':');15}16describe('request(url)', function () {17 it('should be supported', function (done) {18 const app = express();19 let s;20 app.get('/', function (req, res) {21 res.send('hello');22 });23 s = app.listen(function () {24 const url = 'http://localhost:' + s.address().port;25 request(url)26 .get('/')27 .expect('hello', done);28 });29 });30 describe('.end(cb)', function () {31 it('should set `this` to the test object when calling cb', function (done) {32 const app = express();33 let s;34 app.get('/', function (req, res) {35 res.send('hello');36 });37 s = app.listen(function () {38 const url = 'http://localhost:' + s.address().port;39 const test = request(url).get('/');40 test.end(function (err, res) {41 this.should.eql(test);42 done();43 });44 });45 });46 });47});48describe('request(app)', function () {49 it('should fire up the app on an ephemeral port', function (done) {50 const app = express();51 app.get('/', function (req, res) {52 res.send('hey');53 });54 request(app)55 .get('/')56 .end(function (err, res) {57 res.status.should.equal(200);58 res.text.should.equal('hey');59 done();60 });61 });62 it('should work with an active server', function (done) {63 const app = express();64 let server;65 app.get('/', function (req, res) {66 res.send('hey');67 });68 server = app.listen(4000, function () {69 request(server)70 .get('/')71 .end(function (err, res) {72 res.status.should.equal(200);73 res.text.should.equal('hey');74 done();75 });76 });77 });78 it('should work with remote server', function (done) {79 const app = express();80 app.get('/', function (req, res) {81 res.send('hey');82 });83 app.listen(4001, function () {84 request('http://localhost:4001')85 .get('/')86 .end(function (err, res) {87 res.status.should.equal(200);88 res.text.should.equal('hey');89 done();90 });91 });92 });93 it('should work with a https server', function (done) {94 const app = express();95 const fixtures = path.join(__dirname, 'fixtures');96 const server = https.createServer({97 key: fs.readFileSync(path.join(fixtures, 'test_key.pem')),98 cert: fs.readFileSync(path.join(fixtures, 'test_cert.pem'))99 }, app);100 app.get('/', function (req, res) {101 res.send('hey');102 });103 request(server)104 .get('/')105 .end(function (err, res) {106 if (err) return done(err);107 res.status.should.equal(200);108 res.text.should.equal('hey');109 done();110 });111 });112 it('should work with .send() etc', function (done) {113 const app = express();114 app.use(bodyParser.json());115 app.post('/', function (req, res) {116 res.send(req.body.name);117 });118 request(app)119 .post('/')120 .send({ name: 'john' })121 .expect('john', done);122 });123 it('should work when unbuffered', function (done) {124 const app = express();125 app.get('/', function (req, res) {126 res.end('Hello');127 });128 request(app)129 .get('/')130 .expect('Hello', done);131 });132 it('should default redirects to 0', function (done) {133 const app = express();134 app.get('/', function (req, res) {135 res.redirect('/login');136 });137 request(app)138 .get('/')139 .expect(302, done);140 });141 it('should handle redirects', function (done) {142 const app = express();143 app.get('/login', function (req, res) {144 res.end('Login');145 });146 app.get('/', function (req, res) {147 res.redirect('/login');148 });149 request(app)150 .get('/')151 .redirects(1)152 .end(function (err, res) {153 should.exist(res);154 res.status.should.be.equal(200);155 res.text.should.be.equal('Login');156 done();157 });158 });159 it('should handle socket errors', function (done) {160 const app = express();161 app.get('/', function (req, res) {162 res.destroy();163 });164 request(app)165 .get('/')166 .end(function (err) {167 should.exist(err);168 done();169 });170 });171 describe('.end(fn)', function () {172 it('should close server', function (done) {173 const app = express();174 let test;175 app.get('/', function (req, res) {176 res.send('supertest FTW!');177 });178 test = request(app)179 .get('/')180 .end(function () {181 });182 test._server.on('close', function () {183 done();184 });185 });186 it('should wait for server to close before invoking fn', function (done) {187 const app = express();188 let closed = false;189 let test;190 app.get('/', function (req, res) {191 res.send('supertest FTW!');192 });193 test = request(app)194 .get('/')195 .end(function () {196 closed.should.be.true;197 done();198 });199 test._server.on('close', function () {200 closed = true;201 });202 });203 it('should support nested requests', function (done) {204 const app = express();205 const test = request(app);206 app.get('/', function (req, res) {207 res.send('supertest FTW!');208 });209 test210 .get('/')211 .end(function () {212 test213 .get('/')214 .end(function (err, res) {215 (err === null).should.be.true;216 res.status.should.equal(200);217 res.text.should.equal('supertest FTW!');218 done();219 });220 });221 });222 it('should include the response in the error callback', function (done) {223 const app = express();224 app.get('/', function (req, res) {225 res.send('whatever');226 });227 request(app)228 .get('/')229 .expect(function () {230 throw new Error('Some error');231 })232 .end(function (err, res) {233 should.exist(err);234 should.exist(res);235 // Duck-typing response, just in case.236 res.status.should.equal(200);237 done();238 });239 });240 it('should set `this` to the test object when calling the error callback', function (done) {241 const app = express();242 let test;243 app.get('/', function (req, res) {244 res.send('whatever');245 });246 test = request(app).get('/');247 test.expect(function () {248 throw new Error('Some error');249 }).end(function (err, res) {250 should.exist(err);251 this.should.eql(test);252 done();253 });254 });255 it('should handle an undefined Response', function (done) {256 const app = express();257 let server;258 app.get('/', function (req, res) {259 setTimeout(function () {260 res.end();261 }, 20);262 });263 server = app.listen(function () {264 const url = 'http://localhost:' + server.address().port;265 request(url)266 .get('/')267 .timeout(1)268 .expect(200, function (err) {269 err.should.be.an.instanceof(Error);270 return done();271 });272 });273 });274 it('should handle error returned when server goes down', function (done) {275 const app = express();276 let server;277 app.get('/', function (req, res) {278 res.end();279 });280 server = app.listen(function () {281 const url = 'http://localhost:' + server.address().port;282 server.close();283 request(url)284 .get('/')285 .expect(200, function (err) {286 err.should.be.an.instanceof(Error);287 return done();288 });289 });290 });291 });292 describe('.expect(status[, fn])', function () {293 it('should assert the response status', function (done) {294 const app = express();295 app.get('/', function (req, res) {296 res.send('hey');297 });298 request(app)299 .get('/')300 .expect(404)301 .end(function (err, res) {302 err.message.should.equal('expected 404 "Not Found", got 200 "OK"');303 shouldIncludeStackWithThisFile(err);304 done();305 });306 });307 });308 describe('.expect(status)', function () {309 it('should handle connection error', function (done) {310 const req = request.agent('http://localhost:1234');311 req312 .get('/')313 .expect(200)314 .end(function (err, res) {315 err.message.should.equal('ECONNREFUSED: Connection refused');316 done();317 });318 });319 });320 describe('.expect(status)', function () {321 it('should assert only status', function (done) {322 const app = express();323 app.get('/', function (req, res) {324 res.send('hey');325 });326 request(app)327 .get('/')328 .expect(200)329 .end(done);330 });331 });332 describe('.expect(status, body[, fn])', function () {333 it('should assert the response body and status', function (done) {334 const app = express();335 app.get('/', function (req, res) {336 res.send('foo');337 });338 request(app)339 .get('/')340 .expect(200, 'foo', done);341 });342 describe('when the body argument is an empty string', function () {343 it('should not quietly pass on failure', function (done) {344 const app = express();345 app.get('/', function (req, res) {346 res.send('foo');347 });348 request(app)349 .get('/')350 .expect(200, '')351 .end(function (err, res) {352 err.message.should.equal('expected \'\' response body, got \'foo\'');353 shouldIncludeStackWithThisFile(err);354 done();355 });356 });357 });358 });359 describe('.expect(body[, fn])', function () {360 it('should assert the response body', function (done) {361 const app = express();362 app.set('json spaces', 0);363 app.get('/', function (req, res) {364 res.send({ foo: 'bar' });365 });366 request(app)367 .get('/')368 .expect('hey')369 .end(function (err, res) {370 err.message.should.equal('expected \'hey\' response body, got \'{"foo":"bar"}\'');371 shouldIncludeStackWithThisFile(err);372 done();373 });374 });375 it('should assert the status before the body', function (done) {376 const app = express();377 app.set('json spaces', 0);378 app.get('/', function (req, res) {379 res.status(500).send({ message: 'something went wrong' });380 });381 request(app)382 .get('/')383 .expect(200)384 .expect('hey')385 .end(function (err, res) {386 err.message.should.equal('expected 200 "OK", got 500 "Internal Server Error"');387 shouldIncludeStackWithThisFile(err);388 done();389 });390 });391 it('should assert the response text', function (done) {392 const app = express();393 app.set('json spaces', 0);394 app.get('/', function (req, res) {395 res.send({ foo: 'bar' });396 });397 request(app)398 .get('/')399 .expect('{"foo":"bar"}', done);400 });401 it('should assert the parsed response body', function (done) {402 const app = express();403 app.set('json spaces', 0);404 app.get('/', function (req, res) {405 res.send({ foo: 'bar' });406 });407 request(app)408 .get('/')409 .expect({ foo: 'baz' })410 .end(function (err, res) {411 err.message.should.equal('expected { foo: \'baz\' } response body, got { foo: \'bar\' }');412 shouldIncludeStackWithThisFile(err);413 request(app)414 .get('/')415 .expect({ foo: 'bar' })416 .end(done);417 });418 });419 it('should test response object types', function (done) {420 const app = express();421 app.get('/', function (req, res) {422 res.status(200).json({ stringValue: 'foo', numberValue: 3 });423 });424 request(app)425 .get('/')426 .expect({ stringValue: 'foo', numberValue: 3 }, done);427 });428 it('should deep test response object types', function (done) {429 const app = express();430 app.get('/', function (req, res) {431 res.status(200)432 .json({ stringValue: 'foo', numberValue: 3, nestedObject: { innerString: '5' } });433 });434 request(app)435 .get('/')436 .expect({ stringValue: 'foo', numberValue: 3, nestedObject: { innerString: 5 } })437 .end(function (err, res) {438 err.message.replace(/[^a-zA-Z]/g, '').should.equal('expected {\n stringValue: \'foo\',\n numberValue: 3,\n nestedObject: { innerString: 5 }\n} response body, got {\n stringValue: \'foo\',\n numberValue: 3,\n nestedObject: { innerString: \'5\' }\n}'.replace(/[^a-zA-Z]/g, '')); // eslint-disable-line max-len439 shouldIncludeStackWithThisFile(err);440 request(app)441 .get('/')442 .expect({ stringValue: 'foo', numberValue: 3, nestedObject: { innerString: '5' } })443 .end(done);444 });445 });446 it('should support regular expressions', function (done) {447 const app = express();448 app.get('/', function (req, res) {449 res.send('foobar');450 });451 request(app)452 .get('/')453 .expect(/^bar/)454 .end(function (err, res) {455 err.message.should.equal('expected body \'foobar\' to match /^bar/');456 shouldIncludeStackWithThisFile(err);457 done();458 });459 });460 it('should assert response body multiple times', function (done) {461 const app = express();462 app.get('/', function (req, res) {463 res.send('hey tj');464 });465 request(app)466 .get('/')467 .expect(/tj/)468 .expect('hey')469 .expect('hey tj')470 .end(function (err, res) {471 err.message.should.equal("expected 'hey' response body, got 'hey tj'");472 shouldIncludeStackWithThisFile(err);473 done();474 });475 });476 it('should assert response body multiple times with no exception', function (done) {477 const app = express();478 app.get('/', function (req, res) {479 res.send('hey tj');480 });481 request(app)482 .get('/')483 .expect(/tj/)484 .expect(/^hey/)485 .expect('hey tj', done);486 });487 });488 describe('.expect(field, value[, fn])', function () {489 it('should assert the header field presence', function (done) {490 const app = express();491 app.get('/', function (req, res) {492 res.send({ foo: 'bar' });493 });494 request(app)495 .get('/')496 .expect('Content-Foo', 'bar')497 .end(function (err, res) {498 err.message.should.equal('expected "Content-Foo" header field');499 shouldIncludeStackWithThisFile(err);500 done();501 });502 });503 it('should assert the header field value', function (done) {504 const app = express();505 app.get('/', function (req, res) {506 res.send({ foo: 'bar' });507 });508 request(app)509 .get('/')510 .expect('Content-Type', 'text/html')511 .end(function (err, res) {512 err.message.should.equal('expected "Content-Type" of "text/html", '513 + 'got "application/json; charset=utf-8"');514 shouldIncludeStackWithThisFile(err);515 done();516 });517 });518 it('should assert multiple fields', function (done) {519 const app = express();520 app.get('/', function (req, res) {521 res.send('hey');522 });523 request(app)524 .get('/')525 .expect('Content-Type', 'text/html; charset=utf-8')526 .expect('Content-Length', '3')527 .end(done);528 });529 it('should support regular expressions', function (done) {530 const app = express();531 app.get('/', function (req, res) {532 res.send('hey');533 });534 request(app)535 .get('/')536 .expect('Content-Type', /^application/)537 .end(function (err) {538 err.message.should.equal('expected "Content-Type" matching /^application/, '539 + 'got "text/html; charset=utf-8"');540 shouldIncludeStackWithThisFile(err);541 done();542 });543 });544 it('should support numbers', function (done) {545 const app = express();546 app.get('/', function (req, res) {547 res.send('hey');548 });549 request(app)550 .get('/')551 .expect('Content-Length', 4)552 .end(function (err) {553 err.message.should.equal('expected "Content-Length" of "4", got "3"');554 shouldIncludeStackWithThisFile(err);555 done();556 });557 });558 describe('handling arbitrary expect functions', function () {559 let app;560 let get;561 before(function () {562 app = express();563 app.get('/', function (req, res) {564 res.send('hey');565 });566 });567 beforeEach(function () {568 get = request(app).get('/');569 });570 it('reports errors', function (done) {571 get572 .expect(function (res) {573 throw new Error('failed');574 })575 .end(function (err) {576 err.message.should.equal('failed');577 shouldIncludeStackWithThisFile(err);578 done();579 });580 });581 it(582 'ensures truthy non-errors returned from asserts are not promoted to errors',583 function (done) {584 get585 .expect(function (res) {586 return 'some descriptive error';587 })588 .end(function (err) {589 should.not.exist(err);590 done();591 });592 }593 );594 it('ensures truthy errors returned from asserts are throw to end', function (done) {595 get596 .expect(function (res) {597 return new Error('some descriptive error');598 })599 .end(function (err) {600 err.message.should.equal('some descriptive error');601 shouldIncludeStackWithThisFile(err);602 (err instanceof Error).should.be.true;603 done();604 });605 });606 it("doesn't create false negatives", function (done) {607 get608 .expect(function (res) {609 })610 .end(done);611 });612 it("doesn't create false negatives on non error objects", function (done) {613 const handler = {614 get: function(target, prop, receiver) {615 throw Error('Should not be called for non Error objects');616 }617 };618 const proxy = new Proxy({}, handler); // eslint-disable-line no-undef619 get620 .expect(() => proxy)621 .end(done);622 });623 it('handles multiple asserts', function (done) {624 const calls = [];625 get626 .expect(function (res) {627 calls[0] = 1;628 })629 .expect(function (res) {630 calls[1] = 1;631 })632 .expect(function (res) {633 calls[2] = 1;634 })635 .end(function () {636 const callCount = [0, 1, 2].reduce(function (count, i) {637 return count + calls[i];638 }, 0);639 callCount.should.equal(3, "didn't see all assertions run");640 done();641 });642 });643 it('plays well with normal assertions - no false positives', function (done) {644 get645 .expect(function (res) {646 })647 .expect('Content-Type', /json/)648 .end(function (err) {649 err.message.should.match(/Content-Type/);650 shouldIncludeStackWithThisFile(err);651 done();652 });653 });654 it('plays well with normal assertions - no false negatives', function (done) {655 get656 .expect(function (res) {657 })658 .expect('Content-Type', /html/)659 .expect(function (res) {660 })661 .expect('Content-Type', /text/)662 .end(done);663 });664 });665 describe('handling multiple assertions per field', function () {666 it('should work', function (done) {667 const app = express();668 app.get('/', function (req, res) {669 res.send('hey');670 });671 request(app)672 .get('/')673 .expect('Content-Type', /text/)674 .expect('Content-Type', /html/)675 .end(done);676 });677 it('should return an error if the first one fails', function (done) {678 const app = express();679 app.get('/', function (req, res) {680 res.send('hey');681 });682 request(app)683 .get('/')684 .expect('Content-Type', /bloop/)685 .expect('Content-Type', /html/)686 .end(function (err) {687 err.message.should.equal('expected "Content-Type" matching /bloop/, '688 + 'got "text/html; charset=utf-8"');689 shouldIncludeStackWithThisFile(err);690 done();691 });692 });693 it('should return an error if a middle one fails', function (done) {694 const app = express();695 app.get('/', function (req, res) {696 res.send('hey');697 });698 request(app)699 .get('/')700 .expect('Content-Type', /text/)701 .expect('Content-Type', /bloop/)702 .expect('Content-Type', /html/)703 .end(function (err) {704 err.message.should.equal('expected "Content-Type" matching /bloop/, '705 + 'got "text/html; charset=utf-8"');706 shouldIncludeStackWithThisFile(err);707 done();708 });709 });710 it('should return an error if the last one fails', function (done) {711 const app = express();712 app.get('/', function (req, res) {713 res.send('hey');714 });715 request(app)716 .get('/')717 .expect('Content-Type', /text/)718 .expect('Content-Type', /html/)719 .expect('Content-Type', /bloop/)720 .end(function (err) {721 err.message.should.equal('expected "Content-Type" matching /bloop/, '722 + 'got "text/html; charset=utf-8"');723 shouldIncludeStackWithThisFile(err);724 done();725 });726 });727 });728 });729});730describe('request.agent(app)', function () {731 const app = express();732 const agent = request.agent(app)733 .set('header', 'hey');734 app.use(cookieParser());735 app.get('/', function (req, res) {736 res.cookie('cookie', 'hey');737 res.send();738 });739 app.get('/return_cookies', function (req, res) {740 if (req.cookies.cookie) res.send(req.cookies.cookie);741 else res.send(':(');742 });743 app.get('/return_headers', function (req, res) {744 if (req.get('header')) res.send(req.get('header'));745 else res.send(':(');746 });747 it('should save cookies', function (done) {748 agent749 .get('/')750 .expect('set-cookie', 'cookie=hey; Path=/', done);751 });752 it('should send cookies', function (done) {753 agent754 .get('/return_cookies')755 .expect('hey', done);756 });757 it('should send global agent headers', function (done) {758 agent759 .get('/return_headers')760 .expect('hey', done);761 });762});763describe('agent.host(host)', function () {764 it('should set request hostname', function (done) {765 const app = express();766 const agent = request.agent(app);767 app.get('/', function (req, res) {768 res.send({ hostname: req.hostname });769 });770 agent771 .host('something.test')772 .get('/')773 .end(function (err, res) {774 if (err) return done(err);775 res.body.hostname.should.equal('something.test');776 done();777 });778 });779});780describe('.<http verb> works as expected', function () {781 it('.delete should work', function (done) {782 const app = express();783 app.delete('/', function (req, res) {784 res.sendStatus(200);785 });786 request(app)787 .delete('/')788 .expect(200, done);789 });790 it('.del should work', function (done) {791 const app = express();792 app.delete('/', function (req, res) {793 res.sendStatus(200);794 });795 request(app)796 .del('/')797 .expect(200, done);798 });799 it('.get should work', function (done) {800 const app = express();801 app.get('/', function (req, res) {802 res.sendStatus(200);803 });804 request(app)805 .get('/')806 .expect(200, done);807 });808 it('.post should work', function (done) {809 const app = express();810 app.post('/', function (req, res) {811 res.sendStatus(200);812 });813 request(app)814 .post('/')815 .expect(200, done);816 });817 it('.put should work', function (done) {818 const app = express();819 app.put('/', function (req, res) {820 res.sendStatus(200);821 });822 request(app)823 .put('/')824 .expect(200, done);825 });826 it('.head should work', function (done) {827 const app = express();828 app.head('/', function (req, res) {829 res.statusCode = 200;830 res.set('Content-Encoding', 'gzip');831 res.set('Content-Length', '1024');832 res.status(200);833 res.end();834 });835 request(app)836 .head('/')837 .set('accept-encoding', 'gzip, deflate')838 .end(function (err, res) {839 if (err) return done(err);840 res.should.have.property('statusCode', 200);841 res.headers.should.have.property('content-length', '1024');842 done();843 });844 });845});846describe('assert ordering by call order', function () {847 it('should assert the body before status', function (done) {848 const app = express();849 app.set('json spaces', 0);850 app.get('/', function (req, res) {851 res.status(500).json({ message: 'something went wrong' });852 });853 request(app)854 .get('/')855 .expect('hey')856 .expect(200)857 .end(function (err, res) {858 err.message.should.equal('expected \'hey\' response body, '859 + 'got \'{"message":"something went wrong"}\'');860 shouldIncludeStackWithThisFile(err);861 done();862 });863 });864 it('should assert the status before body', function (done) {865 const app = express();866 app.set('json spaces', 0);867 app.get('/', function (req, res) {868 res.status(500).json({ message: 'something went wrong' });869 });870 request(app)871 .get('/')872 .expect(200)873 .expect('hey')874 .end(function (err, res) {875 err.message.should.equal('expected 200 "OK", got 500 "Internal Server Error"');876 shouldIncludeStackWithThisFile(err);877 done();878 });879 });880 it('should assert the fields before body and status', function (done) {881 const app = express();882 app.set('json spaces', 0);883 app.get('/', function (req, res) {884 res.status(200).json({ hello: 'world' });885 });886 request(app)887 .get('/')888 .expect('content-type', /html/)889 .expect('hello')890 .end(function (err, res) {891 err.message.should.equal('expected "content-type" matching /html/, '892 + 'got "application/json; charset=utf-8"');893 shouldIncludeStackWithThisFile(err);894 done();895 });896 });897 it('should call the expect function in order', function (done) {898 const app = express();899 app.get('/', function (req, res) {900 res.status(200).json({});901 });902 request(app)903 .get('/')904 .expect(function (res) {905 res.body.first = 1;906 })907 .expect(function (res) {908 (res.body.first === 1).should.be.true;909 res.body.second = 2;910 })911 .end(function (err, res) {912 if (err) return done(err);913 (res.body.first === 1).should.be.true;914 (res.body.second === 2).should.be.true;915 done();916 });917 });918 it('should call expect(fn) and expect(status, fn) in order', function (done) {919 const app = express();920 app.get('/', function (req, res) {921 res.status(200).json({});922 });923 request(app)924 .get('/')925 .expect(function (res) {926 res.body.first = 1;927 })928 .expect(200, function (err, res) {929 (err === null).should.be.true;930 (res.body.first === 1).should.be.true;931 done();932 });933 });934 it('should call expect(fn) and expect(header,value) in order', function (done) {935 const app = express();936 app.get('/', function (req, res) {937 res938 .set('X-Some-Header', 'Some value')939 .send();940 });941 request(app)942 .get('/')943 .expect('X-Some-Header', 'Some value')944 .expect(function (res) {945 res.headers['x-some-header'] = '';946 })947 .expect('X-Some-Header', '')948 .end(done);949 });950 it('should call expect(fn) and expect(body) in order', function (done) {951 const app = express();952 app.get('/', function (req, res) {953 res.json({ somebody: 'some body value' });954 });955 request(app)956 .get('/')957 .expect(/some body value/)958 .expect(function (res) {959 res.body.somebody = 'nobody';960 })961 .expect(/some body value/) // res.text should not be modified.962 .expect({ somebody: 'nobody' })963 .expect(function (res) {964 res.text = 'gone';965 })966 .expect('gone')967 .expect(/gone/)968 .expect({ somebody: 'nobody' }) // res.body should not be modified969 .expect('gone', done);970 });971});972describe('request.get(url).query(vals) works as expected', function () {973 it('normal single query string value works', function (done) {974 const app = express();975 app.get('/', function (req, res) {976 res.status(200).send(req.query.val);977 });978 request(app)979 .get('/')980 .query({ val: 'Test1' })981 .expect(200, function (err, res) {982 res.text.should.be.equal('Test1');983 done();984 });985 });986 it('array query string value works', function (done) {987 const app = express();988 app.get('/', function (req, res) {989 res.status(200).send(Array.isArray(req.query.val));990 });991 request(app)992 .get('/')993 .query({ 'val[]': ['Test1', 'Test2'] })994 .expect(200, function (err, res) {995 res.req.path.should.be.equal('/?val%5B%5D=Test1&val%5B%5D=Test2');996 res.text.should.be.equal('true');997 done();998 });999 });1000 it('array query string value work even with single value', function (done) {1001 const app = express();1002 app.get('/', function (req, res) {1003 res.status(200).send(Array.isArray(req.query.val));1004 });1005 request(app)1006 .get('/')1007 .query({ 'val[]': ['Test1'] })1008 .expect(200, function (err, res) {1009 res.req.path.should.be.equal('/?val%5B%5D=Test1');1010 res.text.should.be.equal('true');1011 done();1012 });1013 });1014 it('object query string value works', function (done) {1015 const app = express();1016 app.get('/', function (req, res) {1017 res.status(200).send(req.query.val.test);1018 });1019 request(app)1020 .get('/')1021 .query({ val: { test: 'Test1' } })1022 .expect(200, function (err, res) {1023 res.text.should.be.equal('Test1');1024 done();1025 });1026 });1027 it('handles unknown errors', function (done) {1028 const app = express();1029 nock.disableNetConnect();1030 app.get('/', function (req, res) {1031 res.status(200).send('OK');1032 });1033 request(app)1034 .get('/')1035 // This expect should never get called, but exposes this issue with other1036 // errors being obscured by the response assertions1037 // https://github.com/visionmedia/supertest/issues/3521038 .expect(200)1039 .end(function (err, res) {1040 err.should.be.an.instanceof(Error);1041 err.message.should.match(/Nock: Disallowed net connect/);1042 shouldIncludeStackWithThisFile(err);1043 done();1044 });1045 nock.restore();1046 });1047 it('should assert using promises', function (done) {1048 const app = express();1049 app.get('/', function (req, res) {1050 res.status(400).send({ promise: true });1051 });1052 request(app)1053 .get('/')1054 .expect(400)1055 .then((res) => {1056 res.body.promise.should.be.equal(true);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const request = require('supertest');2const app = require('../app');3const shouldIncludeStackWithThisFile = require('../test-utils').shouldIncludeStackWithThisFile;4describe('GET /', function () {5 it('should respond with a 200', function (done) {6 request(app)7 .get('/')8 .expect(200, done);9 });10 it('should respond with a 200', function (done) {11 request(app)12 .get('/')13 .expect(200, done);14 });15});16const path = require('path');17function shouldIncludeStackWithThisFile(err) {18 const stack = err.stack;19 const thisFile = path.resolve(__dirname, 'test.js');20 return stack.indexOf(thisFile) > -1;21}22module.exports = {23};24const express = require('express');25const app = express();26app.get('/', function (req, res) {27 res.send('Hello World!');28});29module.exports = app;30{31 "scripts": {32 },33 "dependencies": {34 },35 "devDependencies": {36 }37}

Full Screen

Using AI Code Generation

copy

Full Screen

1const request = require('supertest');2const app = require('../app');3const should = require('should');4const shouldIncludeStackWithThisFile = require('supertest/lib/test').shouldIncludeStackWithThisFile;5describe('Test Suite', function () {6 it('Test Case', function (done) {7 request(app)8 .get('/api/v1/users')9 .expect(200)10 .end(function (err, res) {11 if (err) {12 if (shouldIncludeStackWithThisFile(err)) {13 done(err);14 } else {15 done();16 }17 } else {18 done();19 }20 });21 });22});23const express = require('express');24const app = express();25app.get('/api/v1/users', function (req, res) {26 res.status(200).json({ message: 'OK' });27});28module.exports = app;29 at Test._assertStatus (/Users/username/Projects/username/app/node_modules/supertest/lib/test.js:268:12)30 at Test._assertFunction (/Users/username/Projects/username/app/node_modules/supertest/lib/test.js:283:11)31 at Test.assert (/Users/username/Projects/username/app/node_modules/supertest/lib/test.js:173:18)32 at assert (/Users/username/Projects/username/app/node_modules/supertest/lib/test.js:131:12)33 at Test.Request.callback (/Users/username/Projects/username/app/node_modules/superagent/lib/node/index.js:718:3)34 at parser (/Users/username/Projects/username/app/node_modules/superagent/lib/node/index.js:906:18)35 at IncomingMessage.res.on (/Users/username/Projects/username/app/node_modules/superagent/lib/node/parsers/json.js:19:7)36 at IncomingMessage.emit (events.js:198:13)37 at endReadableNT (_stream_readable.js:1145:

Full Screen

Using AI Code Generation

copy

Full Screen

1var should = require('should');2var supertest = require('supertest');3var app = require('./app');4describe('app', function() {5 it('should return a 404', function(done) {6 supertest(app)7 .get('/doesnotexist')8 .expect(404)9 .end(function(err, res) {10 if (err) {11 return done(err);12 }13 res.error.should.have.property('stack');14 res.error.shouldIncludeStackWithThisFile();15 done();16 });17 });18});19var should = require('should');20var supertest = require('supertest-as-promised');21var app = require('./app');22describe('app', function() {23 it('should return a 404', function(done) {24 supertest(app)25 .get('/doesnotexist')26 .expect(404)27 .end(function(err, res) {28 if (err) {29 return done(err);30 }31 res.error.should.have.property('stack');32 res.error.shouldIncludeStackWithThisFile();33 done();34 });35 });36});

Full Screen

Using AI Code Generation

copy

Full Screen

1app.get('/test', function(req, res){2 res.status(500).send({error: 'error'});3});4app.get('/test', function(req, res){5 res.status(500).send({error: 'error'});6});7app.get('/test', function(req, res){8 res.status(500).send({error: 'error'});9});10app.get('/test', function(req, res){11 res.status(500).send({error: 'error'});12});13app.get('/test', function(req, res){14 res.status(500).send({error: 'error'});15});16app.get('/test', function(req, res){17 res.status(500).send({error: 'error'});18});19app.get('/test', function(req, res){20 res.status(500).send({error: 'error'});21});22app.get('/test', function(req, res){23 res.status(500).send({error: 'error'});24});25app.get('/test', function(req, res){26 res.status(500).send({error: 'error'});27});

Full Screen

Using AI Code Generation

copy

Full Screen

1const shouldIncludeStackWithThisFile = require('supertest/lib/utils').shouldIncludeStackWithThisFile;2if (shouldIncludeStackWithThisFile()) {3 console.log('include stack trace');4} else {5 console.log('do not include stack trace');6}

Full Screen

Using AI Code Generation

copy

Full Screen

1supertest.shouldIncludeStackWithThisFile = function () {2 const err = new Error();3 return err.stack.includes('test.js');4};5const supertest = require('supertest');6const app = require('../app');7describe('GET /', () => {8 it('should return 200 OK', () => {9 return supertest(app)10 .get('/')11 .expect(200)12 .expect('Content-Type', /text\/html/);13 });14});15const express = require('express');16const app = express();17app.get('/', (req, res) => {18 res.status(200).send('Hello World');19});20module.exports = app;21{22 "scripts": {23 },24 "devDependencies": {25 }26}

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run supertest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful