1
1
import { wrappedNodeFetch } from '../integrations/octokit/require' ;
2
2
import { Response } from 'node-fetch' ;
3
3
import fetch from 'node-fetch' ;
4
- import { createExecutionContext , getExecutionContext } from '../src/context' ;
4
+ import { createExecutionContext , getExecutionContext } from '../src/context' ;
5
5
import { HTTP } from '../src/keploy' ;
6
6
7
7
describe ( 'wrappedNodeFetch' , ( ) => {
8
- it ( 'should call fetch function with correct arguments in record mode' , async ( ) => {
8
+ it ( 'should return mocked response in test mode - case 1' , async ( ) => {
9
+ const mockResponse = new Response ( 'mocked response' ) ;
9
10
const ctx = {
10
- mode : 'record ' ,
11
+ mode : 'test ' ,
11
12
testId : 'testId' ,
12
- mocks : [ ] ,
13
- deps : [ ] ,
13
+ mocks : [
14
+ {
15
+ Version : 'V1_BETA2' ,
16
+ Name : 'testId' ,
17
+ Kind : HTTP ,
18
+ Spec : {
19
+ Metadata : {
20
+ name : 'node-fetch' ,
21
+ url : 'https://api.keploy.io/healthz' ,
22
+ options : { method : 'GET' } ,
23
+ type : 'HTTP_CLIENT' ,
24
+ } ,
25
+ Req : {
26
+ URL : 'https://api.keploy.io/healthz' ,
27
+ Body : '' ,
28
+ Header : { } ,
29
+ Method : 'GET' ,
30
+ } ,
31
+ Res : {
32
+ StatusCode : 200 ,
33
+ Header : { 'content-type' : { Value : [ 'text/plain' ] } } ,
34
+ Body : 'mocked response' ,
35
+ } ,
36
+ } ,
37
+ } ,
38
+ ] ,
39
+ deps : [ ] ,
40
+
14
41
} ;
15
42
createExecutionContext ( ctx )
43
+
16
44
const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
17
45
const url = 'https://api.keploy.io/healthz' ;
18
46
const options = {
19
47
method : 'GET' ,
20
48
} ;
21
49
const response = await wrappedFetch ( url , options ) ;
22
50
const updatedctx = getExecutionContext ( ) . context ;
23
- const responseBody = await response . text ( ) ;
24
- const recordedOutput = updatedctx . mocks [ 0 ] . Spec . Res . Body ;
25
- expect ( response ) . toBeInstanceOf ( Response ) ;
26
- expect ( updatedctx . mocks . length ) . toBeGreaterThan ( 0 ) ;
27
- expect ( updatedctx . deps . length ) . toBeGreaterThan ( 0 ) ;
28
- expect ( response ) . toHaveProperty ( 'body' ) ;
29
- expect ( responseBody ) . toEqual ( recordedOutput ) ;
51
+ expect ( response . status ) . toEqual ( mockResponse . status ) ;
52
+ expect ( response . statusText ) . toEqual ( mockResponse . statusText ) ;
53
+
54
+ const mocks = updatedctx . mocks . length ;
55
+ expect ( mocks ) . toBe ( 0 ) ;
30
56
} ) ;
31
57
32
- it ( 'should return mocked response in test mode' , async ( ) => {
58
+ it ( 'should return mocked response in test mode - case 2 ' , async ( ) => {
33
59
const mockResponse = new Response ( 'mocked response' ) ;
34
60
const ctx = {
35
61
mode : 'test' ,
@@ -42,12 +68,12 @@ describe('wrappedNodeFetch', () => {
42
68
Spec : {
43
69
Metadata : {
44
70
name : 'node-fetch' ,
45
- url : 'https://api.keploy.io/healthz ' ,
71
+ url : 'https://api.keploy.io/status ' ,
46
72
options : { method : 'GET' } ,
47
73
type : 'HTTP_CLIENT' ,
48
74
} ,
49
75
Req : {
50
- URL : 'https://api.keploy.io/healthz ' ,
76
+ URL : 'https://api.keploy.io/status ' ,
51
77
Body : '' ,
52
78
Header : { } ,
53
79
Method : 'GET' ,
@@ -61,12 +87,13 @@ describe('wrappedNodeFetch', () => {
61
87
} ,
62
88
] ,
63
89
deps : [ ] ,
90
+
64
91
65
92
} ;
66
93
createExecutionContext ( ctx )
67
94
68
95
const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
69
- const url = 'https://api.keploy.io/healthz ' ;
96
+ const url = 'https://api.keploy.io/status ' ;
70
97
const options = {
71
98
method : 'GET' ,
72
99
} ;
@@ -78,20 +105,58 @@ describe('wrappedNodeFetch', () => {
78
105
const mocks = updatedctx . mocks . length ;
79
106
expect ( mocks ) . toBe ( 0 ) ;
80
107
} ) ;
81
-
82
- it ( 'should return undefined if execution context is not present in record mode' , async ( ) => {
83
- const mockFetch = jest . fn ( ) . mockResolvedValue ( new Response ( ) ) ;
84
- const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
85
- const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
86
- const url = 'https://api.keploy.io/healthz' ;
108
+ it ( 'should record an HTTP request in record mode' , async ( ) => {
109
+ const ctx = {
110
+ mode : 'record' ,
111
+ testId : 'testId' ,
112
+ mocks : [ ] ,
113
+ deps : [ ] ,
114
+ } ;
115
+ createExecutionContext ( ctx )
116
+ const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
117
+ const url = 'https://jsonplaceholder.typicode.com/posts' ;
118
+ const options = {
119
+ method : 'POST' ,
120
+ headers : {
121
+ 'Content-Type' : 'application/json' ,
122
+ } ,
123
+ body : JSON . stringify ( { data : 'test' } ) ,
124
+ } ;
125
+ const response = await wrappedFetch ( url , options ) ;
126
+ const updatedctx = getExecutionContext ( ) . context ;
127
+ const responseBody = await response . text ( ) ;
128
+ const recordedOutput = updatedctx . mocks [ 0 ] . Spec . Res . Body ;
129
+ expect ( response ) . toBeInstanceOf ( Response ) ;
130
+ expect ( updatedctx . mocks . length ) . toBeGreaterThan ( 0 ) ;
131
+ expect ( updatedctx . deps . length ) . toBeGreaterThan ( 0 ) ;
132
+ expect ( response ) . toHaveProperty ( 'body' ) ;
133
+ expect ( responseBody ) . toEqual ( recordedOutput ) ;
134
+ } ) ;
135
+ it ( 'should record a successful HTTP response with JSON body in record mode' , async ( ) => {
136
+ const ctx = {
137
+ mode : 'record' ,
138
+ testId : 'testId' ,
139
+ mocks : [ ] ,
140
+ deps : [ ] ,
141
+ } ;
142
+ createExecutionContext ( ctx ) ;
143
+ const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
144
+ const url = 'https://jsonplaceholder.typicode.com/todos/1' ;
87
145
const options = {
88
146
method : 'GET' ,
89
147
} ;
90
148
const response = await wrappedFetch ( url , options ) ;
91
- expect ( consoleSpy ) . toHaveBeenCalledWith ( 'keploy context is not present to mock dependencies' ) ;
92
- expect ( response ) . toBeUndefined ( ) ;
149
+ const json = await response . json ( ) ;
150
+ expect ( response . status ) . toBe ( 200 ) ;
151
+ expect ( json . userId ) . toBeDefined ( ) ;
152
+ expect ( json . id ) . toBeDefined ( ) ;
153
+ expect ( json . title ) . toBeDefined ( ) ;
154
+ expect ( json . completed ) . toBeDefined ( ) ;
155
+ const updatedctx = getExecutionContext ( ) . context ;
156
+ expect ( updatedctx . mocks . length ) . toBeGreaterThan ( 0 ) ;
157
+ expect ( updatedctx . deps . length ) . toBeGreaterThan ( 0 ) ;
93
158
} ) ;
94
-
159
+
95
160
it ( 'should call fetch function with correct arguments in off mode' , async ( ) => {
96
161
const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
97
162
const ctx = {
@@ -112,4 +177,29 @@ describe('wrappedNodeFetch', () => {
112
177
expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
113
178
expect ( response ) . toBeInstanceOf ( Response ) ;
114
179
} ) ;
115
- } ) ;
180
+
181
+ it ( 'should call fetch function with correct arguments in off mode' , async ( ) => {
182
+ const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
183
+ const ctx = {
184
+ mode : 'off' ,
185
+ testId : 'testId' ,
186
+ mocks : [ ] ,
187
+ deps : [ ] ,
188
+ } ;
189
+ createExecutionContext ( ctx ) ;
190
+
191
+ const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
192
+ const url = 'https://api.example.com/test' ;
193
+ const options = {
194
+ method : 'POST' ,
195
+ headers : {
196
+ 'Content-Type' : 'application/json' ,
197
+ } ,
198
+ body : JSON . stringify ( { data : 'test' } ) ,
199
+ } ;
200
+ const response = await wrappedFetch ( url , options ) ;
201
+
202
+ expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
203
+ expect ( response ) . toBeInstanceOf ( Response ) ;
204
+ } ) ;
205
+ } ) ;
0 commit comments