I am using Supertest with Mocha to test an API developed with Node JS.
我正在使用Supertest和Mocha来测试用Node JS开发的API。
And I want to do a lót of different tests on the API. With almost all of them I have to set Authorization and Content-Type headers again (because the API requires them for this test).
我想对API进行不同的测试。几乎所有这些都必须再次设置Authorization和Content-Type标头(因为API要求它们进行此测试)。
it('Creation without an email address should fail and return error code 50040', function(done) {
request
.post('/mpl/entities')
.set('Authorization', 'Token 1234567890') //set header for this test
.set('Content-Type', 'application/json') //set header for this test
.send({
firstname: "test"
})
.expect('Content-Type', /json/)
.expect(500)
.expect(anErrorCode('50040'))
.end(done);
});
it('Creation with a duplicate email address should fail and return error code 50086', function(done) {
request
.post('/mpl/entities')
.set('Authorization', 'Token 1234567890') //<-- again
.set('Content-Type', 'application/json') //<-- again, I'm getting tired
.send({
email: "a@b.nl"
})
.expect('Content-Type', /json/)
.expect(500)
.expect(anErrorCode('50086'))
.end(done);
});
Can I create an alternative request with those headers set by default?
我可以使用默认设置的标头创建替代请求吗?
3 个解决方案
#1
28
If i remember correctly in superagent one can pass a hash to set
如果我在superagent中记得正确,可以传递哈希来设置
.set({key:value,key2:value2})
let me know if it doesnt work with supertest.
让我知道它是否与supertest不起作用。
#2
8
You could use a common routine to build your "default" headers as an object and pass them to the request:
您可以使用公共例程将“默认”标头构建为对象,并将它们传递给请求:
//# file:config.js
var config = {
authorization: { "Authorization":"authvalue" }
}
// Content-Type left out because supertest will use Content-Type json when you use the appropriate method
module.exports = config;
And now in your test.js:
现在在你的test.js中:
//# file:test.js
var request = require("supertest");
var config = require("./config");
request = request(config.baseUrl)
var commonHeaders = { "authorization":"TokenValueASDF" };
describe("testing", function() {
it.should('present authorization header to server', function(done) {
request.get('/someurl')
.set(commonHeaders)
.set({"X-TestSpecificHeader":"Value"})
.expect(200,done) //if not authorized you'd get 401
})
})
Also, if you need to have your app get that token value at runtime (most likely) see this article for using a requested token value that is generated for the tests: https://jaketrent.com/post/authenticated-supertest-tests/
此外,如果您需要让您的应用程序在运行时获取该令牌值(最有可能),请参阅本文以使用为测试生成的请求令牌值:https://jaketrent.com/post/authenticated-supertest-tests /
#3
2
You can use the library superagent-defaults
as follows:
您可以使用库superagent-defaults,如下所示:
installation
npm install --save-dev supertest superagent-defaults
usage
var defaults = require('superagent-defaults');
var supertest = require('supertest');
var request = defaults(supertest(app)); // or url
// set the default headers
request.set(commonHeaders);
// use as usually
version
- supertest v3.0.0
- supertest v3.0.0
- superagent-defaults v0.1.14
- superagent-defaults v0.1.14
#1
28
If i remember correctly in superagent one can pass a hash to set
如果我在superagent中记得正确,可以传递哈希来设置
.set({key:value,key2:value2})
let me know if it doesnt work with supertest.
让我知道它是否与supertest不起作用。
#2
8
You could use a common routine to build your "default" headers as an object and pass them to the request:
您可以使用公共例程将“默认”标头构建为对象,并将它们传递给请求:
//# file:config.js
var config = {
authorization: { "Authorization":"authvalue" }
}
// Content-Type left out because supertest will use Content-Type json when you use the appropriate method
module.exports = config;
And now in your test.js:
现在在你的test.js中:
//# file:test.js
var request = require("supertest");
var config = require("./config");
request = request(config.baseUrl)
var commonHeaders = { "authorization":"TokenValueASDF" };
describe("testing", function() {
it.should('present authorization header to server', function(done) {
request.get('/someurl')
.set(commonHeaders)
.set({"X-TestSpecificHeader":"Value"})
.expect(200,done) //if not authorized you'd get 401
})
})
Also, if you need to have your app get that token value at runtime (most likely) see this article for using a requested token value that is generated for the tests: https://jaketrent.com/post/authenticated-supertest-tests/
此外,如果您需要让您的应用程序在运行时获取该令牌值(最有可能),请参阅本文以使用为测试生成的请求令牌值:https://jaketrent.com/post/authenticated-supertest-tests /
#3
2
You can use the library superagent-defaults
as follows:
您可以使用库superagent-defaults,如下所示:
installation
npm install --save-dev supertest superagent-defaults
usage
var defaults = require('superagent-defaults');
var supertest = require('supertest');
var request = defaults(supertest(app)); // or url
// set the default headers
request.set(commonHeaders);
// use as usually
version
- supertest v3.0.0
- supertest v3.0.0
- superagent-defaults v0.1.14
- superagent-defaults v0.1.14