supertest

supertest test express middleware

谁都会走 提交于 2019-12-01 16:18:16
问题 found the following hint on how to test middleware in express: https://github.com/visionmedia/express/blob/master/test/req.xhr.js I was wondering why my tests were always passing. Until I noticed that when i copied the test from express they behaved the same. I tried screwing them up but they keep passing: https://github.com/visionmedia/express/blob/master/test/req.xhr.js What am I missing here? it('should return true when X-Requested-With is xmlhttprequest', function(done){ var app = express

How to test express form post with CSRF?

点点圈 提交于 2019-11-30 21:13:03
I'm trying to write a test for my server side form validation but I keep getting a Forbidden error. It seems that this needs to be a 2 step process. Step 1, acquire the CSRF value from the form. Step 2, use the CSRF value to post to the form handler. However no matter how I try to post I get a forbidden error. --full test: https://github.com/socketwiz/swblog/blob/master/test/contact.js#L57-L100 I've tried changing the following line thusly: https://github.com/socketwiz/swblog/blob/master/test/contact.js#L85 .send({name: 'foo', 'X-CSRF-Token': token}) .set('X-CSRF-Token', token) .set('Cookie',

NodeJS HTTPS API testing with mocha and super test -“DEPTH_ZERO_SELF_SIGNED_CERT”

ⅰ亾dé卋堺 提交于 2019-11-30 12:06:36
I need to test an API served via HTTPS with mocha and super test (the certificate are not expired) This is a gist of the server : ... var app = express(); var _options = { key: fs.readFileSync('my-key.pem');, cert: fs.readFileSync('my-cert.pem') }; // Start HTTPS server https.createServer(_options, app).listen(app.get('port'), app.get('ip'), function () { // ok or not logs }); and this is the route to be tested app.get('/hello',function (req, res) { res.json(200); }); I'm trying to test with this code in test/test.js var supertest = require('supertest'), api = supertest('https://localhost:3000

How to test express form post with CSRF?

不问归期 提交于 2019-11-28 01:34:14
问题 I'm trying to write a test for my server side form validation but I keep getting a Forbidden error. It seems that this needs to be a 2 step process. Step 1, acquire the CSRF value from the form. Step 2, use the CSRF value to post to the form handler. However no matter how I try to post I get a forbidden error. --full test: https://github.com/socketwiz/swblog/blob/master/test/contact.js#L57-L100 I've tried changing the following line thusly: https://github.com/socketwiz/swblog/blob/master/test

How to authenticate Supertest requests with Passport?

亡梦爱人 提交于 2019-11-27 06:17:59
I'm using Passport.js for authentication (local strategy) and testing with Mocha and Supertest. How can I create a session and make authenticated requests with Supertest? You should use superagent for that. It is lower level module and used by supertest . Take a look at the section Persisting an agent : var request = require('superagent'); var user1 = request.agent(); user1 .post('http://localhost:4000/signin') .send({ user: 'hunter@hunterloftis.com', password: 'password' }) .end(function(err, res) { // user1 will manage its own cookies // res.redirects contains an Array of redirects }); Now

How to authenticate Supertest requests with Passport?

怎甘沉沦 提交于 2019-11-26 17:35:06
问题 I'm using Passport.js for authentication (local strategy) and testing with Mocha and Supertest. How can I create a session and make authenticated requests with Supertest? 回答1: You should use superagent for that. It is lower level module and used by supertest . Take a look at the section Persisting an agent: var request = require('superagent'); var user1 = request.agent(); user1 .post('http://localhost:4000/signin') .send({ user: 'hunter@hunterloftis.com', password: 'password' }) .end(function