summaryrefslogtreecommitdiffstats
path: root/test/fetch/blob-uri.js
blob: f9db96c36b12445879b86e5d11ba051e25ee7819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict'

const { test } = require('tap')
const { fetch } = require('../..')
const { Blob } = require('buffer')

test('fetching blob: uris', async (t) => {
  const blobContents = 'hello world'
  /** @type {import('buffer').Blob} */
  let blob
  /** @type {string} */
  let objectURL

  t.beforeEach(() => {
    blob = new Blob([blobContents])
    objectURL = URL.createObjectURL(blob)
  })

  t.test('a normal fetch request works', async (t) => {
    const res = await fetch(objectURL)

    t.equal(blobContents, await res.text())
    t.equal(blob.type, res.headers.get('Content-Type'))
    t.equal(`${blob.size}`, res.headers.get('Content-Length'))
    t.end()
  })

  t.test('non-GET method to blob: fails', async (t) => {
    try {
      await fetch(objectURL, {
        method: 'POST'
      })
      t.fail('expected POST to blob: uri to fail')
    } catch (e) {
      t.ok(e, 'Got the expected error')
    } finally {
      t.end()
    }
  })

  // https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L36-L41
  t.test('fetching revoked URL should fail', async (t) => {
    URL.revokeObjectURL(objectURL)

    try {
      await fetch(objectURL)
      t.fail('expected revoked blob: url to fail')
    } catch (e) {
      t.ok(e, 'Got the expected error')
    } finally {
      t.end()
    }
  })

  // https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L28-L34
  t.test('works with a fragment', async (t) => {
    const res = await fetch(objectURL + '#fragment')

    t.equal(blobContents, await res.text())
    t.end()
  })

  // https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L52-L56
  t.test('Appending a query string to blob: url should cause fetch to fail', async (t) => {
    try {
      await fetch(objectURL + '?querystring')
      t.fail('expected ?querystring blob: url to fail')
    } catch (e) {
      t.ok(e, 'Got the expected error')
    } finally {
      t.end()
    }
  })

  // https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L58-L62
  t.test('Appending a path should cause fetch to fail', async (t) => {
    try {
      await fetch(objectURL + '/path')
      t.fail('expected /path blob: url to fail')
    } catch (e) {
      t.ok(e, 'Got the expected error')
    } finally {
      t.end()
    }
  })

  // https://github.com/web-platform-tests/wpt/blob/7b0ebaccc62b566a1965396e5be7bb2bc06f841f/FileAPI/url/resources/fetch-tests.js#L64-L70
  t.test('these http methods should fail', async (t) => {
    for (const method of ['HEAD', 'POST', 'DELETE', 'OPTIONS', 'PUT', 'CUSTOM']) {
      try {
        await fetch(objectURL, { method })
        t.fail(`${method} fetch should have failed`)
      } catch (e) {
        t.ok(e, `${method} blob url - test succeeded`)
      }
    }

    t.end()
  })
})