|
1 | 1 | import { createServer, Server } from "http"; |
2 | 2 | import { expect } from "chai"; |
3 | | -import * as nock from "nock"; |
4 | | -import AbortController from "abort-controller"; |
| 3 | +import * as sinon from "sinon"; |
5 | 4 | import * as FormData from "form-data"; |
| 5 | +import * as auth from "./auth"; |
| 6 | +import nock from "./test/helpers/nock"; |
6 | 7 | const proxySetup = require("proxy"); |
7 | 8 |
|
8 | 9 | import { Client, CLI_OAUTH_PROJECT_NUMBER } from "./apiv2"; |
9 | 10 | import { FirebaseError } from "./error"; |
10 | 11 | import { streamToString, stringToStream } from "./utils"; |
11 | 12 |
|
12 | 13 | describe("apiv2", () => { |
| 14 | + let authStub: sinon.SinonStub | undefined; |
| 15 | + before(() => { |
| 16 | + if (typeof (auth.getAccessToken as any).restore !== "function") { |
| 17 | + authStub = sinon.stub(auth, "getAccessToken").resolves({ access_token: "owner" } as any); |
| 18 | + } |
| 19 | + }); |
| 20 | + after(() => { |
| 21 | + if (authStub) { |
| 22 | + authStub.restore(); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
13 | 26 | beforeEach(() => { |
14 | 27 | // The api module has package variables that we don't want sticking around. |
15 | 28 | delete require.cache[require.resolve("./apiv2")]; |
@@ -151,8 +164,21 @@ describe("apiv2", () => { |
151 | 164 |
|
152 | 165 | it("should resend a multipart body when retrying after a premature close", async () => { |
153 | 166 | const sentBodies: string[] = []; |
| 167 | + let lastPushTime = 0; |
154 | 168 | const capture = (b: unknown): boolean => { |
155 | | - sentBodies.push(typeof b === "string" ? b : JSON.stringify(b)); |
| 169 | + const now = Date.now(); |
| 170 | + if (now - lastPushTime > 5) { |
| 171 | + let bodyStr = ""; |
| 172 | + if (b instanceof Uint8Array || Buffer.isBuffer(b)) { |
| 173 | + bodyStr = Buffer.from(b).toString("utf8"); |
| 174 | + } else if (typeof b === "string") { |
| 175 | + bodyStr = b; |
| 176 | + } else { |
| 177 | + bodyStr = JSON.stringify(b); |
| 178 | + } |
| 179 | + sentBodies.push(bodyStr); |
| 180 | + lastPushTime = now; |
| 181 | + } |
156 | 182 | return true; |
157 | 183 | }; |
158 | 184 | nock("https://example.com").post("/upload", capture).once().replyWithError({ |
@@ -584,7 +610,11 @@ describe("apiv2", () => { |
584 | 610 | new Promise((resolve) => proxyServer.close(resolve)), |
585 | 611 | new Promise((resolve) => targetServer.close(resolve)), |
586 | 612 | ]); |
587 | | - process.env.HTTP_PROXY = oldProxy; |
| 613 | + if (oldProxy === undefined) { |
| 614 | + delete process.env.HTTP_PROXY; |
| 615 | + } else { |
| 616 | + process.env.HTTP_PROXY = oldProxy; |
| 617 | + } |
588 | 618 | }); |
589 | 619 |
|
590 | 620 | it("should be able to make a basic GET request", async () => { |
|
0 commit comments