如果你看一下源代码,你会发现它只是一个普通的 https post 函数,它带有一个包含 json web 令牌的身份验证头,我建议使用单元测试 api 用于 https 函数并模拟头方法从测试用户以及请求正文返回令牌
[更新]示例
const firebase = require("firebase");
var config = {
your config
};
firebase.initializeApp(config);
const test = require("firebase-functions-test")(
{
your config
},
"your access token"
);
const admin = require("firebase-admin");
const chai = require("chai");
const sinon = require("sinon");
const email = "test@test.test";
const password = "password";
let myFunctions = require("your function file");
firebase
.auth()
.signInWithEmailAndPassword(email, password)
.then(user => user.getIdToken())
.then(token => {
const req = {
body: { data: { your:"data"} },
method: "POST",
contentType: "application/json",
header: name =>
name === "Authorization"
? `Bearer ${token}`
: name === "Content-Type" ? "application/json" : null,
headers: { origin: "" }
};
const res = {
status: status => {
console.log("Status: ", status);
return {
send: result => {
console.log("result", result);
}
};
},
getHeader: () => {},
setHeader: () => {}
};
myFunctions.yourFunction(req, res);
})
.catch(console.error);