-
-
Notifications
You must be signed in to change notification settings - Fork 681
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for netlify functions (#2493)
Co-authored-by: Ansh Goyal <[email protected]>%0ACo-authored-by: asyncapi-bot <[email protected]>
- Loading branch information
1 parent
9e4e1b6
commit 9f6308c
Showing
5 changed files
with
248 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Run tests for netlify edge-functions | ||
|
||
on: | ||
workflow_dispatch | ||
|
||
jobs: | ||
netlify-tests: | ||
strategy: | ||
matrix: | ||
deno-version: [1.30.0] | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup Deno | ||
uses: denolib/setup-deno@v2 | ||
with: | ||
deno-version: ${{ matrix.deno-version }} | ||
- name: Test with Deno | ||
run: deno test --allow-env --trace-ops | ||
|
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,5 +1,7 @@ | ||
module.exports = { | ||
verbose: true, // display individual test results with the test suite hierarchy | ||
collectCoverage: true, // collect test coverage information\ | ||
collectCoverageFrom: ['scripts/**/*.js'] | ||
verbose: true, | ||
collectCoverage: true, | ||
collectCoverageFrom: ['scripts/**/*.js'], | ||
// To disallow netlify edge function tests from running | ||
testMatch: ['**/tests/**/*.test.*', '!**/netlify/**/*.test.*'], | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,218 @@ | ||
import serveDefinitions from "../serve-definitions.ts"; | ||
import { Context } from "https://edge-bootstrap.netlify.app/v1/index.ts"; | ||
import * as mf from "https://deno.land/x/[email protected]/mod.ts"; | ||
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts"; | ||
|
||
const metricURL = "https://metric-api.eu.newrelic.com/metric/v1"; | ||
|
||
const validRequests = [ | ||
{ | ||
requestURL: "https://asyncapi.com/definitions/2.4.0/info.json", | ||
responseURL: | ||
"https://raw.githubusercontent.com/asyncapi/spec-json-schemas/master/definitions/2.4.0/info.json", | ||
}, | ||
{ | ||
requestURL: "https://asyncapi.com/definitions/2.4.0.json", | ||
responseURL: | ||
"https://raw.githubusercontent.com/asyncapi/spec-json-schemas/master/schemas/2.4.0.json", | ||
}, | ||
{ | ||
requestURL: "https://asyncapi.com/schema-store/2.5.0/operation.json", | ||
responseURL: | ||
"https://raw.githubusercontent.com/asyncapi/spec-json-schemas/master/definitions/2.5.0/operation.json", | ||
}, | ||
{ | ||
requestURL: "https://asyncapi.com/schema-store/2.5.0-without-$id.json", | ||
responseURL: | ||
"https://raw.githubusercontent.com/asyncapi/spec-json-schemas/master/schemas/2.5.0-without-$id.json", | ||
}, | ||
]; | ||
|
||
const invalidRequests = [ | ||
{ | ||
requestURL: "https://asyncapi.com/definitions/asyncapi.yaml", | ||
}, | ||
{ | ||
requestURL: "https://asyncapi.com/schema-store/2.4.0.JSON", | ||
}, | ||
{ | ||
requestURL: "https://asyncapi.com/foobar", | ||
}, | ||
{ | ||
requestURL: "https://asyncapi.com/", | ||
}, | ||
]; | ||
|
||
const context = { | ||
next: () => {}, | ||
log: () => {}, | ||
}; | ||
|
||
let metricCalls = 0; | ||
|
||
function setup() { | ||
mf.install(); | ||
|
||
mf.mock("*", (req) => { | ||
console.log(req.url); | ||
|
||
if (req.url === metricURL) { | ||
metricCalls++; | ||
} | ||
|
||
const body = { | ||
url: req.url, | ||
method: req.method, | ||
headers: req.headers, | ||
}; | ||
|
||
return new Response(JSON.stringify(body), { | ||
status: 200, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
Deno.test("serve-definitions test for validRequests", async () => { | ||
metricCalls = 0; | ||
|
||
setup(); | ||
|
||
for (const entry of validRequests) { | ||
console.log("Testing: " + entry.requestURL); | ||
|
||
const request = new Request(entry.requestURL, { method: "GET" }); | ||
const response = await serveDefinitions(request, context as Context); | ||
const body = await response.json(); | ||
|
||
assertEquals(response.status, 200); | ||
assertEquals(response.headers.get("Content-Type"), "application/schema+json"); | ||
assertEquals(body.url, entry.responseURL); | ||
|
||
console.log("\n"); | ||
} | ||
|
||
assertEquals(metricCalls, validRequests.length); | ||
|
||
mf.uninstall(); | ||
}); | ||
|
||
Deno.test("serve-definitions test for invalidRequests", async () => { | ||
metricCalls = 0; | ||
|
||
setup(); | ||
|
||
for (const entry of invalidRequests) { | ||
console.log("Testing: " + entry.requestURL); | ||
const request = new Request(entry.requestURL, { method: "GET" }); | ||
const response = await serveDefinitions(request, context as Context); | ||
|
||
assertEquals(response, undefined); | ||
} | ||
|
||
// No metrics should be sent for invalid requests | ||
assertEquals(metricCalls, 0); | ||
|
||
mf.uninstall(); | ||
}); | ||
|
||
Deno.test("serve-definitions test for various response statuses", async () => { | ||
const testCases = [ | ||
{ requestURL: "https://asyncapi.com/definitions/2.4.0/info.json", status: 200, mockParam: "GET@https://asyncapi.com/definitions/2.4.0/info.json" }, | ||
{ requestURL: "https://asyncapi.com/definitions/2.4.0/info.json", status: 304, mockParam: "GET@https://asyncapi.com/definitions/2.4.0/info.json" }, | ||
{ requestURL: "https://asyncapi.com/definitions/2.4.0/info.json", status: 404, mockParam: "GET@https://asyncapi.com/definitions/2.4.0/info.json" }, | ||
{ requestURL: "https://asyncapi.com/definitions/2.4.0/info.json", status: 500, mockParam: "GET@https://asyncapi.com/definitions/2.4.0/info.json" }, | ||
]; | ||
|
||
for (const { requestURL, status, mockParam } of testCases) { | ||
console.log("Testing: " + requestURL); | ||
|
||
mf.install(); | ||
|
||
mf.mock("*", () => { | ||
return new Response(status === 200 ? JSON.stringify({ url: requestURL }) : null, { | ||
status, | ||
}); | ||
}); | ||
|
||
const request = new Request(requestURL, { method: "GET" }); | ||
const response = await serveDefinitions(request, context as Context); | ||
|
||
if (status === 200) { | ||
const body = response.body ? await response.json() : null; | ||
assertEquals(response.status, status); | ||
assertEquals(body?.url, requestURL); | ||
} else { | ||
assertEquals(response.status, status); | ||
} | ||
|
||
if (status === 200 || status === 304) { | ||
metricCalls++; | ||
} | ||
|
||
|
||
console.log("\n"); | ||
mf.uninstall(); | ||
} | ||
|
||
assertEquals(metricCalls, testCases.filter((testCase) => testCase.status === 200 || testCase.status === 304).length); | ||
}); | ||
|
||
Deno.test("serve-definitions test for schema-unrelated requests", async () => { | ||
setup(); | ||
|
||
const schemaUnrelatedRequests = [ | ||
"https://asyncapi.com/definitions/asyncapi.yaml", | ||
"https://asyncapi.com/schema-store/2.4.0.JSON", | ||
"https://asyncapi.com/foobar", | ||
"https://asyncapi.com/", | ||
]; | ||
|
||
for (const requestURL of schemaUnrelatedRequests) { | ||
console.log("Testing: " + requestURL); | ||
const request = new Request(requestURL, { method: "GET" }); | ||
const response = await serveDefinitions(request, context as Context); | ||
|
||
assertEquals(response, undefined); | ||
} | ||
|
||
mf.uninstall(); | ||
}); | ||
|
||
Deno.test("serve-definitions test for schema-related non-JSON requests", async () => { | ||
setup(); | ||
metricCalls = 0; | ||
|
||
const schemaRelatedNonJsonRequests = [ | ||
"https://asyncapi.com/schema-store/2.5.0-without-$id", | ||
]; | ||
|
||
for (const requestURL of schemaRelatedNonJsonRequests) { | ||
const context = { | ||
next: () => { | ||
return new Response(JSON.stringify({ url: requestURL }), { | ||
status: 200, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
}, | ||
log: () => {}, | ||
} | ||
|
||
console.log("Testing: " + requestURL); | ||
const request = new Request(requestURL, { method: "GET" }); | ||
const response = await serveDefinitions(request, context as unknown as Context); | ||
const body = response?.body ? await response.json() : null; | ||
|
||
assertEquals(response?.status, 200); | ||
assertEquals(body.url, requestURL); | ||
assertEquals(response.headers.get("Content-Type"), "application/json"); // Default content type for non-JSON requests | ||
} | ||
|
||
mf.uninstall(); | ||
|
||
assertEquals(metricCalls, 0); | ||
}); |
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