diff options
author | 2022-03-07 15:36:22 -0600 | |
---|---|---|
committer | 2022-03-07 15:36:22 -0600 | |
commit | f18ee36dc0abdc5c8ec87734de7962966d16fe65 (patch) | |
tree | c01a7034186cb0bbe5e1d042f4a5dd09bad21ed5 /packages/webapi/test/fetch.js | |
parent | 10a9c3412b4f6e8607687a74eafdb150d3222047 (diff) | |
download | astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.tar.gz astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.tar.zst astro-f18ee36dc0abdc5c8ec87734de7962966d16fe65.zip |
Add `@astrojs/webapi` package (#2729)@astrojs/webapi@0.11.0
* chore: add @astrojs/webapi
* chore: update package.json
* fix: update file case
* fix: remove lowercase file
* chore: update tests to use mocha
* chore: update LICENSE
Diffstat (limited to 'packages/webapi/test/fetch.js')
-rw-r--r-- | packages/webapi/test/fetch.js | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/packages/webapi/test/fetch.js b/packages/webapi/test/fetch.js new file mode 100644 index 000000000..08d7c9297 --- /dev/null +++ b/packages/webapi/test/fetch.js @@ -0,0 +1,140 @@ +import { assert, test } from '../run/test.setup.js' +import { polyfill } from '../mod.js' + +test(() => { + return [ + { + name: 'Fetch functionality', + test() { + const target = {} + + polyfill(target) + + assert.equal(Reflect.has(target, 'fetch'), true) + assert.equal(typeof target.fetch, 'function') + }, + }, + { + name: 'Fetch with https', + async test() { + const target = {} + + polyfill(target) + + const { fetch } = target + + const response = await fetch('https://api.openbrewerydb.org/breweries') + + assert.equal(response.constructor, target.Response) + + const json = await response.json() + + assert.equal(Array.isArray(json), true) + }, + }, + { + name: 'Fetch with file', + async test() { + const target = {} + + polyfill(target) + + const { fetch } = target + + const url = new URL('../package.json', import.meta.url) + + const response = await fetch(url) + + assert.equal(response.constructor, target.Response) + + assert.equal(response.status, 200) + assert.equal(response.statusText, '') + assert.equal(response.headers.has('date'), true) + assert.equal(response.headers.has('content-length'), true) + assert.equal(response.headers.has('last-modified'), true) + + const json = await response.json() + + assert.equal(json.name, '@astrojs/webapi') + }, + }, + { + name: 'Fetch with missing file', + async test() { + const target = {} + + polyfill(target) + + const { fetch } = target + + const url = new URL('../missing.json', import.meta.url) + + const response = await fetch(url) + + assert.equal(response.constructor, target.Response) + + assert.equal(response.status, 404) + assert.equal(response.statusText, '') + assert.equal(response.headers.has('date'), true) + assert.equal(response.headers.has('content-length'), false) + assert.equal(response.headers.has('last-modified'), false) + }, + }, + { + name: 'Fetch with (file) Request', + async test() { + const target = {} + + polyfill(target) + + const { Request, fetch } = target + + const request = new Request(new URL('../package.json', import.meta.url)) + + const response = await fetch(request) + + assert.equal(response.constructor, target.Response) + + const json = await response.json() + + assert.equal(json.name, '@astrojs/webapi') + }, + }, + { + name: 'Fetch with relative file', + async test() { + const target = {} + + polyfill(target) + + const { fetch } = target + + const response = await fetch('package.json') + + const json = await response.json() + + assert.equal(json.name, '@astrojs/webapi') + }, + }, + { + name: 'Fetch with data', + async test() { + const target = {} + + polyfill(target) + + const { fetch } = target + + const jsonURI = `data:application/json,${encodeURIComponent(JSON.stringify({ + name: '@astrojs/webapi' + }))}` + + const response = await fetch(jsonURI) + + const json = await response.json() + + assert.equal(json.name, '@astrojs/webapi') + }, + }, + ] +}) |