2014-11-18 7 views
3

모카 요청 사이에 내 세션을 유지해야합니다.SailsJs :: 모카와 세션 유지

로그인 후, 나는 명시 세션 객체에 사용자 ID에 저장 :

브라우저에
req.session.user = user.id ; 

는 세션 (우편 배달부 테스트) 필요한 문제없이 유지됩니다.

하지만 내 REST API를 외부 앱에 연결할 수있게해야하며 API에서 각 요청에 대해 인증하지 않아도됩니다.

mocha 또는 API의 클라이언트 응용 프로그램을 통해 두 가지 요청간에 세션을 유지할 수있는 방법이 있습니까?

미리 감사드립니다.

영어는 나의 어머니가 아닌, 내가 원했던 것처럼 분명하지 않을 수도 있습니다. 그래서 당신이 나를 도울 필요가있는 정보를 제공 할 수 있습니다. 알베르토에

UPDATE

덕분에, 나는 Supertest와 모카 살아 내 세션을 유지하는 방법을 알아 냈어. 에이전트는 세션이 파괴되거나 로그 아웃이 요청 될 때까지 세션을 유지합니다. don '이되어야하는 것은 동일한 에이전트를 사용하여 로그인하고 API를 요청하는 것입니다. 내가 무슨 짓을

은 다음과 같습니다

var request = require('supertest'), 
    should = require('chai').should(); 

describe('ImageController', function() { 
    var agent = request.agent('http://localhost:1337') ; 

    before(function(done){ 
     agent 
     .post('/auth/local') 
     .send({identifier: 'email', password: 'password'}) 
     .end(function(err, res) { 
      if (err) return done(err); 

      done(); 
     }); 
    }) 

    after(function(done){ 
     agent 
     .get('/logout') 
     .end(function(err, res) { 
      if (err) return done(err); 

      done(); 
     }); 
    }) 
    describe('POST /image', function(){ 
    it('should return 201 for image creation after login', function (done) { 
     agent 
     .post('/image') 
     .send({name: 'test.png'}) 
     .end(function (err, res) { 
      if (err) return done(err); 

      res.status.should.be.equal(201); 
      done(); 
     }); 
    }); 
    }); 
}); 
+0

에 오신 것을 환영합니다 슈퍼 테스트 예와 https://github.com/tj/supertest#example

Sails.js 예 :

는 supertest 문서에서 하나의 예를 가지고! [해당 제목에 "태그에"태그를 포함해야합니까? "] (http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-the-titles)를 참조하십시오. 여기서 컨센서스는 다음과 같습니다. "아니, 그들은해서는 안된다". –

+0

환영합니다! 제목에 태그를 넣은 부분을 설명해 주시겠습니까? – Aston

답변

3

사용 supertest 에이전트가 쿠키를 저장할 수있는 방법이 있습니다. https://github.com/albertosouza/sails-test-example

테스트 파일의 예 snipplet : StackOverflow의에

var request = require('supertest'); 
var assert = require('assert'); 
var authenticated; 

describe('Example test', function() { 
    // use efore all to create custom stub data 
    before(function(done) { 
    // use supertest.agent for store cookies ... 
    // logged in agent 

    // after authenticated requests 
    //login and save one agent with your session cookies. Ex: 
    authenticated = request.agent(sails.hooks.http.app); 
    authenticated.post('/auth/login') 
    .send({ 
     email: user.email, 
     password: user.password 
    }) 
    .end(function(err) { 
     done(err); 
    }); 
    }); 

    describe('authenticated requests', function(){ 
    it ('should access one protected route', function(done){ 
     // use the authenticated agent to do authenticated requests 
     authenticated.get('/protected') 
     .expect(200) 
     .end(function(err, res) { 
     if (err) return done(err); 

     console.log('response:', res.text); 

     done(); 
     }); 
    }); 
    }); 
}); 
+0

답변 해 주셔서 감사합니다. 나는 그것을 가능한 한 빨리 검사 할 것이다. – Aston

+0

고맙습니다. – Aston

+0

Github 링크가 끊어졌습니다. 답변에 관련 스 니펫을 붙여 넣을 수 있습니까? –