- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个模拟的(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 中运行结果时/strong> 我 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
我得到的错误
最佳答案
您需要应用程序的一些部分来处理回调(即:您需要公开 http://localhost:3000/api/v1/users/auth/facebook/callback 和 http://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('/');
});
关于node.js - ECONNREFUSED 127.0.0.1 :3000 at TCPConnectWrap. afterConnect [as oncomplete] errno : 'ECONNREFUSED' ,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58348542/
我有一个相当简单的设置来测试 Spring 中的 Stomp 支持。 我计划使用 JS 将消息发送到队列并在 Spring 应用程序中接收和处理它们。但是,由于某种原因,它似乎不起作用。 JS 客户端
我正在尝试使用 XMLHttpRequest 模块在 for 循环中异步调用 REST API。我在 1 个循环中发出 400 个请求,每 100 个请求后等待 1 秒。这在客户端 JavaScrip
在尝试使用 MSSQL 注入(inject)准备好的语句时(乏味) - 一旦我尝试执行请求,我就会收到以下错误: Database connection failed: Requests can on
我正在尝试通过 dokcer-compose 将 Node 与 mysql 连接。我可以使用工作台访问 mysql,但是当我尝试连接 Node 时出现错误: Error: connect ECONNR
我正在尝试使用 url 链接下载文件,但出现以下错误 throw er; // Unhandled 'error' event Error: connect ECONNREFUSED at expor
我在一个模拟的(facebook/google)策略上运行一些测试但是当我一个一个地运行这些测试时我遇到的问题是它正在运行但是当我同时运行它们时它们在第一次测试后不工作,连接应用程序立即失败。 我尝试
我想访问我保存在 C:/Users/myusername/.aws 中的 AWS 凭证该文件称为凭证,没有任何扩展名,它有我的 aws_access_key_id、aws_secret_access_
我是一名优秀的程序员,十分优秀!