Skip to content

Commit

Permalink
fix: url shortener not compiling (backport #3485) (#3491)
Browse files Browse the repository at this point in the history
Co-authored-by: Elad Ben-Israel <[email protected]>
Co-authored-by: Uri Bar <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2023
1 parent 6a71510 commit 38e271c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
12 changes: 0 additions & 12 deletions examples/proposed/url-shortener.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,3 @@ exports.makeId = function() {

return id;
}

exports.fetch = async function(url, method, body) {
const response = await fetch(url, {
method,
body: body ? JSON.stringify(body) : undefined,
headers: {
"Content-Type": "application/json"
}
});
const text = await response.text();
return text;
}
40 changes: 20 additions & 20 deletions examples/proposed/url-shortener.w
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
bring cloud;
bring http;

class Utils {
extern "./url-shortener.js" inflight makeId(): str;
extern "./url-shortener.js" inflight fetch(url: str, method: str, body: Json?): str;
class Extern {
extern "./url-shortener.js" static inflight makeId(): str;
}
let utils = new Utils();

class UrlShortener {
urlLookup: cloud.Bucket;
Expand Down Expand Up @@ -42,7 +41,7 @@ class UrlShortener {
return id;
}

let newId = utils.makeId();
let newId = Extern.makeId();

// (transaction 1)
this.urlLookup.put(url, newId);
Expand Down Expand Up @@ -107,7 +106,7 @@ class UrlShortenerApi {
status: 302,
headers: Map<str>{
"Content-Type" => "text/xml",
Location => fullUrl
"Location" => fullUrl
}
};
} else {
Expand All @@ -130,29 +129,30 @@ let urlShortenerApi = new UrlShortenerApi(urlShortener);
let TEST_URL = "https://news.ycombinator.com/";

test "shorten url" {
let response = utils.fetch("${urlShortenerApi.api.url}/create", "POST", Json { url: TEST_URL });
let newUrl = str.fromJson(Json.parse(response).get("shortenedUrl"));
let response = http.post("${urlShortenerApi.api.url}/create", body: Json.stringify({ url: TEST_URL }));
let newUrl = str.fromJson(Json.parse(response.body ?? "").get("shortenedUrl"));
assert(newUrl.startsWith(urlShortenerApi.api.url));
}

test "shorten url twice" {
let response1 = utils.fetch("${urlShortenerApi.api.url}/create", "POST", Json { url: TEST_URL });
let newUrl1 = str.fromJson(Json.parse(response1).get("shortenedUrl"));

let response2 = utils.fetch("${urlShortenerApi.api.url}/create", "POST", Json { url: TEST_URL });
let newUrl2 = str.fromJson(Json.parse(response2).get("shortenedUrl"));
let response1 = http.post("${urlShortenerApi.api.url}/create", body: Json.stringify({ url: TEST_URL }));
let newUrl1 = Json.tryParse(response1.body ?? "")?.get("shortenedUrl")?.asStr();
let response2 = http.post("${urlShortenerApi.api.url}/create", body: Json.stringify({ url: TEST_URL }));
let newUrl2 = Json.tryParse(response2.body ?? "")?.get("shortenedUrl")?.asStr();
assert(newUrl1 == newUrl2);
}

test "redirect sends to correct page" {
let createResponse = utils.fetch("${urlShortenerApi.api.url}/create", "POST", Json { url: TEST_URL });
let newUrl = str.fromJson(Json.parse(createResponse).get("shortenedUrl"));

let redirectedResponse = utils.fetch(newUrl, "GET");
assert(redirectedResponse.contains("<title>Hacker News</title>"));
let createResponse = http.post("${urlShortenerApi.api.url}/create", body: Json.stringify({ url: TEST_URL }));
if let newUrl = Json.tryParse(createResponse.body ?? "")?.get("shortenedUrl")?.asStr() {
let redirectedResponse = http.get(newUrl);
assert(redirectedResponse.body?.contains("<title>Hacker News</title>") ?? false);
} else {
assert(false);
}
}

test "invalid short url" {
let response = utils.fetch("${urlShortenerApi.api.url}/u/invalid", "GET");
assert(response.contains("url not found"));
let response = http.get("${urlShortenerApi.api.url}/u/invalid");
assert(response.body?.contains("url not found") ?? false);
}

0 comments on commit 38e271c

Please sign in to comment.