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

fix(sdk): ambiguous api paths are allowed #4291

Closed
wants to merge 167 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
167 commits
Select commit Hold shift + click to select a range
367b4f7
First version of the check for ambiguous paths
gcfbn Sep 26, 2023
0e40a53
Update unit tests
gcfbn Sep 26, 2023
6a22038
Fix the e2e test
gcfbn Sep 28, 2023
977a4d2
Address PR remarks
gcfbn Sep 28, 2023
a6d7098
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
50bd9b2
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
f4ace7f
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
99a8e36
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
eb740a5
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
2ab0880
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
17979ed
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
bd45f14
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
348fbeb
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
4bc1fc2
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
6032e9a
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
bd8e251
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
ac29b34
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
9d4d824
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
52d7064
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
2188b9f
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
d068c46
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
3bae471
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
72e5e23
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
c6e6425
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
c423d7c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
ea991c7
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
98bed23
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
cd87599
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
8070db7
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
0902c22
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
f646d38
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 28, 2023
b549b84
chore: self mutation (e2e-2of2.diff)
monadabot Sep 28, 2023
2814636
Merge remote-tracking branch 'upstream/main' into 4052-ambiguous-api-…
gcfbn Sep 30, 2023
6883d36
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
c847f21
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
9bb29b2
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
c6b115f
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
559f377
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
c25d6fe
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
a346691
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
8d87f12
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
ae88a95
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
72b635b
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
d270ce1
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
a514761
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
ca15ec0
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
bc1d190
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
93f59aa
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
44de7d1
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
f02944c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
e9e1081
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
94ab66c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
7928d7a
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
5ba5c3c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
5c836fa
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
a129427
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
59686cb
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
4ca9e2e
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
d1366a6
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
f527f25
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
37f4488
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
5f1472c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
05aad34
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
5eb5f5f
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
6d54387
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
5753204
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
1d78e7a
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
59b427c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
f4e906d
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
ffd8c52
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
05c2523
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
1b71c14
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
5988165
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
5885605
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
02672cc
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
7daade0
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
d006b6f
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
4212af3
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
71d1d53
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
fcd809b
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
1007ca6
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
24797ce
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
f01d922
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
613d030
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
16ebcec
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
23ddd2a
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
4f37dd8
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
55b907d
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
cbc6395
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
70a100c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
9afd00b
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
4bcccf3
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
deda680
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
5684950
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
7d4145a
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
3c55a36
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
c420327
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
6d7f58c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
39e88bb
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
050a04b
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
2843721
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
a4654d8
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
9827106
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
090cae2
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
20dec58
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
4b5408f
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
275a641
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
16a7339
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
b135033
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
e323d74
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
b0d9321
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
38fbb5e
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Sep 30, 2023
483b131
chore: self mutation (e2e-2of2.diff)
monadabot Sep 30, 2023
5474b8f
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
1faaff4
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
88689e7
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
40d4fcf
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
8dc2c54
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
5cbaf48
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
1219e8b
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
63236c4
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
15b5ec0
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
98b92d5
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
b2c5011
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
ef3345a
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
6aabfbd
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
0478b2e
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
a42e879
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
09c83e4
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
1cd4eed
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
4febda5
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
547d565
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
52bafcb
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
478d196
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
9e70f7f
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
7786687
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
90fe550
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
c05122c
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
e54dd9e
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
1316345
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
8b83b78
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
591aef5
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
523eb5f
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
405a9d5
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
70491bf
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
a4ca4cd
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
8517566
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
49ece24
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
bc4e242
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
6d29fad
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
2afd55a
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
1dfe9f4
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
a8b66df
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
90bc79e
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
7982dd7
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
4d3fd08
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
fda24c0
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
0e409ac
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
857bc74
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
88ae918
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
b536d07
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
51087ab
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
46f6cad
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
3bb42aa
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
45b18c3
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
e469288
Merge branch 'main' into 4052-ambiguous-api-paths
monadabot Oct 1, 2023
4335163
chore: self mutation (e2e-2of2.diff)
monadabot Oct 1, 2023
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
15 changes: 15 additions & 0 deletions examples/tests/invalid/ambiguous_api_paths.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bring cloud;

let api = new cloud.Api();


let handler = inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
return cloud.ApiResponse {
body: "ok",
status: 200
};
};

api.get("/test/path", handler);
api.get("/test/{variable}", handler);
// ^ Endpoint for path '/test/{variable}' and method 'GET' is ambiguous - it conflicts with existing endpoint for path '/test/path'
6 changes: 3 additions & 3 deletions examples/tests/valid/api_valid_path.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ testInvalidPath("/{no.dots.here}");


// valid paths
testValidPath("/test");
testValidPath("/test/path");
testValidPath("/test/alphanumer1cPa_th");
testValidPath("/test/regular/path");
testValidPath("/test/pa-th/{with}/two/{variable_s}/f?bla=5&b=6");
testValidPath("/test/param/is/{last}");
testValidPath("/test/{param}");
testValidPath("/test/path/{param}");
testValidPath("/{param}");
testValidPath("/t/{param}");
testValidPath("/test/regular/path/{param}");
testValidPath("/test/segment1/{param1}/segment2?query1=value1?query2=value2");
testValidPath("/test/segment1/segment2?query=value1&query2=value2");
testValidPath("/test.withDots");
testValidPath("/test/path.withDots");
57 changes: 57 additions & 0 deletions libs/wingsdk/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,57 @@ export abstract class Api extends Resource {
};
}

/**
* Checks if two given paths are ambiguous.
* @param pathA
* @param pathB
* @returns A boolean value indicating if provided paths are ambiguous.
* @internal
*/
protected _arePathsAmbiguous(pathA: string, pathB: string): boolean {
const partsA = pathA.split("/");
const partsB = pathB.split("/");

if (partsA.length !== partsB.length) {
return false;
}

for (let i = 0; i < partsA.length; i++) {
const partA = partsA[i];
const partB = partsB[i];

if (
partA !== partB &&
!partA.match(/^{.+?}$/) &&
!partB.match(/^{.+?}$/)
) {
return false;
}
}

return true;
}
gcfbn marked this conversation as resolved.
Show resolved Hide resolved

/**
* Checks if provided path and method are ambigous with paths and methods already defined in the api spec.
* @param path Path to be checked
* @param method HTTP method
* @returns A boolean value indicating if provided path and method are ambiguous.
* @internal
*/
protected _findAmbiguousPath(
path: string,
method: string
): string | undefined {
const existingPaths = Object.keys(this.apiSpec.paths);
gcfbn marked this conversation as resolved.
Show resolved Hide resolved

return existingPaths.find(
(existingPath) =>
!!this.apiSpec.paths[existingPath][method.toLowerCase()] &&
this._arePathsAmbiguous(existingPath, path)
);
}

/**
* Generates the OpenAPI schema for CORS headers based on the provided CORS options.
* @param corsOptions The CORS options to generate the schema from.
Expand Down Expand Up @@ -441,6 +492,12 @@ export abstract class Api extends Resource {
`Endpoint for path '${path}' and method '${method}' already exists`
);
}
const ambiguousPath = this._findAmbiguousPath(path, method);
if (!!ambiguousPath) {
throw new Error(
`Endpoint for path '${path}' and method '${method}' is ambiguous - it conflicts with existing endpoint for path '${ambiguousPath}'`
);
gcfbn marked this conversation as resolved.
Show resolved Hide resolved
}
const operationId = `${method.toLowerCase()}${
path === "/" ? "" : path.replace("/", "-")
}`;
Expand Down
190 changes: 190 additions & 0 deletions libs/wingsdk/test/target-sim/__snapshots__/api.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,195 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`api allows duplicates routes with different methods 1`] = `
{
".wing/onrequesthandler-e645076f_c887c38c.js": "exports.handler = async function(event) {
return await (new ((function(){
return class Handler {
constructor(clients) {
for (const [name, client] of Object.entries(clients)) {
this[name] = client;
}
}
async handle(req) { return { status: 200, body: req.body, headers: req.headers }; }
};
})())({

})).handle(event);
};",
"connections.json": {
"connections": [
{
"name": "get()",
"source": "root/my_api",
"target": "root/my_api/OnRequestHandler-e645076f",
},
{
"name": "post()",
"source": "root/my_api",
"target": "root/my_api/OnRequestHandler-e645076f",
},
],
"version": "connections-0.1",
},
"simulator.json": {
"resources": [
{
"attrs": {},
"path": "root/cloud.TestRunner",
"props": {
"tests": {},
},
"type": "wingsdk.cloud.TestRunner",
},
{
"attrs": {},
"path": "root/my_api/OnRequestHandler-e645076f",
"props": {
"environmentVariables": {},
"sourceCodeFile": ".wing/onrequesthandler-e645076f_c887c38c.js",
"sourceCodeLanguage": "javascript",
"timeout": 60000,
},
"type": "wingsdk.cloud.Function",
},
{
"attrs": {},
"path": "root/my_api",
"props": {
"openApiSpec": {
"openapi": "3.0.3",
"paths": {
"/hello": {
"get": {
"operationId": "get-hello",
"parameters": [],
"responses": {
"200": {
"content": {},
"description": "200 response",
},
},
},
"post": {
"operationId": "post-hello",
"parameters": [],
"responses": {
"200": {
"content": {},
"description": "200 response",
},
},
},
},
},
},
},
"type": "wingsdk.cloud.Api",
},
{
"attrs": {},
"path": "root/my_api/ApiEventMapping-e645076f",
"props": {
"publisher": "\${root/my_api#attrs.handle}",
"subscriber": "\${root/my_api/OnRequestHandler-e645076f#attrs.handle}",
"subscriptionProps": {
"routes": [
{
"method": "GET",
"path": "/hello",
},
{
"method": "POST",
"path": "/hello",
},
],
},
},
"type": "wingsdk.sim.EventMapping",
},
],
"sdkVersion": "0.0.0",
},
"tree.json": {
"tree": {
"children": {
"Handler": {
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.70",
},
"display": {
"description": "An inflight resource",
"hidden": true,
"title": "Inflight",
},
"id": "Handler",
"path": "root/Handler",
},
"cloud.TestRunner": {
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.70",
},
"display": {
"description": "A suite of APIs for running tests and collecting results.",
"hidden": true,
"title": "TestRunner",
},
"id": "cloud.TestRunner",
"path": "root/cloud.TestRunner",
},
"my_api": {
"children": {
"ApiEventMapping-e645076f": {
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.70",
},
"display": {
"hidden": true,
},
"id": "ApiEventMapping-e645076f",
"path": "root/my_api/ApiEventMapping-e645076f",
},
"OnRequestHandler-e645076f": {
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.70",
},
"display": {
"description": "A cloud function (FaaS)",
"sourceModule": "@winglang/sdk",
"title": "get()",
},
"id": "OnRequestHandler-e645076f",
"path": "root/my_api/OnRequestHandler-e645076f",
},
},
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.70",
},
"display": {
"description": "A REST API endpoint",
"title": "Api",
},
"id": "my_api",
"path": "root/my_api",
},
},
"constructInfo": {
"fqn": "constructs.Construct",
"version": "10.2.70",
},
"id": "root",
"path": "root",
},
"version": "tree-0.1",
},
}
`;

exports[`api handler can read the request params 1`] = `
[
"wingsdk.cloud.TestRunner created.",
Expand Down
90 changes: 90 additions & 0 deletions libs/wingsdk/test/target-sim/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,96 @@ test("api with 'name' & 'age' parameter", async () => {
expect(app.snapshot()).toMatchSnapshot();
});

test("api doesn't allow duplicated routes", () => {
// GIVEN
const app = new SimApp();
const api = cloud.Api._newApi(app, "my_api");
const inflight = Testing.makeHandler(app, "Handler", INFLIGHT_CODE_ECHO_BODY);
api.get("/hello", inflight);

// THEN
expect(() => api.get("/hello", inflight)).toThrowError(
"Endpoint for path '/hello' and method 'GET' already exists"
);
});

test("api allows duplicates routes with different methods", () => {
// GIVEN
const app = new SimApp();
const api = cloud.Api._newApi(app, "my_api");
const inflight = Testing.makeHandler(app, "Handler", INFLIGHT_CODE_ECHO_BODY);
api.get("/hello", inflight);

// WHEN
api.post("/hello", inflight);

// THEN
expect(app.snapshot()).toMatchSnapshot();
});

test("api doesn't allow ambiguous routes", () => {
// GIVEN
const app = new SimApp();
const api = cloud.Api._newApi(app, "my_api");
const path = "/api/hello/{name}";
const inflightGet = Testing.makeHandler(
app,
"Handler",
INFLIGHT_CODE_ECHO_BODY
);
api.get(path, inflightGet);

// WHEN
const ambiguousPath = "/api/{name}/hello";

// THEN
expect(() => api.get(ambiguousPath, inflightGet)).toThrowError(
`Endpoint for path '${ambiguousPath}' and method 'GET' is ambiguous - it conflicts with existing endpoint for path '${path}'`
);
});

test("api doesn't allow ambiguous routes containing only variables", () => {
// GIVEN
const app = new SimApp();
const api = cloud.Api._newApi(app, "my_api");
const path = "/{age}";
const inflightGet = Testing.makeHandler(
app,
"Handler",
INFLIGHT_CODE_ECHO_BODY
);
api.get(path, inflightGet);

// WHEN
const ambiguousPath = "/{name}";

// THEN
expect(() => api.get(ambiguousPath, inflightGet)).toThrowError(
`Endpoint for path '${ambiguousPath}' and method 'GET' is ambiguous - it conflicts with existing endpoint for path '${path}'`
);
});

test("api doesn't allow ambiguous routes containing different number of varaibles", () => {
// GIVEN
const app = new SimApp();
const api = cloud.Api._newApi(app, "my_api");
const path = "/{param}/{something}";
const inflightGet = Testing.makeHandler(
app,
"Handler",
INFLIGHT_CODE_ECHO_BODY
);
api.get(path, inflightGet);

// WHEN
const ambiguousPath = "/path/{something}";

// THEN
expect(() => api.get(ambiguousPath, inflightGet)).toThrowError(
`Endpoint for path '${ambiguousPath}' and method 'GET' is ambiguous - it conflicts with existing endpoint for path '${path}'`
);
});

gcfbn marked this conversation as resolved.
Show resolved Hide resolved
test("api with multiple GET routes and one lambda", () => {
// GIVEN
const app = new SimApp();
Expand Down
Loading
Loading