1
1
/* eslint-disable @typescript-eslint/ban-ts-comment */
2
- import Client from '../../../client/request' ;
2
+ import Client from '../../../src/ client/request' ;
3
3
import createServer , { port } from '../../resources/mockServer' ;
4
4
import { Server as HttpServer } from 'http' ;
5
5
import { Server as HttpsServer } from 'https' ;
6
- import Response from '../../../core/response' ;
7
- import fetch , { Headers , Request , Response as FetchResponse } from 'node-fetch' ;
6
+ import Response from '../../../src/core/response' ;
8
7
9
-
10
- if ( ! globalThis . fetch ) {
11
- // @ts -ignore
12
- globalThis . fetch = fetch ;
13
- // @ts -ignore
14
- globalThis . Headers = Headers ;
15
- // @ts -ignore
16
- globalThis . Request = Request ;
17
- // @ts -ignore
18
- globalThis . Response = FetchResponse ;
19
- }
8
+ import { afterAll , beforeAll , describe , expect , test } from 'vitest' ;
20
9
21
10
describe ( 'request' , ( ) => {
22
11
let server : HttpServer | HttpsServer ;
@@ -50,20 +39,22 @@ describe('request', () => {
50
39
one : 'two'
51
40
} ) . then ( function ( response : Response ) {
52
41
expect ( response . status ) . toEqual ( 200 ) ;
53
- expect ( response . body ) . toEqual ( JSON . stringify ( {
42
+ expect ( JSON . parse ( response . body as string ) ) . toEqual ( {
54
43
url : '/' ,
55
44
method : 'GET' ,
56
45
body : '' ,
57
46
headers : {
58
47
one : 'two' ,
59
48
accept : '*/*' ,
60
- 'user-agent' : 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ,
61
- 'accept-encoding' : 'gzip,deflate' ,
62
- connection : 'close' ,
49
+ 'user-agent' : 'node' ,
50
+ 'accept-encoding' : 'gzip, deflate' ,
51
+ 'accept-language' : '*' ,
52
+ 'sec-fetch-mode' : 'cors' ,
53
+ connection : 'keep-alive' ,
63
54
host : `127.0.0.1:${ port } ` ,
64
55
} ,
65
56
message : 'hello!'
66
- } ) ) ;
57
+ } ) ;
67
58
expect ( response . headers [ 'this-is' ] ) . toEqual ( 'for real' ) ;
68
59
} ) ) ;
69
60
} ) ;
@@ -78,21 +69,23 @@ describe('request', () => {
78
69
one : [ 'two' , 'three' ]
79
70
} ) . then ( function ( response : Response ) {
80
71
expect ( response . status ) . toEqual ( 200 ) ;
81
- expect ( response . body ) . toEqual ( JSON . stringify ( {
72
+ expect ( JSON . parse ( response . body as string ) ) . toEqual ( {
82
73
url : '/' ,
83
74
method : 'GET' ,
84
75
body : '' ,
85
76
headers : {
86
77
// node fetch doesn't seem to retain dupe arrays, this is what we get.
87
78
one : 'two, three' ,
88
79
accept : '*/*' ,
89
- 'user-agent' : 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)' ,
90
- 'accept-encoding' : 'gzip,deflate' ,
91
- connection : 'close' ,
80
+ 'user-agent' : 'node' ,
81
+ 'accept-encoding' : 'gzip, deflate' ,
82
+ 'accept-language' : '*' ,
83
+ connection : 'keep-alive' ,
92
84
host : `127.0.0.1:${ port } ` ,
85
+ 'sec-fetch-mode' : 'cors' ,
93
86
} ,
94
87
message : 'hello!'
95
- } ) ) ;
88
+ } ) ;
96
89
// I don't think this is correct for dupliate headers, it seems to be something node-fetch is doing,
97
90
// not sure if it's spec accurate: https://github.com/node-fetch/node-fetch/issues/771
98
91
expect ( response . headers [ 'cache-control' ] ) . toEqual ( 'no-cache, no-store' ) ;
@@ -114,7 +107,7 @@ describe('request', () => {
114
107
} ) . then ( function ( response : Response ) {
115
108
expect ( response . status ) . toEqual ( 200 ) ;
116
109
117
- expect ( response . body ) . toEqual ( JSON . stringify ( {
110
+ expect ( JSON . parse ( response . body as string ) ) . toMatchObject ( {
118
111
url : '/' ,
119
112
method : 'POST' ,
120
113
body : '{"yeah": "what"}' ,
@@ -123,13 +116,13 @@ describe('request', () => {
123
116
'content-type' : 'text/plain;charset=UTF-8' ,
124
117
accept : '*/*' ,
125
118
'content-length' : '16' ,
126
- 'user-agent' : 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch) ' ,
127
- 'accept-encoding' : 'gzip,deflate' ,
128
- connection : 'close ' ,
119
+ 'user-agent' : 'node' ,
120
+ 'accept-encoding' : 'gzip, deflate' ,
121
+ connection : 'keep-alive ' ,
129
122
host : `127.0.0.1:${ port } ` ,
130
123
} ,
131
124
message : 'hello!'
132
- } ) ) ;
125
+ } ) ;
133
126
134
127
expect ( response . headers [ 'content-type' ] ) . toEqual ( 'application/json' ) ;
135
128
} ) ) ;
0 commit comments