Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 592b752

Browse files
authored
test(e2e-tests): add initial set of API tests, minor improvements to checking cache status in other tests (#624)
1 parent ca8094b commit 592b752

File tree

6 files changed

+128
-10
lines changed

6 files changed

+128
-10
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
describe("API Routes Tests", () => {
2+
before(() => {
3+
cy.ensureAllRoutesNotErrored();
4+
});
5+
6+
describe("Basic API", () => {
7+
const path = "/api/basic-api";
8+
9+
["DELETE", "POST", "GET", "PUT", "PATCH", "OPTIONS", "HEAD"].forEach(
10+
(method) => {
11+
it(`serves API request for path ${path} and method ${method}`, () => {
12+
cy.request({ url: path, method: method }).then((response) => {
13+
expect(response.status).to.equal(200);
14+
cy.verifyResponseCacheStatus(response, false);
15+
16+
if (method === "HEAD") {
17+
expect(response.body).to.be.empty;
18+
} else {
19+
expect(response.body).to.deep.equal({
20+
name: "This is a basic API route.",
21+
method: method
22+
});
23+
}
24+
});
25+
});
26+
}
27+
);
28+
});
29+
30+
describe("Dynamic + Nested API", () => {
31+
const base = "api/nested/";
32+
33+
["DELETE", "POST", "GET", "PUT", "PATCH", "OPTIONS", "HEAD"].forEach(
34+
(method) => {
35+
const id = "1";
36+
const path = base + id;
37+
38+
it(`serves API request for path ${path} and method ${method}`, () => {
39+
cy.request({ url: path, method: method }).then((response) => {
40+
expect(response.status).to.equal(200);
41+
cy.verifyResponseCacheStatus(response, false);
42+
43+
if (method === "HEAD") {
44+
expect(response.body).to.be.empty;
45+
} else {
46+
expect(response.body).to.deep.equal({
47+
id: id,
48+
name: `User ${id}`,
49+
method: method
50+
});
51+
}
52+
});
53+
});
54+
}
55+
);
56+
57+
["1", "2", "3", "4", "5"].forEach((id) => {
58+
const path = base + id;
59+
it(`serves API request for path ${path} for different IDs`, () => {
60+
cy.request({ url: path, method: "GET" }).then((response) => {
61+
expect(response.status).to.equal(200);
62+
expect(response.body).to.deep.equal({
63+
id: id,
64+
name: `User ${id}`,
65+
method: "GET"
66+
});
67+
cy.verifyResponseCacheStatus(response, false);
68+
});
69+
});
70+
});
71+
});
72+
});

packages/e2e-tests/next-app/cypress/integration/data-requests.test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ describe("Data Requests", () => {
1313
expect(response.headers["cache-control"]).to.not.be.undefined;
1414

1515
if (i === 1) {
16-
expect(response.headers["x-cache"]).to.equal(
17-
"Hit from cloudfront"
18-
);
16+
cy.verifyResponseCacheStatus(response, true);
1917
} else {
20-
expect([
18+
expect(response.headers["x-cache"]).to.be.oneOf([
2119
"Miss from cloudfront",
2220
"Hit from cloudfront"
23-
]).to.contain(response.headers["x-cache"]);
21+
]);
2422
}
2523
});
2624
}
@@ -58,10 +56,7 @@ describe("Data Requests", () => {
5856
for (let i = 0; i < 2; i++) {
5957
cy.request(fullPath).then((response) => {
6058
expect(response.status).to.equal(200);
61-
expect([
62-
"Miss from cloudfront",
63-
"LambdaGeneratedResponse from cloudfront"
64-
]).to.contain(response.headers["x-cache"]);
59+
cy.verifyResponseCacheStatus(response, false);
6560
expect(response.headers["cache-control"]).to.be.undefined;
6661
});
6762
}

packages/e2e-tests/next-app/cypress/integration/static-files.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("Static Files Tests", () => {
2424
cy.request(path);
2525
cy.request(path).then((response) => {
2626
expect(response.status).to.equal(200);
27-
expect(response.headers["x-cache"]).to.equal("Hit from cloudfront");
27+
cy.verifyResponseCacheStatus(response, true);
2828
});
2929
});
3030

packages/e2e-tests/next-app/cypress/support/commands.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ declare namespace Cypress {
3939
path: string,
4040
redirectedPath: string
4141
) => Cypress.Chainable<JQuery>;
42+
verifyResponseCacheStatus: (
43+
response: Cypress.Response,
44+
shouldBeCached: boolean
45+
) => Cypress.Chainable<JQuery>;
4246
}
4347
}
4448

@@ -115,3 +119,17 @@ Cypress.Commands.add(
115119
});
116120
}
117121
);
122+
123+
Cypress.Commands.add(
124+
"verifyResponseCacheStatus",
125+
(response: Cypress.Response, shouldBeCached: boolean) => {
126+
if (shouldBeCached) {
127+
expect(response.headers["x-cache"]).to.equal("Hit from cloudfront");
128+
} else {
129+
expect(response.headers["x-cache"]).to.be.oneOf([
130+
"Miss from cloudfront",
131+
"LambdaGeneratedResponse from cloudfront"
132+
]);
133+
}
134+
}
135+
);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NextApiRequest, NextApiResponse } from "next";
2+
3+
type Data = {
4+
name: string;
5+
method: string | undefined;
6+
};
7+
8+
export default (req: NextApiRequest, res: NextApiResponse<Data>) => {
9+
res.setHeader("Content-Type", "application/json");
10+
11+
res.status(200).json({
12+
name: "This is a basic API route.",
13+
method: req.method
14+
});
15+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { NextApiRequest, NextApiResponse } from "next";
2+
3+
type Data = {
4+
id: string | string[];
5+
name: string;
6+
method: string | undefined;
7+
};
8+
9+
export default (req: NextApiRequest, res: NextApiResponse<Data>) => {
10+
res.setHeader("Content-Type", "application/json");
11+
12+
const {
13+
query: { id },
14+
method
15+
} = req;
16+
17+
res.status(200).json({ id: id, name: `User ${id}`, method: method });
18+
};

0 commit comments

Comments
 (0)