Skip to content

Commit

Permalink
fix(sdk): fixing api.url token inconsistency (#3772)
Browse files Browse the repository at this point in the history
Fixes: #3024 #3539 #3342 
The problem with the token was the way cdktf is tokenizing strings, I assume it tried to create a new token every time we call `api.url`, or maybe tokenize a token. Now they're all the same- no matter if you call it on preflight or inflight it or assign it to another variable- it'll generate exactly the same token number.

other than that:
- brought back the aws api SDK tests
- changed sim response to be an empty string (in express it's an empty object by default)

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
tsuf239 authored Aug 13, 2023
1 parent a930aa4 commit 024225b
Show file tree
Hide file tree
Showing 23 changed files with 738 additions and 423 deletions.
17 changes: 5 additions & 12 deletions examples/tests/sdk_tests/api/delete.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ bring cloud;
bring http;
bring util;

// https://github.com/winglang/wing/issues/3049
let http_DELETE = http.HttpMethod.DELETE;
let api_DELETE = cloud.HttpMethod.DELETE;

let api = new cloud.Api();

api.delete("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
assert(req.method == api_DELETE);
assert(req.method == cloud.HttpMethod.DELETE);
assert(req.query?.get("all") == "true");
assert(req.query?.get("page") == "6");
assert(req.path == "/path");
Expand All @@ -20,12 +16,11 @@ api.delete("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
};
});

/// https://github.com/winglang/wing/issues/3342
if (util.env("WING_TARGET") != "tf-aws") {
test "http.delete and http.fetch can preform a call to an api" {

test "http.delete and http.fetch can preform a call to an api" {
let url = "${api.url}/path?all=true&page=6";
let response: http.Response = http.delete(url);
let fetchResponse: http.Response = http.fetch(url, method: http_DELETE);
let fetchResponse: http.Response = http.fetch(url, method: http.HttpMethod.DELETE);

assert(response.body == "6");
assert(response.status == 200);
Expand All @@ -34,6 +29,4 @@ if (util.env("WING_TARGET") != "tf-aws") {
assert(fetchResponse.body == "6");
assert(fetchResponse.status == 200);
assert(fetchResponse.url == url);

}
}
}
16 changes: 5 additions & 11 deletions examples/tests/sdk_tests/api/get.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ bring cloud;
bring http;
bring util;

// https://github.com/winglang/wing/issues/3049
let http_GET = http.HttpMethod.GET;
let api_GET = cloud.HttpMethod.GET;

let api = new cloud.Api();
let body = "ok!";

api.get("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
assert(req.method == api_GET);
assert(req.method == cloud.HttpMethod.GET);
assert(req.path == "/path");
// req.body is not a string in sim- https://github.com/winglang/wing/issues/3024
// assert(req.body?.length == 0);
assert(req.body?.length == 0);
assert(req.headers?.get("content-type") == "application/json");

return cloud.ApiResponse {
Expand All @@ -22,12 +18,11 @@ api.get("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
};
});

/// https://github.com/winglang/wing/issues/3342
if (util.env("WING_TARGET") != "tf-aws") {
test "http.get and http.fetch can preform a call to an api" {

test "http.get and http.fetch can preform a call to an api" {
let url = api.url + "/path";
let getResponse: http.Response = http.get(url, headers: { "content-type" => "application/json" });
let fetchResponse: http.Response = http.fetch(url, method: http_GET, headers: { "content-type" => "application/json" });
let fetchResponse: http.Response = http.fetch(url, method: http.HttpMethod.GET, headers: { "content-type" => "application/json" });
let fetchResponseNoMethod: http.Response = http.fetch(url, headers: { "content-type" => "application/json" });


Expand All @@ -42,5 +37,4 @@ if (util.env("WING_TARGET") != "tf-aws") {
assert(fetchResponseNoMethod.body == body);
assert(fetchResponseNoMethod.status == 200);
assert(fetchResponseNoMethod.url == url);
}
}
28 changes: 8 additions & 20 deletions examples/tests/sdk_tests/api/options.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,26 @@ bring cloud;
bring http;
bring util;

// https://github.com/winglang/wing/issues/3049
let api_OPTIONS = cloud.HttpMethod.OPTIONS;
let http_OPTIONS = http.HttpMethod.OPTIONS;

let api_HEAD = cloud.HttpMethod.HEAD;
let http_HEAD = http.HttpMethod.HEAD;

let api_CONNECT = cloud.HttpMethod.CONNECT;


let api = new cloud.Api();
let path = "/path";


api.options(path, inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
assert(req.method == api_OPTIONS);
assert(req.method == cloud.HttpMethod.OPTIONS);
assert(req.path == path);
// req.body is not a string in sim- https://github.com/winglang/wing/issues/3024
// assert(req.body?.length == 0);
assert(req.body?.length == 0);

return cloud.ApiResponse {
status: 204
};
});

api.head(path, inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
assert(req.method == api_HEAD);
assert(req.method == cloud.HttpMethod.HEAD);
assert(req.path == path);
// req.body is not a string in sim- https://github.com/winglang/wing/issues/3024
// assert(req.body?.length == 0);
assert(req.body?.length == 0);

return cloud.ApiResponse {
status: 204
Expand All @@ -48,16 +38,14 @@ api.connect(path, inflight (req: cloud.ApiRequest): cloud.ApiResponse => {



/// https://github.com/winglang/wing/issues/3342
if (util.env("WING_TARGET") != "tf-aws") {
test "http.fetch can preform a call to an api to CONNECT, HEAD and OPTIONS" {

test "http.fetch can preform a call to an api to CONNECT, HEAD and OPTIONS" {
let url = api.url + path;
let options: http.Response = http.fetch(url, method: http_OPTIONS);
let head: http.Response = http.fetch(url, method: http_HEAD);
let options: http.Response = http.fetch(url, method: http.HttpMethod.OPTIONS);
let head: http.Response = http.fetch(url, method: http.HttpMethod.HEAD);

assert(options.status == 204);
assert(options.url == url);
}
}


15 changes: 4 additions & 11 deletions examples/tests/sdk_tests/api/patch.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ bring cloud;
bring http;
bring util;

// https://github.com/winglang/wing/issues/3049
let http_PATCH = http.HttpMethod.PATCH;
let api_PATCH = cloud.HttpMethod.PATCH;

let api = new cloud.Api();

let body = Json {"cat": "Tion"};
let _id = "12345";

api.patch("/path/{id}", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
assert(req.method == api_PATCH);
assert(req.method == cloud.HttpMethod.PATCH);

assert(req.vars?.get("id") == _id);
assert(req.path == "/path/"+ _id);
Expand All @@ -25,12 +21,11 @@ api.patch("/path/{id}", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
};
});

/// https://github.com/winglang/wing/issues/3342
if (util.env("WING_TARGET") != "tf-aws") {
test "http.patch and http.fetch can preform a call to an api" {

test "http.patch and http.fetch can preform a call to an api" {
let url = "${api.url}/path/${_id}";
let response: http.Response = http.patch(url, headers: { "content-type" => "application/json" }, body: Json.stringify(body));
let fetchResponse: http.Response = http.patch(url, method: http_PATCH, headers: { "content-type" => "application/json" }, body: Json.stringify(body));
let fetchResponse: http.Response = http.patch(url, method: http.HttpMethod.PATCH, headers: { "content-type" => "application/json" }, body: Json.stringify(body));

assert(response.body == _id);
assert(response.status == 200);
Expand All @@ -39,6 +34,4 @@ if (util.env("WING_TARGET") != "tf-aws") {
assert(fetchResponse.body == _id);
assert(fetchResponse.status == 200);
assert(fetchResponse.url == url);

}
}
15 changes: 4 additions & 11 deletions examples/tests/sdk_tests/api/post.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ bring cloud;
bring http;
bring util;

// https://github.com/winglang/wing/issues/3049
let http_POST = http.HttpMethod.POST;
let api_POST = cloud.HttpMethod.POST;

let api = new cloud.Api();
let body = Json {"cat": "Tion"};

api.post("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
assert(req.method == api_POST);
assert(req.method == cloud.HttpMethod.POST);
assert(req.path == "/path");
assert(req.body == Json.stringify(body));
assert(req.headers?.get("content-type") == "application/json");
Expand All @@ -21,12 +17,11 @@ api.post("/path", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
};
});

/// https://github.com/winglang/wing/issues/3342
if (util.env("WING_TARGET") != "tf-aws") {
test "http.post and http.fetch can preform a call to an api" {

test "http.post and http.fetch can preform a call to an api" {
let url = api.url + "/path";
let response: http.Response = http.post(url, headers: { "content-type" => "application/json" }, body: Json.stringify(body));
let fetchResponse: http.Response = http.post(url, method: http_POST, headers: { "content-type" => "application/json" }, body: Json.stringify(body));
let fetchResponse: http.Response = http.post(url, method: http.HttpMethod.POST, headers: { "content-type" => "application/json" }, body: Json.stringify(body));

assert(response.body == Json.stringify(body));
assert(response.status == 200);
Expand All @@ -35,6 +30,4 @@ if (util.env("WING_TARGET") != "tf-aws") {
assert(fetchResponse.body == Json.stringify(body));
assert(fetchResponse.status == 200);
assert(fetchResponse.url == url);

}
}
14 changes: 4 additions & 10 deletions examples/tests/sdk_tests/api/put.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ bring cloud;
bring http;
bring util;

// https://github.com/winglang/wing/issues/3049
let http_PUT = http.HttpMethod.PUT;
let api_PUT = cloud.HttpMethod.PUT;

let api = new cloud.Api();

let body = Json {"cat": "Tion"};
Expand All @@ -14,7 +10,7 @@ let _id = "12345";

api.put("/path/{id}/nn/{user}", inflight (req: cloud.ApiRequest): cloud.ApiResponse => {
let path = "/path/${_id}/nn/${user}";
assert(req.method == api_PUT);
assert(req.method == cloud.HttpMethod.PUT);
assert(req.vars?.get("id") == _id);
assert(req.vars?.get("user") == user);
assert(req.path == path);
Expand All @@ -28,12 +24,11 @@ api.put("/path/{id}/nn/{user}", inflight (req: cloud.ApiRequest): cloud.ApiRespo
};
});

/// https://github.com/winglang/wing/issues/3342
if (util.env("WING_TARGET") != "tf-aws") {
test "http.put and http.fetch can preform a call to an api" {

test "http.put and http.fetch can preform a call to an api" {
let url = "${api.url}/path/${_id}/nn/${user}";
let response: http.Response = http.put(url, headers: { "content-type" => "application/json" }, body: Json.stringify(body));
let fetchResponse: http.Response = http.put(url, method: http_PUT, headers: { "content-type" => "application/json" }, body: Json.stringify(body));
let fetchResponse: http.Response = http.put(url, method: http.HttpMethod.PUT, headers: { "content-type" => "application/json" }, body: Json.stringify(body));


assert(response.headers.get("content-type") == "application/json; charset=utf-8");
Expand All @@ -45,6 +40,5 @@ if (util.env("WING_TARGET") != "tf-aws") {
assert(fetchResponse.body == _id);
assert(fetchResponse.status == 200);
assert(fetchResponse.url == url);
}
}

2 changes: 1 addition & 1 deletion libs/wingsdk/src/target-sim/api.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function isApiResponse(response: unknown): response is ApiResponse {
function transformRequest(req: express.Request): ApiRequest {
return {
headers: sanitizeParamLikeObject(req.headers),
body: req.body,
body: Object.keys(req.body).length > 0 ? req.body : "",
method: parseHttpMethod(req.method),
path: req.path,
query: sanitizeParamLikeObject(req.query as any),
Expand Down
6 changes: 5 additions & 1 deletion libs/wingsdk/src/target-tf-aws/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Api extends cloud.Api {
}

public get url(): string {
return this.api.stage.invokeUrl;
return this.api.url;
}

/**
Expand Down Expand Up @@ -367,6 +367,7 @@ export class Api extends cloud.Api {
* Encapsulates the API Gateway REST API as a abstraction for Terraform.
*/
class WingRestApi extends Construct {
public readonly url: string;
public readonly api: ApiGatewayRestApi;
public readonly stage: ApiGatewayStage;
private readonly deployment: ApiGatewayDeployment;
Expand Down Expand Up @@ -414,6 +415,9 @@ class WingRestApi extends Construct {
stageName: STAGE_NAME,
deploymentId: this.deployment.id,
});

//should be exported from here, otherwise won't be mapped to the right token
this.url = this.stage.invokeUrl;
}

/**
Expand Down
Loading

0 comments on commit 024225b

Please sign in to comment.