Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for netlify functions #2493

Merged
merged 16 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

6 changes: 4 additions & 2 deletions 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";
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved

const GITHUB_TOKEN = Deno.env.get("GITHUB_TOKEN_NR");
const NR_API_KEY = Deno.env.get("NR_API_KEY");
Expand Down Expand Up @@ -69,7 +69,9 @@ export default async (request: Request, context: Context) => {

function buildRewrite(originalRequest: Request): (Request | null) {
const extractResult = legitimateRequestRegex.exec(new URL(originalRequest.url).pathname);
if (extractResult === null) {

// No need to rewrite the request if it's not a legitimate request for a definition file
if (extractResult === null || extractResult.length < 2 || !extractResult[2]) {
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

Expand Down
105 changes: 105 additions & 0 deletions netlify/edge-functions/tests/serve-definitions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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",
},
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved
{
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",
}
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved
];

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 = {
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved
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(body.url, entry.responseURL);
Shurtu-gal marked this conversation as resolved.
Show resolved Hide resolved

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();
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"generate:videos": "node scripts/build-newsroom-videos.js",
"generate:tools": "node scripts/build-tools.js",
"test": "npx cypress run --component",
"test:netlify": "deno test --allow-env --trace-ops",
"cy:open": "cypress open",
"cy:run": "cypress run"
},
Expand Down