-
Notifications
You must be signed in to change notification settings - Fork 325
🐛 针对 WebDAV 修复 cookies 认证冲突 及 authType 支持 #1308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+385
−75
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
57c66f4
🐛 修复 WebDAV 同步请求携带浏览器 cookies 导致认证冲突的问题 (#1297)
CodFrm 804601b
代码修正
cyfung1031 6669b42
webDavPatched -> patched
cyfung1031 bdae041
webdav.ts: 修正多个代码写法问题
cyfung1031 875ea6c
次序调整
cyfung1031 2b84cd3
Update webdav.ts
cyfung1031 e79a233
加入 visibilityFor 修正UI显示,实作 token 和 none 等 authType方式
cyfung1031 2dbf8ab
注释补充
cyfung1031 219c621
type check
cyfung1031 88f4d60
UI Fix
cyfung1031 1631230
修复 token auth
cyfung1031 b02e4a3
accessToken & UI display
cyfung1031 cff8bad
✅ 添加 WebDAVFileSystem 单元测试
CodFrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import type { WebDAVClient } from "webdav"; | ||
| import { getPatcher } from "webdav"; | ||
| import WebDAVFileSystem from "./webdav"; | ||
| import { WarpTokenError } from "../error"; | ||
|
|
||
| /** 创建 mock WebDAVClient */ | ||
| function createMockClient(overrides?: Partial<WebDAVClient>): WebDAVClient { | ||
| return { | ||
| getQuota: vi.fn().mockResolvedValue({}), | ||
| getDirectoryContents: vi.fn().mockResolvedValue([]), | ||
| getFileContents: vi.fn().mockResolvedValue("content"), | ||
| putFileContents: vi.fn().mockResolvedValue(true), | ||
| createDirectory: vi.fn().mockResolvedValue(undefined), | ||
| deleteFile: vi.fn().mockResolvedValue(undefined), | ||
| ...overrides, | ||
| } as unknown as WebDAVClient; | ||
| } | ||
|
|
||
| /** 创建可测试的 WebDAVFileSystem 实例(替换 client 为 mock) */ | ||
| function createTestFS(mockClient: WebDAVClient, url = "https://dav.example.com"): WebDAVFileSystem { | ||
| const fs = WebDAVFileSystem.fromCredentials(url, {}); | ||
| fs.client = mockClient; | ||
| return fs; | ||
| } | ||
|
|
||
| describe("WebDAVFileSystem", () => { | ||
| let mockClient: WebDAVClient; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockClient = createMockClient(); | ||
| }); | ||
|
|
||
| describe("initWebDAVPatch", () => { | ||
| it("应当通过 getPatcher 注册 fetch patch,设置 credentials 为 omit", () => { | ||
| // fromCredentials 内部调用 initWebDAVPatch,验证 patcher 已注册 fetch | ||
| WebDAVFileSystem.fromCredentials("https://dav.example.com", {}); | ||
|
|
||
| const patcher = getPatcher(); | ||
| // 验证 fetch 已被 patch(patcher 内部有 fetch 注册) | ||
| expect(patcher.isPatched("fetch")).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("fromCredentials", () => { | ||
| it("应当创建 WebDAVFileSystem 实例并设置 url 和 basePath", () => { | ||
| const fs = WebDAVFileSystem.fromCredentials("https://dav.example.com", { | ||
| authType: "password" as any, | ||
| username: "user", | ||
| password: "pass", | ||
| }); | ||
|
|
||
| expect(fs).toBeInstanceOf(WebDAVFileSystem); | ||
| expect(fs.url).toBe("https://dav.example.com"); | ||
| expect(fs.basePath).toBe("/"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("fromSameClient", () => { | ||
| it("应当复用已有 client 并设置新 basePath", () => { | ||
| const fs = createTestFS(mockClient); | ||
| const subFs = WebDAVFileSystem.fromSameClient(fs, "/subdir"); | ||
|
|
||
| expect(subFs).toBeInstanceOf(WebDAVFileSystem); | ||
| expect(subFs.url).toBe("https://dav.example.com"); | ||
| expect(subFs.basePath).toBe("/subdir"); | ||
| expect(subFs.client).toBe(mockClient); | ||
| }); | ||
| }); | ||
|
|
||
| describe("verify", () => { | ||
| it("应当成功验证", async () => { | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await expect(fs.verify()).resolves.toBeUndefined(); | ||
| expect(mockClient.getQuota).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("应当在 401 时抛出 WarpTokenError", async () => { | ||
| (mockClient.getQuota as ReturnType<typeof vi.fn>).mockRejectedValue({ | ||
| response: { status: 401 }, | ||
| message: "Unauthorized", | ||
| }); | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await expect(fs.verify()).rejects.toBeInstanceOf(WarpTokenError); | ||
| }); | ||
|
|
||
| it("应当在其他错误时抛出包含原始信息的 Error", async () => { | ||
| (mockClient.getQuota as ReturnType<typeof vi.fn>).mockRejectedValue({ | ||
| message: "Network error", | ||
| }); | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await expect(fs.verify()).rejects.toThrow("WebDAV verify failed: Network error"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("openDir", () => { | ||
| it("应当返回新实例并拼接路径", async () => { | ||
| const fs = createTestFS(mockClient); | ||
| const subFs = (await fs.openDir("docs")) as WebDAVFileSystem; | ||
|
|
||
| expect(subFs).toBeInstanceOf(WebDAVFileSystem); | ||
| expect(subFs.basePath).toBe("/docs"); | ||
| expect(subFs.client).toBe(mockClient); | ||
| }); | ||
|
|
||
| it("应当支持嵌套 openDir", async () => { | ||
| const fs = createTestFS(mockClient); | ||
| const sub1 = (await fs.openDir("a")) as WebDAVFileSystem; | ||
| const sub2 = (await sub1.openDir("b")) as WebDAVFileSystem; | ||
|
|
||
| expect(sub2.basePath).toBe("/a/b"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("createDir", () => { | ||
| it("应当调用 createDirectory", async () => { | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await fs.createDir("new-folder"); | ||
|
|
||
| expect(mockClient.createDirectory).toHaveBeenCalledWith("/new-folder"); | ||
| }); | ||
|
|
||
| it("应当在 405 错误时静默成功(目录已存在)", async () => { | ||
| (mockClient.createDirectory as ReturnType<typeof vi.fn>).mockRejectedValue({ | ||
| response: { status: 405 }, | ||
| message: "405 Method Not Allowed", | ||
| }); | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await expect(fs.createDir("existing")).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it("应当在 message 包含 405 时也静默成功", async () => { | ||
| (mockClient.createDirectory as ReturnType<typeof vi.fn>).mockRejectedValue({ | ||
| message: "Request failed with status code 405", | ||
| }); | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await expect(fs.createDir("existing")).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it("应当在其他错误时抛出异常", async () => { | ||
| const err = new Error("Forbidden"); | ||
| (mockClient.createDirectory as ReturnType<typeof vi.fn>).mockRejectedValue(err); | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await expect(fs.createDir("denied")).rejects.toThrow("Forbidden"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("delete", () => { | ||
| it("应当调用 deleteFile", async () => { | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await fs.delete("test.txt"); | ||
|
|
||
| expect(mockClient.deleteFile).toHaveBeenCalledWith("/test.txt"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("list", () => { | ||
| it("应当列出文件并过滤目录", async () => { | ||
| (mockClient.getDirectoryContents as ReturnType<typeof vi.fn>).mockResolvedValue([ | ||
| { | ||
| type: "file", | ||
| basename: "test.txt", | ||
| lastmod: "2024-01-01T00:00:00Z", | ||
| etag: '"abc"', | ||
| size: 1024, | ||
| }, | ||
| { | ||
| type: "directory", | ||
| basename: "subdir", | ||
| lastmod: "2024-01-01T00:00:00Z", | ||
| etag: "", | ||
| size: 0, | ||
| }, | ||
| ]); | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| const files = await fs.list(); | ||
|
|
||
| expect(files).toHaveLength(1); | ||
| expect(files[0]).toMatchObject({ | ||
| name: "test.txt", | ||
| path: "/", | ||
| digest: '"abc"', | ||
| size: 1024, | ||
| }); | ||
| }); | ||
|
|
||
| it("应当在 404 时返回空数组", async () => { | ||
| (mockClient.getDirectoryContents as ReturnType<typeof vi.fn>).mockRejectedValue({ | ||
| response: { status: 404 }, | ||
| }); | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| const files = await fs.list(); | ||
| expect(files).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("应当在其他错误时抛出异常", async () => { | ||
| const err = new Error("Server Error"); | ||
| (err as any).response = { status: 500 }; | ||
| (mockClient.getDirectoryContents as ReturnType<typeof vi.fn>).mockRejectedValue(err); | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| await expect(fs.list()).rejects.toThrow("Server Error"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getDirUrl", () => { | ||
| it("应当返回 url + basePath", async () => { | ||
| const fs = createTestFS(mockClient); | ||
| const subFs = (await fs.openDir("docs")) as WebDAVFileSystem; | ||
|
|
||
| expect(await subFs.getDirUrl()).toBe("https://dav.example.com/docs"); | ||
| }); | ||
|
|
||
| it("根路径应返回 url + /", async () => { | ||
| const fs = createTestFS(mockClient); | ||
|
|
||
| expect(await fs.getDirUrl()).toBe("https://dav.example.com/"); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ScriptCat的奇怪排版导致 要 140px
明明内容没有那么阔
不过算了。就这样吧