|
| 1 | +import { describe, it, beforeAll, expect } from "vitest"; |
| 2 | +import { fetch } from "undici"; |
| 3 | +import { waitForEvent } from "./utils"; |
| 4 | +import { httpsCallable } from "firebase/functions"; |
| 5 | +import { functions } from "./client"; |
| 6 | + |
| 7 | +describe("https.v2", () => { |
| 8 | + describe("httpsOnCallTrigger", () => { |
| 9 | + let data: any; |
| 10 | + let callData: any; |
| 11 | + let streamData: any[] = []; |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + data = await waitForEvent("httpsOnCall", async () => { |
| 15 | + const callable = httpsCallable(functions, "httpsOnCallTrigger"); |
| 16 | + |
| 17 | + const { stream, data: result } = await callable.stream({ |
| 18 | + foo: "bar", |
| 19 | + }); |
| 20 | + |
| 21 | + for await (const chunk of stream) { |
| 22 | + streamData.push(chunk); |
| 23 | + } |
| 24 | + |
| 25 | + // Await the final result of the callable |
| 26 | + callData = await result; |
| 27 | + }); |
| 28 | + }, 60_000); |
| 29 | + |
| 30 | + it("should accept the correct data", () => { |
| 31 | + expect(data.acceptsStreaming).toBe(true); |
| 32 | + expect(data.data).toEqual({ foo: "bar" }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should return the correct data", () => { |
| 36 | + expect(callData).toBe("onCall"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should stream the correct data", () => { |
| 40 | + expect(streamData).toEqual(["onCallStreamed"]); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe("httpsOnRequestTrigger", () => { |
| 45 | + let data: any; |
| 46 | + let status: number; |
| 47 | + let body: any; |
| 48 | + |
| 49 | + beforeAll(async () => { |
| 50 | + data = await waitForEvent("httpsOnRequest", async () => { |
| 51 | + console.log("Fetching..."); |
| 52 | + const response = await fetch( |
| 53 | + "https://us-central1-cf3-integration-tests-v2-qa.cloudfunctions.net/httpsOnRequestTrigger", |
| 54 | + { |
| 55 | + method: "POST", |
| 56 | + headers: { |
| 57 | + "Content-Type": "application/json", |
| 58 | + }, |
| 59 | + body: JSON.stringify({ foo: "bar" }), |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + console.log("Response:", response); |
| 64 | + |
| 65 | + status = response.status; |
| 66 | + console.log("Status:", status); |
| 67 | + body = await response.text(); |
| 68 | + console.log("Body:", body); |
| 69 | + }); |
| 70 | + }, 60_000); |
| 71 | + |
| 72 | + it("should accept the correct data", () => { |
| 73 | + expect(data).toEqual({ foo: "bar" }); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should return the correct status", () => { |
| 77 | + expect(status).toBe(201); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should return the correct body", () => { |
| 81 | + expect(body).toBe("onRequest"); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments