【问题标题】:ECONNREFUSED 127.0.0.1:3000 at TCPConnectWrap.afterConnect [as oncomplete] errno: 'ECONNREFUSED',ECONNREFUSED 127.0.0.1:3000 在 TCPConnectWrap.afterConnect [as oncomplete] 错误:'ECONNREFUSED',
【发布时间】:2020-02-09 09:52:52
【问题描述】:

我正在模拟(facebook/google)策略上运行一些测试,但是当我一个接一个地运行这些测试时,我遇到的问题是它正在运行,但是当我同时运行它们时,它们在第一次测试后没有工作,连接申请立即失败。

我尝试以不同的描述运行,但没有任何改变我使用 nodejs 和 @passport-next/passport-mocked 作为依赖项来帮助我模拟这些策略。

  import chai from 'chai';
import chaiHttp from 'chai-http';
import passport from 'passport';
import '../config/passport';
import { describe, it } from 'mocha';
import server from '../index';

chai.use(chaiHttp);
const should = chai.should();

describe.only('login through facebook', () => {
  it('redirects to facebook', (done) => {
    chai
      .request(server)
      .get('/api/v1/users/auth/facebook')
      .end((_err, res) => {
        res.redirects[0].should.contain('api/v1/users/auth/facebook/callback?__mock_strategy_callback=true');
        done();
      });
  });
  it('redirects to google', (done) => {
    chai
      .request(server)
      .get('/api/v1/users/auth/google')
      .end((_err, res) => {
          console.log(_err);
        // res.redirects[0].should.contain('api/v1/users/auth/google/callback?__mock_strategy_callback=true');
        done();
      });
  });


});

这是我的测试文件

import passport from 'passport';
import config from './auth';


let FacebookStrategy;
let GooglePlusStrategy;
if (process.env.NODE_ENV !== 'test') {
  FacebookStrategy = require('passport-facebook');
  GooglePlusStrategy = require('passport-google-oauth20');
} else {
  FacebookStrategy = require('passport-mocked').Strategy;
  GooglePlusStrategy = require('passport-mocked').OAuth2Strategy;
}

passport.use(new FacebookStrategy({
  name: 'facebook',
  clientID: '2618632188182397',
  clientSecret: 'f8a48d72223691ad05ca936fa20049e9',
  callbackURL: 'http://localhost:3000/api/v1/users/auth/facebook/callback',
  profileFields: ['id', 'displayName', 'photos', 'email']
}, (accessToken, refreshToken, profile, done) => {
  const firstName = profile._json.firstName;
  const lastName = profile._json.last_name;
  const email = profile.emails[0].value;
  const user = { firstName, lastName, email };
  // console.log(profile);
  done(null, user);
}));


passport.use(new GooglePlusStrategy({
  name: 'google',
  clientID: config.google.clientID,
  clientSecret: config.google.clientSecret,
  callbackURL: 'http://localhost:3000/api/v1/users/auth/google/callback'
}, (accessToken, refreshToken, profile, cb) => {
  const user = {
    username: profile.displayName,
    email: profile.emails[0].value,
    isverified: true
  };
  console.log(profile);
  cb(null, user);
}));

passport.serializeUser((user, done) => {
  done(null, user);
});

passport.deserializeUser((user, done) => {
  done(null, user);
});

export default passport;

这是我的护照配置

const server = app.listen(process.env.PORT || 3000, () => {
  // eslint-disable-next-line no-console
  console.log(`Listening on port ${server.address().port}`);
});

这是我的应用索引

app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email'] }));

app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }), (req,res) => res.send(">>>>>>>>>>>>>>>>"));


app.get('/auth/google', passport.authenticate('google', { scope: ['email', 'profile'] }));

app.get('/auth/google/callback', passport.authenticate('google'), (req,res) => res.send(">>>>>>>>>>>>>>>>") );

这些是我的路线

我希望我的测试结果采用 json 格式,但现在当我以 null 我 git 错误

Listening on port 3000
  login through facebook
    ✓ redirects to facebook (57ms)
{ Error: connect ECONNREFUSED 127.0.0.1:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3000,
  response: undefined }
    ✓ redirects to google

我遇到的错误

【问题讨论】:

    标签: node.js express testing passport-facebook passport-google-oauth


    【解决方案1】:

    您需要一些应用程序的一部分来处理回调(即:您需要公开http://localhost:3000/api/v1/users/auth/facebook/callbackhttp://localhost:3000/api/v1/users/auth/google/callback,并且最好是制作它们)。

    根据错误,Facebook 无法到达那个点...

    请对Passport site link 中的“路线”部分应用类似的解决方案。

    // GET /auth/google/callback
    //   Use passport.authenticate() as route middleware to authenticate the
    //   request.  If authentication fails, the user will be redirected back to the
    //   login page.  Otherwise, the primary route function function will be called,
    //   which, in this example, will redirect the user to the home page.
    app.get('/auth/google/callback', 
      passport.authenticate('google', { failureRedirect: '/login' }),
      function(req, res) {
        res.redirect('/');
      });
    

    【讨论】:

    • 不是真的,因为当我单独运行一项测试时它运行完美,但是当我同时运行它们时问题就来了,一个必须失败
    • 你是什么意思?它是怎么失败的?
    • 当一个测试通过然后再测试它会失败并显示无法连接到应用程序地址
    • 不,没有 500 代码它只是停在那里我用另一种方式,因为这不起作用
    猜你喜欢
    • 2023-01-17
    • 2021-12-18
    • 2020-09-19
    • 2021-01-24
    • 1970-01-01
    • 2016-02-16
    • 2022-08-18
    • 2019-08-07
    • 2015-10-17
    相关资源
    最近更新 更多