diff options
author | Hailey <me@haileyok.com> | 2024-06-27 11:31:24 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-27 11:31:24 -0700 |
commit | d5ca95233e3f8dd545fddb54a1f182d5a2e354f8 (patch) | |
tree | 5412dbdd5997cd88ad6b2ece3659398a208bc4c4 /bskylink/tests | |
parent | fff3ae8f359f496de3165d9d15c7135fc4269916 (diff) | |
download | voidsky-d5ca95233e3f8dd545fddb54a1f182d5a2e354f8.tar.zst |
offer a json response for grabbing short links (#4671)
Diffstat (limited to 'bskylink/tests')
-rw-r--r-- | bskylink/tests/index.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/bskylink/tests/index.ts b/bskylink/tests/index.ts index 51449c21b..c5604c7a1 100644 --- a/bskylink/tests/index.ts +++ b/bskylink/tests/index.ts @@ -56,6 +56,26 @@ describe('link service', async () => { ) }) + it('returns json object with url when requested', async () => { + const link = await getLink('/start/did:example:carol/zzz/') + const [status, json] = await getJsonRedirect(link) + assert.strictEqual(status, 200) + assert(json.url) + const url = new URL(json.url) + assert.strictEqual(url.pathname, '/start/did:example:carol/zzz') + }) + + it('returns 404 for unknown link when requesting json', async () => { + const [status, json] = await getJsonRedirect( + 'https://test.bsky.link/unknown', + ) + assert(json.error) + assert(json.message) + assert.strictEqual(status, 404) + assert.strictEqual(json.error, 'NotFound') + assert.strictEqual(json.message, 'Link not found') + }) + async function getRedirect(link: string): Promise<[number, string]> { const url = new URL(link) const base = new URL(baseUrl) @@ -70,6 +90,25 @@ describe('link service', async () => { return [res.status, res.headers.get('location') ?? ''] } + async function getJsonRedirect( + link: string, + ): Promise<[number, {url?: string; error?: string; message?: string}]> { + const url = new URL(link) + const base = new URL(baseUrl) + url.protocol = base.protocol + url.host = base.host + const res = await fetch(url, { + redirect: 'manual', + headers: {accept: 'application/json,text/html'}, + }) + assert( + res.headers.get('content-type')?.startsWith('application/json'), + 'content type was not json', + ) + const json = await res.json() + return [res.status, json] + } + async function getLink(path: string): Promise<string> { const res = await fetch(new URL('/link', baseUrl), { method: 'post', |