-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: deprecate
onUnhandledRequest
and accepting object payloads fo…
…r for `webhooks.verify()` and `webhooks.verifyAndReceive()` (#790)
- Loading branch information
Showing
9 changed files
with
136 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,3 +1,90 @@ | ||
import { createServer } from "http"; | ||
import { readFileSync } from "fs"; | ||
|
||
import fetch from "node-fetch"; | ||
import { sign } from "@octokit/webhooks-methods"; | ||
|
||
import { Webhooks, createNodeMiddleware } from "../../src"; | ||
|
||
const pushEventPayloadString = readFileSync( | ||
"test/fixtures/push-payload.json", | ||
"utf-8" | ||
); | ||
let signatureSha256: string; | ||
describe("Deprecations", () => { | ||
test("there are currently no deprecations", () => {}); | ||
beforeAll(async () => { | ||
signatureSha256 = await sign( | ||
{ secret: "mySecret", algorithm: "sha256" }, | ||
pushEventPayloadString | ||
); | ||
}); | ||
test("onUnhandledRequest", async () => { | ||
const spy = jest.spyOn(console, "error"); | ||
const webhooks = new Webhooks({ | ||
secret: "mySecret", | ||
}); | ||
|
||
const server = createServer( | ||
createNodeMiddleware(webhooks, { | ||
onUnhandledRequest(_request, response) { | ||
response.writeHead(404); | ||
response.end("nope"); | ||
}, | ||
}) | ||
).listen(); | ||
|
||
// @ts-expect-error complains about { port } although it's included in returned AddressInfo interface | ||
const { port } = server.address(); | ||
|
||
await fetch(`http://localhost:${port}/api/github/webhooks`, { | ||
method: "PUT", | ||
headers: { | ||
"X-GitHub-Delivery": "123e4567-e89b-12d3-a456-426655440000", | ||
"X-GitHub-Event": "push", | ||
"X-Hub-Signature-256": signatureSha256, | ||
}, | ||
body: "invalid", | ||
}); | ||
|
||
expect(spy).toBeCalledWith( | ||
"[@octokit/webhooks] `onUnhandledRequest()` is deprecated and will be removed in a future release of `@octokit/webhooks`" | ||
); | ||
spy.mockClear(); | ||
server.close(); | ||
}); | ||
|
||
test("webhooks.verify(payload, signature) with object payload", async () => { | ||
const spy = jest.spyOn(console, "error"); | ||
const secret = "mysecret"; | ||
const webhooks = new Webhooks({ secret }); | ||
|
||
await webhooks.verify( | ||
JSON.parse(pushEventPayloadString), | ||
await sign({ secret, algorithm: "sha256" }, pushEventPayloadString) | ||
); | ||
expect(spy).toBeCalledWith( | ||
"[@octokit/webhooks] Passing a JSON payload object to `verify()` is deprecated and the functionality will be removed in a future release of `@octokit/webhooks`" | ||
); | ||
spy.mockClear(); | ||
}); | ||
|
||
test("webhooks.verifyAndReceive(payload, signature) with object payload", async () => { | ||
const spy = jest.spyOn(console, "error"); | ||
const secret = "mysecret"; | ||
const webhooks = new Webhooks({ secret }); | ||
|
||
await webhooks.verifyAndReceive({ | ||
id: "123e456", | ||
name: "push", | ||
payload: JSON.parse(pushEventPayloadString), | ||
signature: await sign( | ||
{ secret, algorithm: "sha256" }, | ||
pushEventPayloadString | ||
), | ||
}); | ||
expect(spy).toBeCalledWith( | ||
"[@octokit/webhooks] Passing a JSON payload object to `verifyAndReceive()` is deprecated and the functionality will be removed in a future release of `@octokit/webhooks`" | ||
); | ||
spy.mockClear(); | ||
}); | ||
}); |