[英]Testing callable cloud functions with the Firebase CLI shell
I have been trying new firebase callable cloud functions in firebase functions:shell
I keep on getting following error我一直在firebase functions:shell
尝试新的 firebase 可调用云firebase functions:shell
我不断收到以下错误
Request has incorrect Content-Type.请求的内容类型不正确。
and和
RESPONSE RECEIVED FROM FUNCTION: 400, {"error":{"status":"INVALID_ARGUMENT","message":"Bad Request"}}从函数收到的响应:400,{"error":{"status":"INVALID_ARGUMENT","message":"Bad Request"}}
Here is ho w I am trying to call this function on shell这是我尝试在 shell 上调用此函数的方式
myFunc.post(dataObject) myFunc.post(dataObject)
I have also tried this我也试过这个
myFunc.post().form(dataObject) myFunc.post().form(dataObject)
But then I get wrong encoding(form) error.但是后来我得到了错误的编码(形式)错误。 dataObject
is valid JSON. dataObject
是有效的 JSON。
Update:更新:
I figured that I need to use firebase serve
for local emulation of these callable https
functions.我想我需要使用firebase serve
来对这些callable https
函数进行本地模拟。 Data needs to be passed in post request like this(notice how its nested in data
parameter)数据需要像这样在 post 请求中传递(注意它是如何嵌套在data
参数中的)
{
"data":{
"applicantId": "XycWNYxqGOhL94ocBl9eWQ6wxHn2",
"openingId": "-L8kYvb_jza8bPNVENRN"
}
}
What I can't figure still is how do I pass dummy auth info while calling that function via a REST client我仍然无法想象的是如何在通过 REST 客户端调用该函数时传递虚拟身份验证信息
我设法让它在函数外壳中运行它:
myFunc.post('').json({"message": "Hello!"})
As far as I can tell, the second parameter to the function contains all of the additional data.据我所知,该函数的第二个参数包含所有附加数据。 Pass an object that contains a headers
map and you should be able to specify anything you want.传递一个包含headers
映射的对象,您应该能够指定任何您想要的内容。
myFunc.post("", { headers: { "authorization": "Bearer ..." } });
If you're using Express to handle routing, then it just looks like:如果您使用 Express 来处理路由,那么它看起来就像:
myApp.post("/my-endpoint", { headers: { "authorization": "Bearer ..." } });
If you take a look at the source code , you can see that it is just a normal https post function With an authentication header that contains a json web token, I'd recommend using the unit test api for https functions and mocking the header methods to return a token from a test user as well as the request body如果您查看源代码,您会发现它只是一个普通的 https post 函数,带有包含 json web 令牌的身份验证标头,我建议对 https 函数使用单元测试 api并模拟标头方法从测试用户以及请求正文返回令牌
[Update] Example [更新]示例
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);
CLI 的正确语法已更改为myFunc({"message": "Hello!"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.