@@ -16,48 +16,99 @@ const page = `<!DOCTYPE HTML>
16
16
</html>`
17
17
18
18
describe ( 'HTML' , ( ) => {
19
- it ( 'auto return html' , async ( ) => {
20
- const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( ) => page )
21
-
22
- const res = await app . handle ( req ( '/' ) )
23
- expect ( await res . text ( ) ) . toBe ( page )
24
- expect ( res . headers . get ( 'Content-Type' ) ) . toContain ( 'text/html' )
25
- } )
26
-
27
- it ( 'manual return html' , async ( ) => {
28
- const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( { html } ) => html ( page ) )
29
-
30
- const res = await app . handle ( req ( '/' ) )
31
- expect ( await res . text ( ) ) . toBe ( page )
32
- expect ( res . headers . get ( 'Content-Type' ) ) . toContain ( 'text/html' )
33
- } )
34
-
35
- it ( 'inherits header' , async ( ) => {
36
- const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( { html, set } ) => {
37
- set . headers . Server = 'Elysia'
38
-
39
- return html ( "<h1>Hi</h1>" )
40
- } )
41
-
42
- const res = await app . handle ( req ( '/' ) )
43
- expect ( res . headers . get ( 'Server' ) ) . toBe ( 'Elysia' )
44
- } )
45
-
46
- it ( 'return any html tag' , async ( ) => {
47
- const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( ) => `<html>Hello World</html>` )
48
-
49
- const res = await app . handle ( req ( '/' ) )
50
- expect ( res . headers . get ( 'Content-type' ) ) . toContain ( 'text/html; charset=utf8' )
51
- } )
52
-
53
- it ( 'consistently identifies html content' , async ( ) => {
54
- const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( ) => `<h1>Hi</h1>` )
55
-
56
- let res = await app . handle ( req ( '/' ) )
57
- expect ( res . headers . get ( 'Content-type' ) ) . toContain ( 'text/html; charset=utf8' )
58
- res = await app . handle ( req ( '/' ) )
59
- expect ( res . headers . get ( 'Content-type' ) ) . toContain ( 'text/html; charset=utf8' )
60
- res = await app . handle ( req ( '/' ) )
61
- expect ( res . headers . get ( 'Content-type' ) ) . toContain ( 'text/html; charset=utf8' )
62
- } )
19
+ it ( 'auto return html' , async ( ) => {
20
+ const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( ) => page )
21
+
22
+ const res = await app . handle ( req ( '/' ) )
23
+ expect ( await res . text ( ) ) . toBe ( page )
24
+ expect ( res . headers . get ( 'Content-Type' ) ) . toContain ( 'text/html' )
25
+ } )
26
+
27
+ it ( 'manual return html' , async ( ) => {
28
+ const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( { html } ) => html ( page ) )
29
+
30
+ const res = await app . handle ( req ( '/' ) )
31
+ expect ( await res . text ( ) ) . toBe ( page )
32
+ expect ( res . headers . get ( 'Content-Type' ) ) . toContain ( 'text/html' )
33
+ } )
34
+
35
+ it ( 'inherits header' , async ( ) => {
36
+ const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( { html, set } ) => {
37
+ set . headers . Server = 'Elysia'
38
+ return html ( '<h1>Hi</h1>' )
39
+ } )
40
+
41
+ const res = await app . handle ( req ( '/' ) )
42
+ expect ( res . headers . get ( 'Server' ) ) . toBe ( 'Elysia' )
43
+ } )
44
+
45
+ it ( 'return any html tag' , async ( ) => {
46
+ const app = new Elysia ( )
47
+ . use ( html ( ) )
48
+ . get ( '/' , ( ) => `<html>Hello World</html>` )
49
+
50
+ const res = await app . handle ( req ( '/' ) )
51
+ expect ( res . headers . get ( 'Content-type' ) ) . toContain (
52
+ 'text/html; charset=utf8'
53
+ )
54
+ } )
55
+
56
+ it ( 'consistently identifies html content' , async ( ) => {
57
+ const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( ) => `<h1>Hi</h1>` )
58
+
59
+ let res = await app . handle ( req ( '/' ) )
60
+ expect ( res . headers . get ( 'Content-type' ) ) . toContain (
61
+ 'text/html; charset=utf8'
62
+ )
63
+ res = await app . handle ( req ( '/' ) )
64
+ expect ( res . headers . get ( 'Content-type' ) ) . toContain (
65
+ 'text/html; charset=utf8'
66
+ )
67
+ res = await app . handle ( req ( '/' ) )
68
+ expect ( res . headers . get ( 'Content-type' ) ) . toContain (
69
+ 'text/html; charset=utf8'
70
+ )
71
+ } )
72
+ } )
73
+
74
+ describe ( 'HTML vs No html - header' , ( ) => {
75
+ it ( 'inherits header plain response when using the html plugin' , async ( ) => {
76
+ const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( { set } ) => {
77
+ set . headers . Server = 'Elysia'
78
+ return 'Hello'
79
+ } )
80
+ const res = await app . handle ( req ( '/' ) )
81
+ expect ( res . headers . get ( 'Server' ) ) . toBe ( 'Elysia' )
82
+ } )
83
+
84
+ it ( 'inherits header plain response not using the html plugin' , async ( ) => {
85
+ const app = new Elysia ( ) . get ( '/' , ( { set } ) => {
86
+ set . headers . Server = 'Elysia'
87
+ return 'Hello'
88
+ } )
89
+ const res = await app . handle ( req ( '/' ) )
90
+ expect ( res . headers . get ( 'Server' ) ) . toBe ( 'Elysia' )
91
+ expect ( res . headers . get ( 'Content-Type' ) ) . toBe ( null )
92
+ } )
93
+
94
+ it ( 'inherits header json response when using the html plugin' , async ( ) => {
95
+ const app = new Elysia ( ) . use ( html ( ) ) . get ( '/' , ( { set } ) => {
96
+ set . headers . Server = 'Elysia'
97
+ return { Hello : 1 }
98
+ } )
99
+ const res = await app . handle ( req ( '/' ) )
100
+ expect ( res . headers . get ( 'Server' ) ) . toBe ( 'Elysia' )
101
+ } )
102
+
103
+ it ( 'inherits header json response not using the html plugin' , async ( ) => {
104
+ const app = new Elysia ( ) . get ( '/' , ( { set } ) => {
105
+ set . headers . Server = 'Elysia'
106
+ return { Hello : 1 }
107
+ } )
108
+ const res = await app . handle ( req ( '/' ) )
109
+ expect ( res . headers . get ( 'Server' ) ) . toBe ( 'Elysia' )
110
+ expect ( res . headers . get ( 'Content-Type' ) ) . toBe (
111
+ 'application/json;charset=utf-8'
112
+ )
113
+ } )
63
114
} )
0 commit comments