【问题标题】:Testing callable cloud functions with the Firebase CLI shell使用 Firebase CLI shell 测试可调用的云函数
【发布时间】:2018-09-16 03:29:27
【问题描述】:

我一直在 firebase functions:shell 中尝试新的 firebase 可调用云函数,但不断出现以下错误

请求的 Content-Type 不正确。

从函数收到响应:400,{"error":{"status":"INVALID_ARGUMENT","message":"Bad Request"}}

这里是嗬 w 我想在 shell 上调用这个函数

myFunc.post(dataObject)

我也试过了

myFunc.post().form(dataObject)

然后我得到错误的编码(表单)错误。 dataObject 是有效的 JSON。

更新:

我想我需要使用firebase serve 来模拟这些callable https 函数。数据需要像这样在 post 请求中传递(注意它是如何嵌套在 data 参数中的)

{
 "data":{
    "applicantId": "XycWNYxqGOhL94ocBl9eWQ6wxHn2",
    "openingId": "-L8kYvb_jza8bPNVENRN"
 }
}

我仍然想不通的是如何在通过 REST 客户端调用该函数时传递虚拟身份验证信息

【问题讨论】:

  • 目前您正在尝试做的事情并没有得到很好的支持。您必须了解可调用的有线协议才能操作它们,而这尚未记录在案。

标签: node.js firebase google-cloud-functions firebase-cli


【解决方案1】:

CLI 的正确语法已更改为 myFunc({"message": "Hello!"})

【讨论】:

  • 欢迎来到 Stackoverflow。请尝试很好地描述您的答案或其工作原理,以便对其他人有所帮助。
【解决方案2】:

据我所知,函数的 second 参数包含所有附加数据。传递一个包含headers 映射的对象,您应该能够指定任何您想要的内容。

myFunc.post("", { headers: { "authorization": "Bearer ..." } });

如果您使用 Express 来处理路由,那么它看起来就像:

myApp.post("/my-endpoint", { headers: { "authorization": "Bearer ..." } });

【讨论】:

    【解决方案3】:

    如果你看一下source code,你会发现它只是一个普通的 https post 函数,带有一个包含 json web 令牌的身份验证标头,我建议使用 unit test api for https functions 并模拟标头从测试用户和请求正文返回令牌的方法

    [更新] 示例

    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);
    

    【讨论】:

      【解决方案4】:

      我设法让它在函数 shell 中运行它:

      myFunc.post('').json({"message": "Hello!"})

      【讨论】:

      • OMG 感谢 Merry 的提示。我通过将消息包装在“数据”对象中,使其在函数 v2 中工作。 myFunc.post('').json({"data": {"message": "Hello!"}})
      • 您可以通过myFunc.post('', {headers: {Authorization: 'Bearer $token'}}).json({'data': 'hello'})进行身份验证
      猜你喜欢
      • 2020-04-18
      • 2018-02-24
      • 2018-05-17
      • 2019-01-18
      • 2020-07-07
      • 2020-11-24
      • 2020-05-17
      • 2020-07-12
      • 2023-02-16
      相关资源
      最近更新 更多