Skip to content

Commit

Permalink
Add cloudchamber curl command
Browse files Browse the repository at this point in the history
  • Loading branch information
IRCody committed Jun 21, 2024
1 parent 6f445cf commit b36cb7f
Show file tree
Hide file tree
Showing 4 changed files with 549 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-dragons-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

feature: Add 'cloudchamber curl command
112 changes: 112 additions & 0 deletions packages/wrangler/src/__tests__/cloudchamber/curl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import patchConsole from "patch-console";
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
import { mockConsoleMethods } from "../helpers/mock-console";
import { msw } from "../helpers/msw";
import { runInTempDir } from "../helpers/run-in-tmp";
import { runWrangler } from "../helpers/run-wrangler";
import { mockAccount, setWranglerConfig } from "./utils";
import { MOCK_DEPLOYMENTS_COMPLEX } from "../helpers/mock-cloudchamber";
import { useMockIsTTY } from "../helpers/mock-istty";
import { http, HttpResponse } from "msw";

describe("cloudchamber curl", () => {
const std = mockConsoleMethods();
const { setIsTTY } = useMockIsTTY();

mockAccountId();
mockApiToken();
runInTempDir();
beforeEach(mockAccount);

afterEach(() => {
patchConsole(() => {});
msw.resetHandlers();
});

it("should help", async () => {
await runWrangler("cloudchamber curl --help");
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`
"wrangler cloudchamber curl <path>
perform curl in wrangler
Positionals:
path [string] [required] [default: \\"/\\"]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
Options:
--json Return output as clean JSON [boolean] [default: false]
-H, --header Add headers in the form of --header <name>:<value> [array]
-D, --data Add a JSON body to the request [string]
-X, --method [string] [default: \\"GET\\"]
-s, --silent Only output response [boolean]
-v, --verbose Show version number [boolean]
--use-stdin, --stdin Equivalent of using --data-binary @- in curl [boolean]"
`);
});

it("should be able to use data flag", async () => {
setIsTTY(false)
setWranglerConfig({});
msw.use(
http.post(
"*/deployments/v2",
async ({ request }) => {
expect(await request.text()).toMatchInlineSnapshot(
`"{\\"image\\":\\"hello:world\\",\\"location\\":\\"sfo06\\",\\"ssh_public_key_ids\\":[],\\"environment_variables\\":[{\\"name\\":\\"HELLO\\",\\"value\\":\\"WORLD\\"},{\\"name\\":\\"YOU\\",\\"value\\":\\"CONQUERED\\"}],\\"vcpu\\":3,\\"memory\\":\\"400GB\\",\\"network\\":{\\"assign_ipv4\\":\\"predefined\\"}}"`
);
return HttpResponse.json(MOCK_DEPLOYMENTS_COMPLEX[0]);
}
)
);

await runWrangler(`cloudchamber curl /deployments/v2 --json -X POST -D "{\\"image\\":\\"hello:world\\",\\"location\\":\\"sfo06\\",\\"ssh_public_key_ids\\":[],\\"environment_variables\\":[{\\"name\\":\\"HELLO\\",\\"value\\":\\"WORLD\\"},{\\"name\\":\\"YOU\\",\\"value\\":\\"CONQUERED\\"}],\\"vcpu\\":3,\\"memory\\":\\"400GB\\",\\"network\\":{\\"assign_ipv4\\":\\"predefined\\"}}"`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"id\\": \\"1\\",
\\"type\\": \\"default\\",
\\"created_at\\": \\"123\\",
\\"account_id\\": \\"123\\",
\\"vcpu\\": 4,
\\"memory\\": \\"400MB\\",
\\"version\\": 1,
\\"image\\": \\"hello\\",
\\"location\\": {
\\"name\\": \\"sfo06\\",
\\"enabled\\": true
},
\\"network\\": {
\\"ipv4\\": \\"1.1.1.1\\"
},
\\"placements_ref\\": \\"http://ref\\",
\\"node_group\\": \\"metal\\"
}"
`);
});

it("should set headers", async () => {
setIsTTY(false)
setWranglerConfig({});
msw.use(
http.get(
"*/test",
async ({ request }) => {
expect(request.headers.get("something")).toEqual("here");
expect(request.headers.get("other")).toEqual("thing");
return HttpResponse.json(`{}`);
}
)
);
await runWrangler("cloudchamber curl /test --json --header something:here --header other:thing");
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.out).toMatchInlineSnapshot(`"\\"{}\\""`);
});
});
Loading

0 comments on commit b36cb7f

Please sign in to comment.