diff --git a/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/url-shortener-microservice.md b/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/url-shortener-microservice.md index cea5d654bc..6a55cf35cf 100644 --- a/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/url-shortener-microservice.md +++ b/curriculum/challenges/english/05-apis-and-microservices/apis-and-microservices-projects/url-shortener-microservice.md @@ -22,18 +22,82 @@ Start this project on Repl.it using { + const url = getUserInput('url'); + const urlVariable = Date.now(); + const res = await fetch(url + '/api/shorturl/new/', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `url=https://timestamp-microservice.freecodecamp.rocks/${urlVariable}` + }); + + if (res.ok) { + const { short_url, original_url } = await res.json(); + assert.isNotNull(short_url); + assert.match(original_url, new RegExp(`https://timestamp-microservice.freecodecamp.rocks/${urlVariable}`)); + } else { + throw new Error(`${res.status} ${res.statusText}`); + } + } + " + + - text: When you visit `/api/shorturl/`, you will be redirected to the original URL. + testString: "async getUserInput => { + const url = getUserInput('url'); + const urlVariable = Date.now(); + let shortenedUrlVariable; + + const postResponse = await fetch(url + '/api/shorturl/new/', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `url=https://timestamp-microservice.freecodecamp.rocks/${urlVariable}` + }); + + if (postResponse.ok) { + const { short_url } = await postResponse.json(); + shortenedUrlVariable = short_url; + } else { + throw new Error(`${postResponse.status} ${postResponse.statusText}`); + } + + const getResponse = await fetch(url + '/api/shorturl/' + shortenedUrlVariable); + + if (getResponse) { + const { redirected, url } = getResponse; + + assert.isTrue(redirected); + assert.strictEqual(url, `https://timestamp-microservice.freecodecamp.rocks/${urlVariable}`); + } else { + throw new Error(`${getResponse.status} ${getResponse.statusText}`); + } + } + " + + - text: "If you pass an invalid URL that doesn't follow the valid `http://www.example.com` format, the JSON response will contain `{ error: 'invalid url' }`" + testString: "async getUserInput => { + const url = getUserInput('url'); + const res = await fetch(url + '/api/shorturl/new/', { + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + body: `url=ftp:/john-doe.org` + }); + + if (res.ok) { + const { error } = await res.json(); + assert.isNotNull(error); + assert.strictEqual(error.toLowerCase(), 'invalid url'); + } else { + throw new Error(`${res.status} ${res.statusText}`); + } + } + " ```