diff --git a/federation/cryptography/index.test.ts b/federation/cryptography/index.test.ts index 3bf5fbc..e9056c7 100644 --- a/federation/cryptography/index.test.ts +++ b/federation/cryptography/index.test.ts @@ -180,5 +180,20 @@ describe("SignatureConstructor", () => { ); expect(parts[3].split("=")[1]).toBeString(); }); + + test("should correctly sign a Request", async () => { + const url = new URL("https://example.com"); + const request = new Request(url.toString(), { + method: "GET", + body: body, + }); + const newRequest = await ctor.sign(request); + + headers = newRequest.headers; + expect(headers.get("Signature")).toBeDefined(); + expect(headers.get("Date")).toBeDefined(); + + expect(await newRequest.text()).toBe(body); + }); }); }); diff --git a/federation/cryptography/index.ts b/federation/cryptography/index.ts index 11f9095..7a8d4d7 100644 --- a/federation/cryptography/index.ts +++ b/federation/cryptography/index.ts @@ -266,6 +266,8 @@ export class SignatureConstructor { headers: Headers = new Headers(), ): Promise { if (requestOrMethod instanceof Request) { + const request = requestOrMethod.clone(); + const headers = await this.sign( requestOrMethod.method as HttpVerb, new URL(requestOrMethod.url), @@ -273,8 +275,6 @@ export class SignatureConstructor { requestOrMethod.headers, ); - const request = requestOrMethod.clone(); - request.headers.set("Date", headers.get("Date") ?? ""); request.headers.set("Signature", headers.get("Signature") ?? "");