I am writing a test case to test my API . When I try to test for any open API, it is working fine. But When I try to send Authorization Token along with my API, it is not working. Here is the code:
The way i am sending headers is:
.set("Authorization", "Bearer " + token)
Is it the correct way of sending?
I have tried to send the Authorization token in Auth. But not able to get the same. But when I tried to consume same in Postman, it is working fine.
it("Get some random Info", function(done) {
chai
.request(baseUrl)
.get("/someRandomApi")
.set("Authorization", "Bearer " + token)
.end(function(err, res) {
expect(res).to.have.status(200);
done();
});
});
I am writing a test case to test my API . When I try to test for any open API, it is working fine. But When I try to send Authorization Token along with my API, it is not working. Here is the code:
The way i am sending headers is:
.set("Authorization", "Bearer " + token)
Is it the correct way of sending?
I have tried to send the Authorization token in Auth. But not able to get the same. But when I tried to consume same in Postman, it is working fine.
it("Get some random Info", function(done) {
chai
.request(baseUrl)
.get("/someRandomApi")
.set("Authorization", "Bearer " + token)
.end(function(err, res) {
expect(res).to.have.status(200);
done();
});
});
Share
Improve this question
edited Aug 27, 2019 at 5:49
Ryan
4031 gold badge7 silver badges22 bronze badges
asked Aug 27, 2019 at 5:40
Leela VathiLeela Vathi
711 gold badge2 silver badges5 bronze badges
8
|
Show 3 more comments
4 Answers
Reset to default 14I like to set up my tests in the following way:
let baseUrl = 'http://localhost:9090'
let token = 'some_authorization_token'
First I would instantiate my variables baseUrl
and token
at the very top of the test, right after use()
part.
Next to come is the setup of the test.
it("Get some random Info", function(done) {
chai.request(baseUrl)
.get('/someRandomApi')
.set({ "Authorization": `Bearer ${token}` })
.then((res) => {
expect(res).to.have.status(200)
const body = res.body
// console.log(body) - not really needed, but I include them as a comment
done();
}).catch((err) => done(err))
});
Now, .set()
doesn't necessarily have to be like mine, works in your case as well.
You can use the auth
function to set the Authorization header.
it("Get some random Info", function(done) {
chai
.request(baseUrl)
.get("/someRandomApi")
.auth(token, { type: 'bearer' })
.end(function(err, res) {
expect(res).to.have.status(200);
done();
});
});
chai-http has auth
function to send the Authorization Bearer token.
Accroding to chai-http code on Github, token can be pass using:
.auth(accessToken, { type: 'bearer' })
The code would be like:
it("Get some random Info", function(done) {
chai.request(baseUrl)
.get('/someRandomApi')
.set(token,{ type: 'bearer' }) //token is actual token data
.then((res) => {
expect(res).to.have.status(200)
done();
}).catch((err) => done(err))
});
Try calling .get()
after you call .set()
:
it("Get some random Info", function(done) {
chai
.request(baseUrl)
.set("Authorization", "Bearer " + token) //set the header first
.get("/someRandomApi") //then get the data
.end(function(err, res) {
expect(res).to.have.status(200);
done();
});
});
baseUrl
variable – Ryan Commented Aug 27, 2019 at 6:15http://
at the beginning on the url – Ryan Commented Aug 27, 2019 at 6:24