supertest.js 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  2. var request = require('..')
  3. , https = require('https')
  4. , fs = require('fs')
  5. , path = require('path')
  6. , should = require('should')
  7. , express = require('express');
  8. var bodyParser = require('body-parser')
  9. , cookieParser = require('cookie-parser');
  10. describe('request(url)', function(){
  11. it('should be supported', function(done){
  12. var app = express();
  13. app.get('/', function(req, res){
  14. res.send('hello');
  15. });
  16. var s = app.listen(function(){
  17. var url = 'http://localhost:' + s.address().port;
  18. request(url)
  19. .get('/')
  20. .expect("hello", done);
  21. });
  22. });
  23. describe('.end(cb)', function() {
  24. it('should set `this` to the test object when calling cb', function(done) {
  25. var app = express();
  26. app.get('/', function(req, res){
  27. res.send('hello');
  28. });
  29. var s = app.listen(function(){
  30. var url = 'http://localhost:' + s.address().port;
  31. var test = request(url).get('/');
  32. test.end(function(err, res) {
  33. this.should.eql(test);
  34. done();
  35. });
  36. });
  37. });
  38. });
  39. });
  40. describe('request(app)', function(){
  41. it('should fire up the app on an ephemeral port', function(done){
  42. var app = express();
  43. app.get('/', function(req, res){
  44. res.send('hey');
  45. });
  46. request(app)
  47. .get('/')
  48. .end(function(err, res){
  49. res.status.should.equal(200);
  50. res.text.should.equal('hey');
  51. done();
  52. });
  53. });
  54. it('should work with an active server', function(done){
  55. var app = express();
  56. app.get('/', function(req, res){
  57. res.send('hey');
  58. });
  59. var server = app.listen(4000, function(){
  60. request(server)
  61. .get('/')
  62. .end(function(err, res){
  63. res.status.should.equal(200);
  64. res.text.should.equal('hey');
  65. done();
  66. });
  67. });
  68. });
  69. it('should work with remote server', function(done){
  70. var app = express();
  71. app.get('/', function(req, res){
  72. res.send('hey');
  73. });
  74. var server = app.listen(4001, function(){
  75. request('http://localhost:4001')
  76. .get('/')
  77. .end(function(err, res){
  78. res.status.should.equal(200);
  79. res.text.should.equal('hey');
  80. done();
  81. });
  82. });
  83. });
  84. it('should work with a https server', function(done){
  85. var app = express();
  86. app.get('/', function(req, res){
  87. res.send('hey');
  88. });
  89. var fixtures = path.join(__dirname, 'fixtures');
  90. var server = https.createServer({
  91. key: fs.readFileSync(path.join(fixtures, 'test_key.pem')),
  92. cert: fs.readFileSync(path.join(fixtures, 'test_cert.pem'))
  93. }, app);
  94. request(server)
  95. .get('/')
  96. .end(function(err, res){
  97. if (err) return done(err);
  98. res.status.should.equal(200);
  99. res.text.should.equal('hey');
  100. done();
  101. });
  102. });
  103. it('should work with .send() etc', function(done){
  104. var app = express();
  105. app.use(bodyParser.json());
  106. app.post('/', function(req, res){
  107. res.send(req.body.name);
  108. });
  109. request(app)
  110. .post('/')
  111. .send({ name: 'tobi' })
  112. .expect('tobi', done);
  113. });
  114. it('should work when unbuffered', function(done){
  115. var app = express();
  116. app.get('/', function(req, res){
  117. res.end('Hello');
  118. });
  119. request(app)
  120. .get('/')
  121. .expect('Hello', done);
  122. });
  123. it('should default redirects to 0', function(done){
  124. var app = express();
  125. app.get('/', function(req, res){
  126. res.redirect('/login');
  127. });
  128. request(app)
  129. .get('/')
  130. .expect(302, done);
  131. });
  132. it('should handle redirects', function(done){
  133. var app = express();
  134. app.get('/login', function (req, res){
  135. res.end('Login')
  136. });
  137. app.get('/', function(req, res){
  138. res.redirect('/login');
  139. });
  140. request(app)
  141. .get('/')
  142. .redirects(1)
  143. .end(function (err, res) {
  144. should.exist(res)
  145. res.status.should.be.equal(200)
  146. res.text.should.be.equal('Login')
  147. done()
  148. })
  149. });
  150. it('should handle socket errors', function(done) {
  151. var app = express();
  152. app.get('/', function(req, res){
  153. res.destroy();
  154. });
  155. request(app)
  156. .get('/')
  157. .end(function(err) {
  158. should.exist(err);
  159. done();
  160. });
  161. });
  162. describe('.end(fn)', function(){
  163. it('should close server', function(done){
  164. var app = express();
  165. app.get('/', function(req, res){
  166. res.send('supertest FTW!');
  167. });
  168. var test = request(app)
  169. .get('/')
  170. .end(function(){});
  171. test._server.on('close', function(){
  172. done();
  173. });
  174. });
  175. it('should wait for server to close before invoking fn', function(done){
  176. var app = express();
  177. var closed = false;
  178. app.get('/', function(req, res){
  179. res.send('supertest FTW!');
  180. });
  181. var test = request(app)
  182. .get('/')
  183. .end(function(){
  184. closed.should.be.true;
  185. done();
  186. });
  187. test._server.on('close', function(){
  188. closed = true;
  189. });
  190. });
  191. it('should support nested requests', function(done){
  192. var app = express();
  193. var test = request(app);
  194. app.get('/', function(req, res){
  195. res.send('supertest FTW!');
  196. });
  197. test
  198. .get('/')
  199. .end(function(){
  200. test
  201. .get('/')
  202. .end(function(err, res){
  203. (err === null).should.be.true;
  204. res.status.should.equal(200);
  205. res.text.should.equal('supertest FTW!');
  206. done();
  207. });
  208. });
  209. });
  210. it('should include the response in the error callback', function(done){
  211. var app = express();
  212. app.get('/', function(req, res){
  213. res.send('whatever');
  214. });
  215. request(app)
  216. .get('/')
  217. .expect(function() {
  218. throw new Error('Some error');
  219. })
  220. .end(function(err, res){
  221. should.exist(err);
  222. should.exist(res);
  223. // Duck-typing response, just in case.
  224. res.status.should.equal(200);
  225. done();
  226. });
  227. });
  228. it('should set `this` to the test object when calling the error callback', function(done) {
  229. var app = express();
  230. app.get('/', function(req, res){
  231. res.send('whatever');
  232. });
  233. var test = request(app).get('/');
  234. test.expect(function() {
  235. throw new Error('Some error');
  236. }).end(function(err, res){
  237. should.exist(err);
  238. this.should.eql(test);
  239. done();
  240. });
  241. });
  242. it('should handle an undefined Response', function (done) {
  243. var app = express();
  244. app.get('/', function(req, res){
  245. setTimeout( function () {
  246. res.end();
  247. }, 20);
  248. });
  249. var s = app.listen(function(){
  250. var url = 'http://localhost:' + s.address().port;
  251. request(url)
  252. .get('/')
  253. .timeout(1)
  254. .expect(200, function (err) {
  255. err.should.be.an.instanceof(Error);
  256. return done();
  257. });
  258. });
  259. });
  260. it('should handle error returned when server goes down', function (done) {
  261. var app = express();
  262. app.get('/', function(req, res){
  263. res.end();
  264. });
  265. var s = app.listen(function(){
  266. var url = 'http://localhost:' + s.address().port;
  267. s.close();
  268. request(url)
  269. .get('/')
  270. .expect(200, function (err) {
  271. err.should.be.an.instanceof(Error);
  272. return done();
  273. });
  274. });
  275. });
  276. });
  277. describe('.expect(status[, fn])', function(){
  278. it('should assert the response status', function(done){
  279. var app = express();
  280. app.get('/', function(req, res){
  281. res.send('hey');
  282. });
  283. request(app)
  284. .get('/')
  285. .expect(404)
  286. .end(function(err, res){
  287. err.message.should.equal('expected 404 "Not Found", got 200 "OK"');
  288. done();
  289. });
  290. });
  291. });
  292. describe('.expect(status)', function () {
  293. it('should assert only status', function (done) {
  294. var app = express();
  295. app.get('/', function (req, res) {
  296. res.send('hey');
  297. });
  298. request(app)
  299. .get('/')
  300. .expect(200)
  301. .end(done)
  302. });
  303. });
  304. describe('.expect(status, body[, fn])', function(){
  305. it('should assert the response body and status', function(done){
  306. var app = express();
  307. app.get('/', function(req, res){
  308. res.send('foo');
  309. });
  310. request(app)
  311. .get('/')
  312. .expect(200, 'foo', done)
  313. });
  314. describe("when the body argument is an empty string", function() {
  315. it("should not quietly pass on failure", function(done) {
  316. var app = express();
  317. app.get('/', function(req, res){
  318. res.send('foo');
  319. });
  320. request(app)
  321. .get('/')
  322. .expect(200, '')
  323. .end(function(err, res){
  324. err.message.should.equal('expected \'\' response body, got \'foo\'');
  325. done();
  326. });
  327. });
  328. });
  329. });
  330. describe('.expect(body[, fn])', function(){
  331. it('should assert the response body', function(done){
  332. var app = express();
  333. app.set('json spaces', 0);
  334. app.get('/', function(req, res){
  335. res.send({ foo: 'bar' });
  336. });
  337. request(app)
  338. .get('/')
  339. .expect('hey')
  340. .end(function(err, res){
  341. err.message.should.equal('expected \'hey\' response body, got \'{"foo":"bar"}\'');
  342. done();
  343. });
  344. });
  345. it('should assert the status before the body', function (done) {
  346. var app = express();
  347. app.set('json spaces', 0);
  348. app.get('/', function(req, res){
  349. res.status(500).send({ message: 'something went wrong' });
  350. });
  351. request(app)
  352. .get('/')
  353. .expect(200)
  354. .expect('hey')
  355. .end(function(err, res){
  356. err.message.should.equal('expected 200 \"OK"\, got 500 \"Internal Server Error\"');
  357. done();
  358. });
  359. });
  360. it('should assert the response text', function(done){
  361. var 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('{"foo":"bar"}', done);
  369. });
  370. it('should assert the parsed response body', function(done){
  371. var app = express();
  372. app.set('json spaces', 0);
  373. app.get('/', function(req, res){
  374. res.send({ foo: 'bar' });
  375. });
  376. request(app)
  377. .get('/')
  378. .expect({ foo: 'baz' })
  379. .end(function(err, res){
  380. err.message.should.equal('expected { foo: \'baz\' } response body, got { foo: \'bar\' }');
  381. request(app)
  382. .get('/')
  383. .expect({ foo: 'bar' })
  384. .end(done);
  385. });
  386. });
  387. it('should support regular expressions', function(done){
  388. var app = express();
  389. app.get('/', function(req, res){
  390. res.send('foobar');
  391. });
  392. request(app)
  393. .get('/')
  394. .expect(/^bar/)
  395. .end(function(err, res){
  396. err.message.should.equal('expected body \'foobar\' to match /^bar/');
  397. done();
  398. });
  399. });
  400. it('should assert response body multiple times', function(done){
  401. var app = express();
  402. app.get('/', function(req, res){
  403. res.send('hey tj');
  404. });
  405. request(app)
  406. .get('/')
  407. .expect(/tj/)
  408. .expect('hey')
  409. .expect('hey tj')
  410. .end(function (err, res) {
  411. err.message.should.equal("expected 'hey' response body, got 'hey tj'");
  412. done();
  413. });
  414. });
  415. it('should assert response body multiple times with no exception', function(done){
  416. var app = express();
  417. app.get('/', function(req, res){
  418. res.send('hey tj');
  419. });
  420. request(app)
  421. .get('/')
  422. .expect(/tj/)
  423. .expect(/^hey/)
  424. .expect('hey tj', done);
  425. });
  426. });
  427. describe('.expect(field, value[, fn])', function(){
  428. it('should assert the header field presence', function(done){
  429. var app = express();
  430. app.get('/', function(req, res){
  431. res.send({ foo: 'bar' });
  432. });
  433. request(app)
  434. .get('/')
  435. .expect('Content-Foo', 'bar')
  436. .end(function(err, res){
  437. err.message.should.equal('expected "Content-Foo" header field');
  438. done();
  439. });
  440. });
  441. it('should assert the header field value', function(done){
  442. var app = express();
  443. app.get('/', function(req, res){
  444. res.send({ foo: 'bar' });
  445. });
  446. request(app)
  447. .get('/')
  448. .expect('Content-Type', 'text/html')
  449. .end(function(err, res){
  450. err.message.should.equal('expected "Content-Type" of "text/html", got "application/json; charset=utf-8"');
  451. done();
  452. });
  453. });
  454. it('should assert multiple fields', function(done){
  455. var app = express();
  456. app.get('/', function(req, res){
  457. res.send('hey');
  458. });
  459. request(app)
  460. .get('/')
  461. .expect('Content-Type', 'text/html; charset=utf-8')
  462. .expect('Content-Length', '3')
  463. .end(done);
  464. });
  465. it('should support regular expressions', function(done){
  466. var app = express();
  467. app.get('/', function(req, res){
  468. res.send('hey');
  469. });
  470. request(app)
  471. .get('/')
  472. .expect('Content-Type', /^application/)
  473. .end(function(err){
  474. err.message.should.equal('expected "Content-Type" matching /^application/, got "text/html; charset=utf-8"');
  475. done();
  476. });
  477. });
  478. it('should support numbers', function(done){
  479. var app = express();
  480. app.get('/', function(req, res){
  481. res.send('hey');
  482. });
  483. request(app)
  484. .get('/')
  485. .expect('Content-Length', 4)
  486. .end(function(err){
  487. err.message.should.equal('expected "Content-Length" of "4", got "3"');
  488. done();
  489. });
  490. });
  491. describe('handling arbitrary expect functions', function(){
  492. var app, get;
  493. before(function(){
  494. app = express();
  495. app.get('/', function(req, res){
  496. res.send('hey');
  497. });
  498. });
  499. beforeEach(function(){
  500. get = request(app).get('/');
  501. });
  502. it('reports errors',function(done) {
  503. get
  504. .expect(function(res) {
  505. throw new Error("failed")
  506. })
  507. .end(function(err) {
  508. err.message.should.equal('failed');
  509. done()
  510. });
  511. });
  512. it('ensures truthy non-errors returned from asserts are not promoted to errors',function(done){
  513. get
  514. .expect(function(res) {
  515. return "some descriptive error";
  516. })
  517. .end(function(err) {
  518. should.not.exist(err);
  519. done()
  520. });
  521. });
  522. it('ensures truthy errors returned from asserts are throw to end',function(done){
  523. get
  524. .expect(function(res) {
  525. return new Error("some descriptive error");
  526. })
  527. .end(function(err) {
  528. err.message.should.equal("some descriptive error");
  529. (err instanceof Error).should.be.true;
  530. done();
  531. });
  532. });
  533. it("doesn't create false negatives", function(done){
  534. get
  535. .expect(function(res) {})
  536. .end(done);
  537. });
  538. it("handles multiple asserts", function(done){
  539. var calls = [];
  540. get
  541. .expect(function(res) { calls[0] = 1 })
  542. .expect(function(res) { calls[1] = 1 })
  543. .expect(function(res) { calls[2] = 1 })
  544. .end(function() {
  545. var callCount = [0,1,2].reduce(function(count,i) {
  546. return count + calls[i]
  547. },0);
  548. callCount.should.equal(3,"didn't see all assertions run");
  549. done();
  550. });
  551. });
  552. it("plays well with normal assertions - no false positives", function(done){
  553. get
  554. .expect(function(res) {})
  555. .expect('Content-Type', /json/)
  556. .end(function(err) {
  557. err.message.should.match(/Content-Type/);
  558. done();
  559. });
  560. });
  561. it("plays well with normal assertions - no false negatives", function(done){
  562. get
  563. .expect(function(res) {})
  564. .expect('Content-Type', /html/)
  565. .expect(function(res) {})
  566. .expect('Content-Type', /text/)
  567. .end(done)
  568. });
  569. });
  570. describe('handling multiple assertions per field', function(){
  571. it('should work', function(done){
  572. var app = express();
  573. app.get('/', function(req, res){
  574. res.send('hey');
  575. });
  576. request(app)
  577. .get('/')
  578. .expect('Content-Type', /text/)
  579. .expect('Content-Type', /html/)
  580. .end(done);
  581. });
  582. it('should return an error if the first one fails', function(done){
  583. var app = express();
  584. app.get('/', function(req, res){
  585. res.send('hey');
  586. });
  587. request(app)
  588. .get('/')
  589. .expect('Content-Type', /bloop/)
  590. .expect('Content-Type', /html/)
  591. .end(function(err){
  592. err.message.should.equal('expected "Content-Type" matching /bloop/, got "text/html; charset=utf-8"');
  593. done();
  594. });
  595. });
  596. it('should return an error if a middle one fails', function(done){
  597. var app = express();
  598. app.get('/', function(req, res){
  599. res.send('hey');
  600. });
  601. request(app)
  602. .get('/')
  603. .expect('Content-Type', /text/)
  604. .expect('Content-Type', /bloop/)
  605. .expect('Content-Type', /html/)
  606. .end(function(err){
  607. err.message.should.equal('expected "Content-Type" matching /bloop/, got "text/html; charset=utf-8"');
  608. done();
  609. });
  610. });
  611. it('should return an error if the last one fails', function(done){
  612. var app = express();
  613. app.get('/', function(req, res){
  614. res.send('hey');
  615. });
  616. request(app)
  617. .get('/')
  618. .expect('Content-Type', /text/)
  619. .expect('Content-Type', /html/)
  620. .expect('Content-Type', /bloop/)
  621. .end(function(err){
  622. err.message.should.equal('expected "Content-Type" matching /bloop/, got "text/html; charset=utf-8"');
  623. done();
  624. });
  625. });
  626. });
  627. });
  628. });
  629. describe('request.agent(app)', function(){
  630. var app = express();
  631. app.use(cookieParser());
  632. app.get('/', function(req, res){
  633. res.cookie('cookie', 'hey');
  634. res.send();
  635. });
  636. app.get('/return', function(req, res){
  637. if (req.cookies.cookie) res.send(req.cookies.cookie);
  638. else res.send(':(')
  639. });
  640. var agent = request.agent(app);
  641. it('should save cookies', function(done){
  642. agent
  643. .get('/')
  644. .expect('set-cookie', 'cookie=hey; Path=/', done);
  645. });
  646. it('should send cookies', function(done){
  647. agent
  648. .get('/return')
  649. .expect('hey', done);
  650. });
  651. });
  652. describe(".<http verb> works as expected", function(){
  653. it(".delete should work", function (done){
  654. var app = express();
  655. app.delete('/', function(req, res){
  656. res.sendStatus(200);
  657. });
  658. request(app)
  659. .delete('/')
  660. .expect(200, done);
  661. });
  662. it(".del should work", function (done){
  663. var app = express();
  664. app.delete('/', function(req, res){
  665. res.sendStatus(200);
  666. });
  667. request(app)
  668. .del('/')
  669. .expect(200, done);
  670. });
  671. it(".get should work", function (done){
  672. var app = express();
  673. app.get('/', function(req, res){
  674. res.sendStatus(200);
  675. });
  676. request(app)
  677. .get('/')
  678. .expect(200, done);
  679. });
  680. it(".post should work", function (done){
  681. var app = express();
  682. app.post('/', function(req, res){
  683. res.sendStatus(200);
  684. });
  685. request(app)
  686. .post('/')
  687. .expect(200, done);
  688. });
  689. it(".put should work", function (done){
  690. var app = express();
  691. app.put('/', function(req, res){
  692. res.sendStatus(200);
  693. });
  694. request(app)
  695. .put('/')
  696. .expect(200, done);
  697. });
  698. });
  699. describe('assert ordering by call order', function() {
  700. it('should assert the body before status', function(done) {
  701. var app = express();
  702. app.set('json spaces', 0);
  703. app.get('/', function(req, res) {
  704. res.status(500).json({message: 'something went wrong'});
  705. });
  706. request(app)
  707. .get('/')
  708. .expect('hey')
  709. .expect(200)
  710. .end(function(err, res) {
  711. err.message.should.equal('expected \'hey\' response body, got \'{"message":"something went wrong"}\'');
  712. done();
  713. });
  714. });
  715. it('should assert the status before body', function(done) {
  716. var app = express();
  717. app.set('json spaces', 0);
  718. app.get('/', function(req, res) {
  719. res.status(500).json({message: 'something went wrong'});
  720. });
  721. request(app)
  722. .get('/')
  723. .expect(200)
  724. .expect('hey')
  725. .end(function(err, res) {
  726. err.message.should.equal('expected 200 "OK", got 500 "Internal Server Error"');
  727. done();
  728. });
  729. });
  730. it('should assert the fields before body and status', function(done) {
  731. var app = express();
  732. app.set('json spaces', 0);
  733. app.get('/', function(req, res) {
  734. res.status(200).json({hello: 'world'});
  735. });
  736. request(app)
  737. .get('/')
  738. .expect('content-type', /html/)
  739. .expect('hello')
  740. .end(function(err, res) {
  741. err.message.should.equal('expected "content-type" matching /html/, got "application/json; charset=utf-8"');
  742. done();
  743. });
  744. });
  745. it('should call the expect function in order', function(done) {
  746. var app = express();
  747. app.get('/', function(req, res) {
  748. res.status(200).json({});
  749. });
  750. request(app)
  751. .get('/')
  752. .expect(function(res) {
  753. res.body.first = 1;
  754. })
  755. .expect(function(res) {
  756. (res.body.first === 1).should.be.true;
  757. res.body.second = 2;
  758. })
  759. .end(function(err, res) {
  760. if (err) return done(err);
  761. (res.body.first === 1).should.be.true;
  762. (res.body.second === 2).should.be.true;
  763. done();
  764. });
  765. });
  766. it('should call expect(fn) and expect(status, fn) in order', function(done) {
  767. var app = express();
  768. app.get('/', function(req, res) {
  769. res.status(200).json({});
  770. });
  771. request(app)
  772. .get('/')
  773. .expect(function(res) {
  774. res.body.first = 1;
  775. })
  776. .expect(200, function(err, res) {
  777. (err === null).should.be.true;
  778. (res.body.first === 1).should.be.true;
  779. done();
  780. });
  781. });
  782. it('should call expect(fn) and expect(header,value) in order', function(done) {
  783. var app = express();
  784. app.get('/', function(req, res) {
  785. res
  786. .set('X-Some-Header', 'Some value')
  787. .send();
  788. });
  789. request(app)
  790. .get('/')
  791. .expect('X-Some-Header', 'Some value')
  792. .expect(function(res) {
  793. res.headers['x-some-header'] = '';
  794. })
  795. .expect('X-Some-Header', '')
  796. .end(done);
  797. });
  798. it('should call expect(fn) and expect(body) in order', function(done) {
  799. var app = express();
  800. app.get('/', function(req, res) {
  801. res.json({somebody: 'some body value'});
  802. });
  803. request(app)
  804. .get('/')
  805. .expect(/some body value/)
  806. .expect(function(res) {
  807. res.body.somebody = 'nobody';
  808. })
  809. .expect(/some body value/) // res.text should not be modified.
  810. .expect({somebody: 'nobody'})
  811. .expect(function(res) {
  812. res.text = 'gone';
  813. })
  814. .expect('gone')
  815. .expect(/gone/)
  816. .expect({somebody: 'nobody'}) // res.body should not be modified
  817. .expect('gone', done);
  818. });
  819. });
  820. describe("request.get(url).query(vals) works as expected", function(){
  821. it("normal single query string value works", function(done) {
  822. var app = express();
  823. app.get('/', function(req, res){
  824. res.status(200).send(req.query.val);
  825. });
  826. request(app)
  827. .get('/')
  828. .query({val: "Test1"})
  829. .expect(200, function(err, res) {
  830. res.text.should.be.equal('Test1');
  831. done();
  832. });
  833. });
  834. it("array query string value works", function(done) {
  835. var app = express();
  836. app.get('/', function(req, res){
  837. res.status(200).send(Array.isArray(req.query.val));
  838. });
  839. request(app)
  840. .get('/')
  841. .query({'val[]': ["Test1", "Test2"]})
  842. .expect(200, function(err, res) {
  843. res.req.path.should.be.equal('/?val%5B%5D=Test1&val%5B%5D=Test2');
  844. res.text.should.be.equal('true');
  845. done();
  846. });
  847. });
  848. it("array query string value work even with single value", function(done) {
  849. var app = express();
  850. app.get('/', function(req, res){
  851. res.status(200).send(Array.isArray(req.query.val));
  852. });
  853. request(app)
  854. .get('/')
  855. .query({'val[]': ["Test1"]})
  856. .expect(200, function(err, res) {
  857. res.req.path.should.be.equal('/?val%5B%5D=Test1');
  858. res.text.should.be.equal('true');
  859. done();
  860. });
  861. });
  862. it("object query string value works", function(done) {
  863. var app = express();
  864. app.get('/', function(req, res){
  865. res.status(200).send(req.query.val.test);
  866. });
  867. request(app)
  868. .get('/')
  869. .query({val: { test: 'Test1' } })
  870. .expect(200, function(err, res) {
  871. res.text.should.be.equal('Test1');
  872. done();
  873. });
  874. });
  875. });