Skip to content

Commit

Permalink
feat: setup initial testing for netlify edge functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurtu-gal committed Dec 11, 2023
1 parent d023aeb commit 22746a9
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .github/workflows/netlify-edge-functions-test.yml
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

2 changes: 1 addition & 1 deletion netlify/edge-functions/serve-definitions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context } from "netlify:edge";
import type { Context } from "https://edge-bootstrap.netlify.app/v1/index.ts";

const GITHUB_TOKEN = Deno.env.get("GITHUB_TOKEN_NR");
const NR_API_KEY = Deno.env.get("NR_API_KEY");
Expand Down
85 changes: 85 additions & 0 deletions netlify/edge-functions/tests/serve-definitions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 validRequests = [
{
requestURL: "http://localhost:8888/definitions/2.4.0/info.json",
responseURL:
"https://raw.githubusercontent.com/asyncapi/spec-json-schemas/master/definitions/2.4.0/info.json",
},
{
requestURL: "http://localhost:8888/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: "http://localhost:8888/definitions/asyncapi.yaml",
},
{
requestURL: "http://localhost:8888/schema-store/2.4.0.JSON",
},
];

const context = {
next: () => {},
log: () => {},
};

function setup() {
mf.install();

mf.mock("*", (req) => {
console.log(req.url);

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 () => {
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(body.url, entry.responseURL);

console.log("\n");
}

mf.uninstall();
});

Deno.test("serve-definitions test for invalidRequests", async () => {
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);
}

mf.uninstall();
});

0 comments on commit 22746a9

Please sign in to comment.