Skip to content

Testing using Nock #9

Description

@sparkyfen

I'm trying to test my code I've written using supertest and nock but I'm getting a state not matching error.

My package.json dep section:

{
  "oauthio": "^0.3.1",
  "express": "~4.0.0",
  "express-session": "~1.0.2"
}

My package.json dev section:

{
  "nock": "^0.48.2",
  "querystring": "^0.2.0",
  "should": "~3.3.1",
  "supertest": "~0.11.0"
}

I've set up some nock listeners on two endpoints.

My server-side code is:

exports.index = function(req, res) {
  // .....
  OAuth.auth('facebook', req.session, {
      code: code
  }).then(function (reqObj) {
    return reqObj.get('/me');
  }).then(function (info) {
    console.log(info);
    // ......
  });
};

My test:

describe('My Test', function() {
  beforeEach(function (done) {
    // .....
    nock('https://oauth.io')
    .post('/auth/access_token', {
      code: 'someCode',
      key: config.oauth.appKey,
      secret: config.oauth.appSecret
    })
    .reply(200, {
      access_token: 'result_access_token',
      expires_in: 'someday',
      request: {},
      state: 'unique_id',
      provider: 'facebook'
    });
    nock('https://oauth.io')
    .matchHeader('oauthio', qs.stringify({
      k: config.oauth.appKey,
      access_token: 'result_access_token'
    }))
    .get('/request/facebook/me')
    .reply(200, {
      email: 'mockUser@gmail.com',
      first_name: 'Foo',
      last_name: 'Bar'
    });
    done();
  });
  afterEach(function (done) {
    // .....
  });
});

it('should respond with a successful login', function(done) {
    request(app)
      .post('/api/user/login/facebook')
      .set('cookie', cookie)
      .send({code: 'someCode'})
      .expect(200)
      .expect('Content-Type', /json/)
      .end(function(err, res) {
        if (err) return done(err);
        res.body.should.be.instanceof(Object);
        done();
      });
  });

However, my response on the test is:

[Error: State is not matching]

Any thoughts on why I'm getting this error?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions