mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-08-28 14:27:08 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c4419a837c |
@@ -147,14 +147,9 @@ export class ObsidianAPIService extends InjectableAPIService<ObsidianServiceCont
|
|||||||
: req instanceof Request && typeof req.method === "string"
|
: req instanceof Request && typeof req.method === "string"
|
||||||
? req.method
|
? req.method
|
||||||
: "GET";
|
: "GET";
|
||||||
if (typeof req !== "string") {
|
const suppliedBody = opts?.body ?? (req instanceof Request ? req.body : undefined);
|
||||||
if (opts?.body) {
|
if (suppliedBody !== undefined && suppliedBody !== null) {
|
||||||
body = typeof opts.body === "string" ? opts.body : await new Response(opts.body).arrayBuffer();
|
body = typeof suppliedBody === "string" ? suppliedBody : await new Response(suppliedBody).arrayBuffer();
|
||||||
} else if (req.body) {
|
|
||||||
body = await new Response(req.body).arrayBuffer();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
body = opts?.body as string;
|
|
||||||
}
|
}
|
||||||
const reqHeaders = new Headers(req instanceof Request ? req.headers : {});
|
const reqHeaders = new Headers(req instanceof Request ? req.headers : {});
|
||||||
|
|
||||||
@@ -192,7 +187,9 @@ export class ObsidianAPIService extends InjectableAPIService<ObsidianServiceCont
|
|||||||
contentType: contentType,
|
contentType: contentType,
|
||||||
};
|
};
|
||||||
const r = await requestUrl({ ...requestParam, throw: false });
|
const r = await requestUrl({ ...requestParam, throw: false });
|
||||||
return new Response(r.arrayBuffer, {
|
const responseHasNoBody =
|
||||||
|
method.toUpperCase() === "HEAD" || r.status === 204 || r.status === 205 || r.status === 304;
|
||||||
|
return new Response(responseHasNoBody ? null : r.arrayBuffer, {
|
||||||
headers: r.headers,
|
headers: r.headers,
|
||||||
status: r.status,
|
status: r.status,
|
||||||
statusText: `${r.status}`,
|
statusText: `${r.status}`,
|
||||||
|
|||||||
@@ -4,16 +4,17 @@ const mocks = vi.hoisted(() => ({
|
|||||||
platform: {
|
platform: {
|
||||||
isMobile: false,
|
isMobile: false,
|
||||||
},
|
},
|
||||||
|
requestUrl: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("@/deps.ts", () => ({
|
vi.mock("@/deps.ts", () => ({
|
||||||
Platform: mocks.platform,
|
Platform: mocks.platform,
|
||||||
requestUrl: vi.fn(),
|
requestUrl: mocks.requestUrl,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("@/deps", () => ({
|
vi.mock("@/deps", () => ({
|
||||||
Platform: mocks.platform,
|
Platform: mocks.platform,
|
||||||
requestUrl: vi.fn(),
|
requestUrl: mocks.requestUrl,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock("@/modules/essentialObsidian/APILib/ObsHttpHandler", () => ({
|
vi.mock("@/modules/essentialObsidian/APILib/ObsHttpHandler", () => ({
|
||||||
@@ -65,3 +66,41 @@ describe("ObsidianAPIService.showWindowOnRight", () => {
|
|||||||
expect(workspace.revealLeaf).toHaveBeenCalledWith(rightLeaf);
|
expect(workspace.revealLeaf).toHaveBeenCalledWith(rightLeaf);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("ObsidianAPIService.nativeFetch", () => {
|
||||||
|
it("normalises a typed-array body when the request URL is a string", async () => {
|
||||||
|
mocks.requestUrl.mockResolvedValue({
|
||||||
|
arrayBuffer: new Uint8Array([9, 8, 7]).buffer,
|
||||||
|
headers: { etag: '"created"' },
|
||||||
|
status: 201,
|
||||||
|
});
|
||||||
|
const source = new Uint8Array([0, 1, 2, 3, 4]);
|
||||||
|
const body = source.subarray(1, 4);
|
||||||
|
|
||||||
|
const response = await createService({}).nativeFetch("http://127.0.0.1:8088/dav/probe.bin", {
|
||||||
|
body: body as unknown as BodyInit,
|
||||||
|
headers: { "Content-Type": "application/octet-stream" },
|
||||||
|
method: "PUT",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status).toBe(201);
|
||||||
|
const request = mocks.requestUrl.mock.calls[0][0] as { body?: unknown };
|
||||||
|
expect(request.body).toBeInstanceOf(ArrayBuffer);
|
||||||
|
expect([...new Uint8Array(request.body as ArrayBuffer)]).toEqual([1, 2, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("constructs a bodyless response for a successful DELETE", async () => {
|
||||||
|
mocks.requestUrl.mockResolvedValue({
|
||||||
|
arrayBuffer: new ArrayBuffer(0),
|
||||||
|
headers: {},
|
||||||
|
status: 204,
|
||||||
|
});
|
||||||
|
|
||||||
|
const response = await createService({}).nativeFetch("http://127.0.0.1:8088/dav/probe.bin", {
|
||||||
|
method: "DELETE",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(response.status).toBe(204);
|
||||||
|
expect(await response.text()).toBe("");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user