1
+ /* node:coverage disable */
2
+
3
+ const {
4
+ Server,
5
+ InputRequest,
6
+ JsonInputRequest,
7
+ OutputResponse,
8
+ JsonOutputResponse,
9
+ Endpoints
10
+ } = require ( '../../js' ) . server ;
11
+ const { describe, it, before, after} = require ( 'node:test' ) ;
12
+ const assert = require ( 'node:assert' ) ;
13
+ const http = require ( 'node:http' ) ;
14
+
15
+ const testBody = { value : 'value' , queryValue : 'otherQueryValue' } ;
16
+
17
+ const serverConfig = new Server (
18
+ http ,
19
+ new JsonInputRequest ( new InputRequest ( ) ) ,
20
+ new JsonOutputResponse ( new OutputResponse ( ) ) ,
21
+ new Endpoints ( [
22
+ {
23
+ route ( ) {
24
+ return {
25
+ method : 'GET' ,
26
+ path : '/test'
27
+ } ;
28
+ } ,
29
+ handle ( request ) {
30
+ return {
31
+ statusCode : 200 ,
32
+ body : JSON . stringify ( { queryKey : request . query ( ) . get ( 'queryKey' ) } )
33
+ } ;
34
+ }
35
+ } ,
36
+ {
37
+ route ( ) {
38
+ return {
39
+ method : 'POST' ,
40
+ path : '/test'
41
+ } ;
42
+ } ,
43
+ handle ( request ) {
44
+ return {
45
+ statusCode : 201 ,
46
+ body : JSON . stringify ( request . body ( ) )
47
+ } ;
48
+ }
49
+ } ,
50
+ ] ) ,
51
+ { port : 8081 }
52
+ ) ;
53
+
54
+
55
+ describe ( 'JSON server' , async ( ) => {
56
+ let serverInstance ;
57
+ before ( async ( ) => {
58
+ serverInstance = await serverConfig . start ( ) ;
59
+ } ) ;
60
+ after ( async ( ) => await serverInstance . stop ( ) ) ;
61
+
62
+ await it ( 'should be started' , async ( ) => {
63
+ await assert . doesNotReject ( ( ) => fetch ( 'http://localhost:8081' ) ,
64
+ { message : 'fetch failed' } ) ;
65
+ } ) ;
66
+
67
+ await it ( 'should return 501' , async ( ) => {
68
+ const response = await fetch ( 'http://localhost:8081/test0' ,
69
+ { method : 'GET' } ) ;
70
+ const body = await ( await response . blob ( ) ) . text ( ) ;
71
+
72
+ assert . strictEqual ( response . status , 501 ) ;
73
+ assert . strictEqual ( response . headers . get ( 'Content-Type' ) . includes ( 'application/json' ) , true ) ;
74
+ assert . strictEqual ( body , 'There are no handler for request.' ) ;
75
+ } ) ;
76
+
77
+ await it ( 'should return 200 and queryValue in body' , async ( ) => {
78
+ const response = await fetch ( 'http://localhost:8081/test?queryKey=queryValue' ,
79
+ { method : 'GET' } ) ;
80
+ const body = await response . json ( ) ;
81
+
82
+ assert . strictEqual ( response . status , 200 ) ;
83
+ assert . strictEqual ( response . headers . get ( 'Content-Type' ) . includes ( 'application/json' ) , true ) ;
84
+ assert . deepStrictEqual ( body , { queryKey : 'queryValue' } ) ;
85
+ } ) ;
86
+
87
+ await it ( 'should return 400, cause content-type is not application/json' , async ( ) => {
88
+ const response = await fetch ( 'http://localhost:8081/test' ,
89
+ {
90
+ method : 'POST' ,
91
+ headers : { 'content-type' : 'application/notJson' }
92
+ } ) ;
93
+ const body = await ( await response . blob ( ) ) . text ( ) ;
94
+
95
+ assert . strictEqual ( response . status , 400 ) ;
96
+ assert . strictEqual ( response . headers . get ( 'Content-Type' ) . includes ( 'application/json' ) , true ) ;
97
+ assert . strictEqual ( body , 'Wrong content-type. Only application/json accepted.' ) ;
98
+ } ) ;
99
+
100
+ await it ( 'should return 400, cause content-type is not set' , async ( ) => {
101
+ const response = await fetch ( 'http://localhost:8081/test' ,
102
+ { method : 'POST' } ) ;
103
+ const body = await ( await response . blob ( ) ) . text ( ) ;
104
+
105
+ assert . strictEqual ( response . status , 400 ) ;
106
+ assert . strictEqual ( response . headers . get ( 'Content-Type' ) . includes ( 'application/json' ) , true ) ;
107
+ assert . strictEqual ( body , 'Wrong content-type. Only application/json accepted.' ) ;
108
+ } ) ;
109
+
110
+ await it ( 'should return 201 and test body' , async ( ) => {
111
+ const response = await fetch ( 'http://localhost:8081/test?queryKey=queryValue' ,
112
+ {
113
+ method : 'POST' ,
114
+ body : JSON . stringify ( testBody ) ,
115
+ headers : { 'content-type' : 'application/json' }
116
+ } ) ;
117
+ const body = await response . json ( ) ;
118
+
119
+ assert . strictEqual ( response . status , 201 ) ;
120
+ assert . strictEqual ( response . headers . get ( 'Content-Type' ) . includes ( 'application/json' ) , true ) ;
121
+ assert . deepStrictEqual ( body , testBody ) ;
122
+ } ) ;
123
+ } ) ;
0 commit comments