chai-http

Set accepted CA list and ignore SSL errors with chai-http

核能气质少年 提交于 2021-01-02 08:22:37
问题 I'm trying to write unit tests for my node code with chai/chai-http. Everything was working fine until I switched my server to an HTTPS server, but because my certificate is signed by an internal company root and the common name of the certificate I'm using doesn't match localhost, chai is throwing an error on my request. I'd like to do the following: Ignore SSL errors related to domain name verification. Set the list of CAs to check against. If this cannot be done, I'd be fine with just

How to send array in JSON object for ChaiHttp Unit Testing?

不问归期 提交于 2020-12-15 05:20:33
问题 I have an API in node.js, where I can send payloads for multiple DeviceIds, to update their settings. For instance, a sample payload that I would send is: {"DeviceId":["1","2","3"],"Settings":[{"Key":"OnSwitch","Value":"true"}]} After sending it, I would say that the DeviceId 1,2,3 all will have updated their settings. This is working correctly and I've tested it locally in Postman. I now want to write a unit test to check the behaviour. My unit test is the following: context('POST With

How to stub express middleware using sinon in typescript?

强颜欢笑 提交于 2020-07-10 10:27:35
问题 I'm trying to write an integration test for my express router using typescript, mocha, sinon and chai-http. This router uses a custom middleware that I wrote which checks for JWT in the header. Ideally, I want to stub my authMiddleware so that I can control its behaviour without actually providing valid/invalid JWT for every test case. When I try to stub authMiddleware in my tests, I realised that express app uses the actual implementation of authMiddleware rather than mocked one. I've tried

chai-http/superagent : set Content-Type of mulipart form field

大憨熊 提交于 2020-02-04 03:50:54
问题 Uploading a file in an integration test as follows: chai.request(server.instance) .post('/profile/photo/0') .set('Access-Token', accessToken) .set('API-Key', testConfig.apiKey) .set('Content-Type', 'image/png') .field({contentId: 'foobar'}) .attach('file', fs.readFileSync(__dirname + '/file.png'), 'file') .end((err, res) => { console.log(JSON.stringify(res.body)) res.should.have.status(200) done() }) The content type of the multipart file is: application/octet-stream and I need it to be image

Stubbing route callback render route status not found

♀尐吖头ヾ 提交于 2020-01-05 04:19:33
问题 I have a route app.get("/test", myControllers.controllerTest) The controllerTest returns current time as result to query my route test file describe.("GET /test", () => { it("should return correct response", done => { sinon.stub(myControllers, "controllerTest").yields(null, {time:'fake-time'}); app = require("../../server"); chai .request(app) .get("/test") .then(res => { expect(res.status).to.equal(200); expect(myControllers.controllerTest).to.have.been.calledOnce; done(); }) .catch(err=>{

Chai doesn't recognize content-type “application/javascript”

人盡茶涼 提交于 2019-12-11 05:00:28
问题 No matter what my server actually returns, Chai always gives me res.body={} if the content-type is "application/javascript". Here is my server: const http = require('http'); const server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "application/javascript"}); response.end('console.log("test");'); }); module.exports = server; server.listen(process.env.PORT || 8000); console.log("Server running at http://localhost:8000/"); It outputs console.log(

How to correctly close express server between tests using mocha and chai

非 Y 不嫁゛ 提交于 2019-12-08 03:11:37
问题 I am looking for a correct way to completely reset my express server between tests! It seems that this is not just a problem for me, many other users have asked the same question, and many blog posts had been written on the argument. The proposed solutions are not working nor satisfactory for me. Here there is another similar question and an article that well describes the problem and suggests some solutions: Stack overflow Closing an express server after running jasmine specs Blog: https:/

How to correctly close express server between tests using mocha and chai

限于喜欢 提交于 2019-12-07 18:42:20
I am looking for a correct way to completely reset my express server between tests! It seems that this is not just a problem for me, many other users have asked the same question, and many blog posts had been written on the argument. The proposed solutions are not working nor satisfactory for me. Here there is another similar question and an article that well describes the problem and suggests some solutions: Stack overflow Closing an express server after running jasmine specs Blog: https://glebbahmutov.com/blog/how-to-correctly-unit-test-express-server/ Here a package is specifically created