File
Blob: tests/worker/protocol/webdav/files-basics.workers.test.ts
| 1 | import { describe, expect, it } from "vitest"; |
| 2 | |
| 3 | import { createDavFixture, davRequest } from "@tests/worker/helpers/dav"; |
| 4 | import { fetchWorker } from "@tests/worker/helpers/http"; |
| 5 | import { name } from "./files-helpers"; |
| 6 | |
| 7 | describe("WebDAV file basics", () => { |
| 8 | it("creates collections, writes file bytes through R2, and reports DAV properties", async () => { |
| 9 | const fixture = await createDavFixture(); |
| 10 | const directory = name("put-get"); |
| 11 | |
| 12 | const options = await fetchWorker(davRequest(fixture, "/files/", { method: "OPTIONS" })); |
| 13 | expect(options.status).toBe(204); |
| 14 | expect(options.headers.get("dav")).toBe("1, 2"); |
| 15 | |
| 16 | const mkcol = await fetchWorker(davRequest(fixture, `/files/${directory}/`, { method: "MKCOL" })); |
| 17 | expect(mkcol.status).toBe(201); |
| 18 | |
| 19 | const put = await fetchWorker( |
| 20 | davRequest(fixture, `/files/${directory}/hello.txt`, { |
| 21 | method: "PUT", |
| 22 | headers: { "content-type": "text/plain" }, |
| 23 | body: "hello dav", |
| 24 | }), |
| 25 | ); |
| 26 | expect(put.status).toBe(201); |
| 27 | expect(put.headers.get("etag")).toMatch(/^"[0-9a-f]+"$/); |
| 28 | |
| 29 | const get = await fetchWorker(davRequest(fixture, `/files/${directory}/hello.txt`)); |
| 30 | expect(get.status).toBe(200); |
| 31 | expect(get.headers.get("content-type")).toBe("text/plain"); |
| 32 | await expect(get.text()).resolves.toBe("hello dav"); |
| 33 | |
| 34 | const propfind = await fetchWorker( |
| 35 | davRequest(fixture, `/files/${directory}/`, { |
| 36 | method: "PROPFIND", |
| 37 | headers: { depth: "1", "content-type": "application/xml" }, |
| 38 | body: `<?xml version="1.0"?><D:propfind xmlns:D="DAV:"><D:prop><D:resourcetype/><D:getcontentlength/><D:getetag/></D:prop></D:propfind>`, |
| 39 | }), |
| 40 | ); |
| 41 | expect(propfind.status).toBe(207); |
| 42 | const xml = await propfind.text(); |
| 43 | expect(xml).toContain(`/files/${directory}/`); |
| 44 | expect(xml).toContain(`/files/${directory}/hello.txt`); |
| 45 | expect(xml).toContain("<D:getcontentlength>9</D:getcontentlength>"); |
| 46 | }); |
| 47 | |
| 48 | it("rejects unsupported MKCOL bodies even when Content-Length is absent", async () => { |
| 49 | const fixture = await createDavFixture(); |
| 50 | const empty = await fetchWorker(davRequest(fixture, `/files/${name("empty-mkcol")}/`, { method: "MKCOL" })); |
| 51 | expect(empty.status).toBe(201); |
| 52 | |
| 53 | const withBody = await fetchWorker( |
| 54 | davRequest(fixture, `/files/${name("body-mkcol")}/`, { |
| 55 | method: "MKCOL", |
| 56 | headers: { "content-type": "application/xml" }, |
| 57 | body: '<D:mkcol xmlns:D="DAV:"/>', |
| 58 | }), |
| 59 | ); |
| 60 | expect(withBody.status).toBe(415); |
| 61 | }); |
| 62 | }); |