简体   繁体   English

使用 Firebase CLI shell 测试可调用的云函数

[英]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.

相关问题 使用可调用的 Firebase 云函数 - Using Firebase cloud functions callable FirebaseError:Internal Given While Testing Callable Firebase Cloud Functions with Node Script - FirebaseError:Internal Given While Testing Callable Firebase Cloud Functions with Node Script 使用Firebase云功能部署/测试功能 - Deploying/Testing functions with Firebase Cloud Functions Firebase 可调用函数。 Promise 在客户端返回 null(云函数) - Firebase callable functions. Promise is returning null on the client ( Cloud Functions ) Firebase CLI 未部署任何 Cloud Functions - Firebase CLI not deploying any Cloud Functions 更改 v2 中可调用的 Firebase 云函数的区域 - Change region of firebase cloud functions of callable in v2 Firebase 可调用函数的中间件 - Middleware for Firebase Callable Functions Cloud Functions Shell 中的 initializeApp() 上的 Firebase 无效凭据 302 - Firebase invalid-credential 302 on initializeApp() in Cloud Functions Shell 如何将依赖项注入 Firebase/Google Cloud Functions? (单元和集成测试) - How to inject dependencies into Firebase/Google Cloud Functions? (unit & integration testing) 使用带有GET的查询字符串在本地测试Firebase HTTP Cloud功能 - Testing Firebase HTTP Cloud functions locally using a query string with GET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM