Node.js集成WireMock模拟Google OAuth流程时,获取用户信息失败的问题求助
我正在Node.js应用的集成测试中尝试用WireMock模拟Google OAuth的完整流程,但目前卡在了获取用户信息的环节,总是抛出InternalOAuthError: Failed to fetch user profile错误,想请大家帮忙看看我的配置哪里出了问题。
我的配置细节:
1. Passport GoogleStrategy 配置
我在Passport管理代码里修改了GoogleStrategy的端点指向本地WireMock服务:
passport.use(new GoogleStrategy({ clientID: config.googleAuth.clientID, clientSecret: config.googleAuth.clientSecret, callbackURL: config.googleAuth.callbackURL, passReqToCallback: true, authorizationURL: process.env.GOOGLE_AUTH_URL || 'https://accounts.google.com/o/oauth2/v2/auth', tokenURL: process.env.GOOGLE_TOKEN_URL || 'https://www.googleapis.com/oauth2/v4/token', userProfileURL: process.env.GOOGLE_USERINFO_URL || 'https://www.googleapis.com/oauth2/v3/userinfo', }, authUser));
2. 环境变量设置(PowerShell)
我把三个OAuth相关的端点指向了本地WireMock的端口:
$env:GOOGLE_AUTH_URL = "http://localhost:8077/oauth/authorize" $env:GOOGLE_TOKEN_URL = "http://localhost:8077/oauth/token" $env:GOOGLE_USERINFO_URL = "http://localhost:8077/userinfo"
3. WireMock 启动与Stub配置
我通过Docker启动WireMock,然后用PowerShell配置了三个核心Stub(授权回调、令牌发放、用户信息):
# 启动WireMock容器 docker run --rm -d -p 8077:8080 --name wiremock wiremock/wiremock:3.13.1 Start-Sleep -Seconds 5 # 配置/oauth/token的POST请求Stub,返回假令牌 $tokenStub = @{ request = @{ method = "POST" url = "/oauth/token" } response = @{ status = 200 jsonBody = @{ access_token = "fake-access-token" token_type = "Bearer" expires_in = 3600 } } } | ConvertTo-Json -Depth 10 Invoke-RestMethod -Uri "http://localhost:8077/__admin/mappings" -Method Post -ContentType "application/json" -Body $tokenStub # 配置/oauth/authorize的GET请求Stub,直接重定向到回调地址并携带假授权码 $authorizeStub = @" { "request": { "method": "GET", "urlPath": "/oauth/authorize", "queryParameters": { "response_type": { "equalTo": "code" }, "redirect_uri": { "contains": "localhost:3000/auth/google/callback" }, "scope": { "contains": "profile" }, "client_id": { "matches": ".*" } } }, "response": { "status": 302, "headers": { "Location": "http://localhost:3000/auth/google/callback?code=fake-auth-code" } } } "@ Invoke-RestMethod -Uri "http://localhost:8077/__admin/mappings" ` -Method Post -ContentType "application/json" -Body $authorizeStub # 配置/userinfo的GET请求Stub,返回假用户信息 $userinfoStub = @" { "request": { "method": "GET", "url": "/userinfo", "headers": { "Authorization": { "matches": "(?i)^Bearer\\s+fake-access-token$" } } }, "response": { "status": 200, "headers": { "Content-Type": "application/json" }, "jsonBody": { "id": "mockuser-123456", "email": "mockuser@example.com", "name": "Mock User", "username": "mockuser-123456" } } } "@ Invoke-RestMethod -Uri "http://localhost:8077/__admin/mappings" -Method Post -ContentType "application/json" -Body $userinfoStub
遇到的错误
测试时触发的完整错误栈如下:
InternalOAuthError: Failed to fetch user profile
at ....\node_modules\passport-google-oauth20\lib\strategy.js:99:19
at passBackControl (....\node_modules\oauth\lib\oauth2.js:132:9)
at IncomingMessage.(....\node_modules\oauth\lib\oauth2.js:157:7)
at IncomingMessage.emit (node:events:531:35)
at IncomingMessage.emit (node:domain:488:12)
at endReadableNT (node:internal/streams/readable:1696:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
我检查了WireMock的请求日志,发现/userinfo的Stub似乎根本没有被命中,怀疑是不是请求的匹配条件有问题?或者Passport在调用用户信息接口时还有什么我没考虑到的参数?
内容来源于stack exchange

