import { describe, expect, it } from "vitest";
import { createDavFixture, davRequest } from "@tests/worker/helpers/dav";
import { fetchWorker } from "@tests/worker/helpers/http";
import { name } from "./files-helpers";
describe("WebDAV file basics", () => {
it("creates collections, writes file bytes through R2, and reports DAV properties", async () => {
const fixture = await createDavFixture();
const directory = name("put-get");
const options = await fetchWorker(davRequest(fixture, "/files/", { method: "OPTIONS" }));
expect(options.status).toBe(204);
expect(options.headers.get("dav")).toBe("1, 2");
const mkcol = await fetchWorker(davRequest(fixture, `/files/${directory}/`, { method: "MKCOL" }));
expect(mkcol.status).toBe(201);
const put = await fetchWorker(
davRequest(fixture, `/files/${directory}/hello.txt`, {
method: "PUT",
headers: { "content-type": "text/plain" },
body: "hello dav",
}),
);
expect(put.status).toBe(201);
expect(put.headers.get("etag")).toMatch(/^"[0-9a-f]+"$/);
const get = await fetchWorker(davRequest(fixture, `/files/${directory}/hello.txt`));
expect(get.status).toBe(200);
expect(get.headers.get("content-type")).toBe("text/plain");
await expect(get.text()).resolves.toBe("hello dav");
const propfind = await fetchWorker(
davRequest(fixture, `/files/${directory}/`, {
method: "PROPFIND",
headers: { depth: "1", "content-type": "application/xml" },
body: ``,
}),
);
expect(propfind.status).toBe(207);
const xml = await propfind.text();
expect(xml).toContain(`/files/${directory}/`);
expect(xml).toContain(`/files/${directory}/hello.txt`);
expect(xml).toContain("9");
});
it("rejects unsupported MKCOL bodies even when Content-Length is absent", async () => {
const fixture = await createDavFixture();
const empty = await fetchWorker(davRequest(fixture, `/files/${name("empty-mkcol")}/`, { method: "MKCOL" }));
expect(empty.status).toBe(201);
const withBody = await fetchWorker(
davRequest(fixture, `/files/${name("body-mkcol")}/`, {
method: "MKCOL",
headers: { "content-type": "application/xml" },
body: '',
}),
);
expect(withBody.status).toBe(415);
});
});