-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathjsonp.test.ts
More file actions
44 lines (38 loc) · 1.72 KB
/
jsonp.test.ts
File metadata and controls
44 lines (38 loc) · 1.72 KB
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
import { describe, it } from 'vitest'
import { jsonp } from '../../packages/jsonp/src/index'
import { InitAppAndTest } from '../../test_helpers/initAppAndTest'
describe('jsonp', () => {
it('when no callback is defined', async () => {
const { fetch } = InitAppAndTest((req, res) => jsonp(req, res)({ jsonp: 'value' }))
await fetch('/').expect('Content-Type', 'application/json; charset=utf-8').expect(200, '{"jsonp":"value"}')
})
it('should preserve existing Content-Type header when already set', async () => {
const { fetch } = InitAppAndTest((req, res) => {
res.set('Content-Type', 'application/custom+json')
jsonp(req, res)({ jsonp: 'value' })
})
// Should preserve the pre-set Content-Type and not override with application/json
await fetch('/').expect('Content-Type', 'application/custom+json; charset=utf-8').expect(200, '{"jsonp":"value"}')
})
it('should use callback with jsonp when defined', async () => {
const { fetch } = InitAppAndTest((req, res) => jsonp(req, res)({ jsonp: 'value' }))
await fetch('/?callback=something')
.expect('Content-Type', 'text/javascript; charset=utf-8')
.expect(200, '/**/ typeof something === \'function\' && something({"jsonp":"value"});')
})
it('should change <>& into UTF', async () => {
const { fetch } = InitAppAndTest((req, res) =>
jsonp(req, res)({ jsonp: '<value>& value' }, { escape: true, spaces: 1 })
)
await fetch('/?callback=something')
.expect('Content-Type', 'text/javascript; charset=utf-8')
.expect(
200,
"/**/ typeof something === 'function' && something({" +
'\n' +
' "jsonp": "\\u003cvalue\\u003e\\u0026 value"' +
'\n' +
'});'
)
})
})