diff --git a/examples/proposed/url-shortener.js b/examples/proposed/url-shortener.js index 13336e06069..271bfbf55c0 100644 --- a/examples/proposed/url-shortener.js +++ b/examples/proposed/url-shortener.js @@ -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; -} diff --git a/examples/proposed/url-shortener.w b/examples/proposed/url-shortener.w index c6ca00bbebc..df8fa7e6217 100644 --- a/examples/proposed/url-shortener.w +++ b/examples/proposed/url-shortener.w @@ -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; @@ -42,7 +41,7 @@ class UrlShortener { return id; } - let newId = utils.makeId(); + let newId = Extern.makeId(); // (transaction 1) this.urlLookup.put(url, newId); @@ -107,7 +106,7 @@ class UrlShortenerApi { status: 302, headers: Map{ "Content-Type" => "text/xml", - Location => fullUrl + "Location" => fullUrl } }; } else { @@ -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("Hacker News")); + 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("Hacker News") ?? 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); }