Skip to content

Commit 485415e

Browse files
test: added tests
1 parent 568637e commit 485415e

File tree

4 files changed

+316
-0
lines changed

4 files changed

+316
-0
lines changed

test/html.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { Elysia } from 'elysia'
3+
import { html } from '../src'
4+
5+
function request(path: string) {
6+
return new Request(`http://localhost${path}`)
7+
}
8+
9+
function handler() {
10+
return (
11+
`
12+
<!DOCTYPE html>
13+
<html lang="en">
14+
<head>
15+
<title>Hello World</title>
16+
</head>
17+
<body>
18+
<h1>Hello World</h1>
19+
</body>
20+
</html>
21+
`
22+
)
23+
}
24+
25+
describe('Jsx html', () => {
26+
it('auto return html', async () => {
27+
const app = new Elysia().use(html()).get('/', handler)
28+
const res = await app.handle(request('/'))
29+
expect(await res.text()).toBe(handler())
30+
expect(res.headers.get('Content-Type')).toContain('text/html')
31+
})
32+
})
33+
34+
describe('HTML', () => {
35+
it('manual return html', async () => {
36+
const app = new Elysia()
37+
.use(html())
38+
.get('/', ({ html }) => html(handler()))
39+
40+
const res = await app.handle(request('/'))
41+
expect(await res.text()).toBe(handler())
42+
expect(res.headers.get('Content-Type')).toContain('text/html')
43+
})
44+
45+
it('inherits header', async () => {
46+
const app = new Elysia().use(html()).get('/', ({ html, set }) => {
47+
set.headers.Server = 'Elysia'
48+
return html(`<h1>Hi</h1>`)
49+
})
50+
51+
const res = await app.handle(request('/'))
52+
expect(res.headers.get('Server')).toBe('Elysia')
53+
})
54+
55+
it('return any html tag', async () => {
56+
const app = new Elysia()
57+
.use(html())
58+
.get('/', () => `<html>Hello World</html>`)
59+
60+
const res = await app.handle(request('/'))
61+
expect(res.headers.get('Content-type')).toContain(
62+
'text/html; charset=utf8'
63+
)
64+
})
65+
66+
it('consistently identifies html content', async () => {
67+
const app = new Elysia().use(html()).get('/', () => `<h1></h1>`)
68+
69+
let res = await app.handle(request('/'))
70+
expect(res.headers.get('Content-type')).toContain(
71+
'text/html; charset=utf8'
72+
)
73+
res = await app.handle(request('/'))
74+
expect(res.headers.get('Content-type')).toContain(
75+
'text/html; charset=utf8'
76+
)
77+
res = await app.handle(request('/'))
78+
expect(res.headers.get('Content-type')).toContain(
79+
'text/html; charset=utf8'
80+
)
81+
})
82+
})

test/is-html.test.tsx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { isHtml } from '../src'
3+
4+
describe('isHtml', () => {
5+
it('detects html', () => {
6+
expect(isHtml('<h1>Hi</h1>')).toBe(true)
7+
expect(isHtml('<html></html>')).toBe(true)
8+
expect(isHtml('<!doctype html>')).toBe(true)
9+
expect(isHtml('<!DOCTYPE html>')).toBe(true)
10+
expect(isHtml('<html lang="en"></html>')).toBe(true)
11+
expect(isHtml('<html></html><h1>Hi</h1>')).toBe(true)
12+
expect(isHtml('<!doctype html><h1>Hi</h1>')).toBe(true)
13+
expect(isHtml('<!DOCTYPE html><h1>Hi</h1>')).toBe(true)
14+
expect(isHtml('<html lang="en"><h1>Hi</h1></html>')).toBe(true)
15+
16+
// Should trim() by default
17+
expect(isHtml(' <h1>Hi</h1> ')).toBe(true)
18+
})
19+
20+
it('detects html with JSX', () => {
21+
expect(isHtml(<h1>Hi</h1>)).toBe(true)
22+
expect(isHtml(<html></html>)).toBe(true)
23+
expect(isHtml(<html lang="en"></html>)).toBe(true)
24+
expect(
25+
isHtml(
26+
<>
27+
<html></html>
28+
<h1>Hi</h1>
29+
</>
30+
)
31+
).toBe(true)
32+
expect(
33+
isHtml(
34+
<html lang="en">
35+
<h1>Hi</h1>
36+
</html>
37+
)
38+
).toBe(true)
39+
})
40+
41+
it('does not detects html on non strings', () => {
42+
expect(isHtml()).toBe(false)
43+
expect(isHtml(undefined)).toBe(false)
44+
expect(isHtml(null)).toBe(false)
45+
expect(isHtml(0)).toBe(false)
46+
expect(isHtml(1)).toBe(false)
47+
expect(isHtml({ a: 1 })).toBe(false)
48+
expect(isHtml({ html: '<div></div>' })).toBe(false)
49+
expect(isHtml([])).toBe(false)
50+
expect(isHtml(true)).toBe(false)
51+
expect(isHtml(false)).toBe(false)
52+
})
53+
54+
it('does not detects html on non html strings', () => {
55+
expect(isHtml('')).toBe(false)
56+
expect(isHtml('Hi')).toBe(false)
57+
expect(isHtml('Hi <h1>Hi</h1>')).toBe(false)
58+
expect(isHtml('<h1>Hi</h1> Hi')).toBe(false)
59+
expect(isHtml('<h1> invalid <h1')).toBe(false)
60+
expect(isHtml('h1> invalid <h1>')).toBe(false)
61+
})
62+
})

test/jsx.test.tsx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { Elysia } from 'elysia'
3+
import { html } from '../src'
4+
5+
function request(path: string) {
6+
return new Request(`http://localhost${path}`)
7+
}
8+
9+
function handler() {
10+
return (
11+
<>
12+
{'<!DOCTYPE HTML>'}
13+
<html lang="en">
14+
<head>
15+
<title>Hello World</title>
16+
</head>
17+
<body>
18+
<h1>Hello World</h1>
19+
</body>
20+
</html>
21+
</>
22+
)
23+
}
24+
25+
describe('Jsx html', () => {
26+
it('auto return html', async () => {
27+
const app = new Elysia().use(html()).get('/', handler)
28+
const res = await app.handle(request('/'))
29+
expect(await res.text()).toBe(handler())
30+
expect(res.headers.get('Content-Type')).toContain('text/html')
31+
})
32+
})
33+
34+
describe('HTML', () => {
35+
it('manual return html', async () => {
36+
const app = new Elysia()
37+
.use(html())
38+
.get('/', ({ html }) => html(handler()))
39+
40+
const res = await app.handle(request('/'))
41+
expect(await res.text()).toBe(handler())
42+
expect(res.headers.get('Content-Type')).toContain('text/html')
43+
})
44+
45+
it('inherits header', async () => {
46+
const app = new Elysia().use(html()).get('/', ({ html, set }) => {
47+
set.headers.Server = 'Elysia'
48+
return html(<h1>Hi</h1>)
49+
})
50+
51+
const res = await app.handle(request('/'))
52+
expect(res.headers.get('Server')).toBe('Elysia')
53+
})
54+
55+
it('return any html tag', async () => {
56+
const app = new Elysia()
57+
.use(html())
58+
.get('/', () => <html>Hello World</html>)
59+
60+
const res = await app.handle(request('/'))
61+
expect(res.headers.get('Content-type')).toContain(
62+
'text/html; charset=utf8'
63+
)
64+
})
65+
66+
it('consistently identifies html content', async () => {
67+
const app = new Elysia().use(html()).get('/', () => <h1></h1>)
68+
69+
let res = await app.handle(request('/'))
70+
expect(res.headers.get('Content-type')).toContain(
71+
'text/html; charset=utf8'
72+
)
73+
res = await app.handle(request('/'))
74+
expect(res.headers.get('Content-type')).toContain(
75+
'text/html; charset=utf8'
76+
)
77+
res = await app.handle(request('/'))
78+
expect(res.headers.get('Content-type')).toContain(
79+
'text/html; charset=utf8'
80+
)
81+
})
82+
})

test/options.test.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { Elysia } from 'elysia'
3+
import { html, isHtml, isTagHtml } from '../src'
4+
5+
function request(path: string) {
6+
return new Request(`http://localhost${path}`)
7+
}
8+
9+
describe('options', () => {
10+
it('tests default contentType', async () => {
11+
const app = new Elysia().use(html()).get('/', () => '<div></div>')
12+
const res = await app.handle(request('/'))
13+
14+
expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf8')
15+
})
16+
17+
it('tests custom contentType', async () => {
18+
const ct = 'NOT TEXT HTML'
19+
20+
const app = new Elysia()
21+
.use(html({ contentType: ct }))
22+
.get('/', () => '<div></div>')
23+
24+
const res = await app.handle(request('/'))
25+
26+
expect(res.headers.get('Content-Type')).toBe(ct)
27+
})
28+
29+
it('tests default autoDetect', async () => {
30+
const app = new Elysia().use(html()).get('/', () => '<div></div>')
31+
const res = await app.handle(request('/'))
32+
33+
expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf8')
34+
})
35+
36+
it('tests false autoDetect', async () => {
37+
const app = new Elysia()
38+
.use(html({ autoDetect: false }))
39+
.get('/', () => '<div></div>')
40+
.get('/html', ({ html }) => html('<div></div>'))
41+
42+
const res = await app.handle(request('/'))
43+
expect(res.headers.get('Content-Type')).toBeNull()
44+
45+
const htmlRes = await app.handle(request('/html'))
46+
expect(htmlRes.headers.get('Content-Type')).toBe(
47+
'text/html; charset=utf8'
48+
)
49+
})
50+
51+
it('tests default autoDoctype', async () => {
52+
const app = new Elysia()
53+
.use(html())
54+
.get('/', ({ html }) => html('<html></html>'))
55+
const res = await app.handle(request('/'))
56+
57+
expect(await res.text()).toBe('<!doctype html><html></html>')
58+
})
59+
60+
it('tests false autoDoctype', async () => {
61+
const app = new Elysia()
62+
.use(html({ autoDoctype: false }))
63+
.get('/', ({ html }) => html('<html></html>'))
64+
const res = await app.handle(request('/'))
65+
66+
expect(await res.text()).toBe('<html></html>')
67+
})
68+
69+
it('tests true autoDoctype with non html tag', async () => {
70+
const app = new Elysia()
71+
.use(html({ autoDoctype: true }))
72+
.get('/', ({ html }) => html('<not-html></not-html>'))
73+
const res = await app.handle(request('/'))
74+
75+
expect(await res.text()).toBe('<not-html></not-html>')
76+
})
77+
78+
it('tests full autoDoctype', async () => {
79+
const app = new Elysia()
80+
.use(html({ autoDoctype: 'full' }))
81+
.get('/invalid', () => '<not-html></not-html>')
82+
.get('/valid', () => '<html></html>')
83+
84+
const invalid = await app.handle(request('/invalid'))
85+
expect(await invalid.text()).toBe('<not-html></not-html>')
86+
87+
const valid = await app.handle(request('/valid'))
88+
expect(await valid.text()).toBe('<!doctype html><html></html>')
89+
})
90+
})

0 commit comments

Comments
 (0)