1
+ /* node:coverage disable */
2
+
3
+ const { describe, it, before, after} = require ( 'node:test' ) ;
4
+ const assert = require ( 'node:assert' ) ;
5
+ const http = require ( 'node:http' ) ;
6
+
7
+ const {
8
+ OutputRequest,
9
+ InputResponse
10
+ } = require ( '../../../js/index' ) . client ;
11
+
12
+ const {
13
+ Server,
14
+ InputRequest,
15
+ OutputResponse,
16
+ Endpoints
17
+ } = require ( '../../../js' ) . server ;
18
+
19
+ const serverConfig = new Server (
20
+ http ,
21
+ new InputRequest ( ) ,
22
+ new OutputResponse ( ) ,
23
+ new Endpoints ( [
24
+ {
25
+ route ( ) {
26
+ return {
27
+ method : 'GET' ,
28
+ path : '/test'
29
+ } ;
30
+ } ,
31
+ handle ( ) {
32
+ return {
33
+ statusCode : 200
34
+ } ;
35
+ }
36
+ } ,
37
+ {
38
+ route ( ) {
39
+ return {
40
+ method : 'POST' ,
41
+ path : '/test'
42
+ } ;
43
+ } ,
44
+ handle ( ) {
45
+ return {
46
+ statusCode : 201 ,
47
+ body : 'test body'
48
+ } ;
49
+ }
50
+ } ,
51
+ ] ) ,
52
+ { port : 8090 }
53
+ ) ;
54
+
55
+ describe ( 'client' , 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 ( ( ) =>
64
+ new OutputRequest ( http , new InputResponse ( ) , {
65
+ url : 'http://localhost' , method : 'GET' , port : '8090'
66
+ } ) . send ( ) ,
67
+ { message : 'fetch failed' } ) ;
68
+
69
+ await assert . doesNotReject ( ( ) =>
70
+ new OutputRequest ( http , new InputResponse ( ) , {
71
+ port : '8090' , method : 'GET' , host : 'localhost'
72
+ } ) . send ( ) ,
73
+ { message : 'fetch failed' } ) ;
74
+ } ) ;
75
+
76
+ await it ( 'should return 501' , async ( ) => {
77
+ const response = await new OutputRequest ( http , new InputResponse ( ) , {
78
+ url : 'http://localhost:8090/no_test' , method : 'GET'
79
+ } ) . send ( ) ;
80
+
81
+ assert . strictEqual ( response . statusCode ( ) , 501 ) ;
82
+ assert . strictEqual ( response . body ( ) . toString ( ) , 'There are no handler for request.' ) ;
83
+ } ) ;
84
+
85
+ await it ( 'should return 200 and no body' , async ( ) => {
86
+ const response = await new OutputRequest ( http , new InputResponse ( ) , {
87
+ url : 'http://localhost:8090/test' , method : 'GET'
88
+ } ) . send ( ) ;
89
+
90
+ assert . strictEqual ( response . statusCode ( ) , 200 ) ;
91
+ assert . strictEqual ( response . body ( ) . length , 0 ) ;
92
+ } ) ;
93
+
94
+ await it ( 'should return 201 and test body' , async ( ) => {
95
+ const response = await new OutputRequest ( http , new InputResponse ( ) , {
96
+ url : 'http://localhost:8090/test' , method : 'POST' , body : 'test body'
97
+ } ) . send ( ) ;
98
+
99
+ assert . strictEqual ( response . statusCode ( ) , 201 ) ;
100
+ assert . strictEqual ( response . body ( ) . toString ( ) , 'test body' ) ;
101
+ } ) ;
102
+
103
+ await it ( 'should not fall, but body is not a string' , async ( ) => {
104
+ const response = await new OutputRequest ( http , new InputResponse ( ) , {
105
+ url : 'http://localhost:8090/test' , method : 'POST' , body : { }
106
+ } ) . send ( ) ;
107
+
108
+ assert . strictEqual ( response . statusCode ( ) , 201 ) ;
109
+ assert . strictEqual ( response . body ( ) . toString ( ) , 'test body' ) ;
110
+ } ) ;
111
+ } ) ;
0 commit comments