2

I'm have some issue in my test with superagent and express.js.

it('should 200 with valid login', (done) => {
    console.log(createdUser[`${validUser.email}`]['token']);
    // JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ODAwZDllZmNiM2VkMzBhOGZmMDUyOGUiLCJmaXJzdE5hbWUiOiJoYW5zIiwibGFzdE5hbWUiOiJvdHRvIiwiZW1haWwiOiJvdHRvQGV4YW1wbGUuZGUiLCJyb2xlIjoiVXNlciIsImlhdCI6MTQ3NjQ1MDc5OSwiZXhwIjoxNDc2NDYwODc5fQ.OlO_dVMCV6bm7XSyzKLFTgb-efOeyU1TniHEcIY7AHU
    request(app)
        .get('/api/protected')
        .set('Authorization', createdUser[`${validUser.email}`]['token'])
        .expect(200)
        .end((err, res)=> {
            if (err) done(err);
            console.log(res.header);
            // assert(true, 'asdfasdf');
            // done();
        });
});

I can't access to the protected path over superagent.

When I'm accessing the path over Postman it is working and I can access the protected path with the suited jwt.

What do have to change in the code? I want to test different paths.

thanks

imalik8088
  • 1,501
  • 5
  • 21
  • 39

3 Answers3

1

Bearer keyword is missing while setting the token

.set('Authorization', `Bearer ${createdUser[`${validUser.email}`]['token']}`)

Jithin Zachariah
  • 313
  • 3
  • 13
0

There is some information missing but here are some possible answers:

  1. You do show how you created your app. If you are using a middleware like express-jwt it needs to be configured before running your test. (app.use(expressJWt...)

  2. Typically, the jwt is sent through the Authorization header using the Bearer schema. ( i.e. Authorization: Bearer eyJhbGciOiJI... )

Anthony Garant
  • 614
  • 7
  • 13
0

You can use superagent’s auth function with { type: ‘bearer’ } option:

request.auth(jwt, { type: 'bearer' })
bruceoutdoors
  • 420
  • 5
  • 8