Skip to content

Commit

Permalink
feat: add tests for metric calls
Browse files Browse the repository at this point in the history
* change url to asyncapi.com
* add test script to package.json
  • Loading branch information
Shurtu-gal committed Dec 21, 2023
1 parent 22746a9 commit aabef87
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
4 changes: 3 additions & 1 deletion netlify/edge-functions/serve-definitions.ts
Original file line number Diff line number Diff line change
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]) {
return null;
}

Expand Down
28 changes: 24 additions & 4 deletions netlify/edge-functions/tests/serve-definitions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,49 @@ 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: "http://localhost:8888/definitions/2.4.0/info.json",
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: "http://localhost:8888/schema-store/2.5.0-without-$id.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: "http://localhost:8888/definitions/asyncapi.yaml",
requestURL: "https://asyncapi.com/definitions/asyncapi.yaml",
},
{
requestURL: "http://localhost:8888/schema-store/2.4.0.JSON",
requestURL: "https://asyncapi.com/schema-store/2.4.0.JSON",
},
{
requestURL: "https://asyncapi.com/foobar",
}
];

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,
Expand All @@ -52,6 +63,8 @@ function setup() {
}

Deno.test("serve-definitions test for validRequests", async () => {
metricCalls = 0;

setup();

for (const entry of validRequests) {
Expand All @@ -67,10 +80,14 @@ Deno.test("serve-definitions test for validRequests", async () => {
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) {
Expand All @@ -81,5 +98,8 @@ Deno.test("serve-definitions test for invalidRequests", async () => {
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

0 comments on commit aabef87

Please sign in to comment.