11import { wrappedNodeFetch } from '../integrations/octokit/require' ;
22import { Response } from 'node-fetch' ;
33import fetch from 'node-fetch' ;
4- import { createExecutionContext , getExecutionContext } from '../src/context' ;
4+ import { createExecutionContext , getExecutionContext } from '../src/context' ;
55import { HTTP } from '../src/keploy' ;
66
77describe ( 'wrappedNodeFetch' , ( ) => {
8- it ( 'should return mocked response in test mode - case 1' , async ( ) => {
9- const mockResponse = new Response ( 'mocked response' ) ;
8+ it ( 'should call fetch function with correct arguments in record mode' , async ( ) => {
109 const ctx = {
11- mode : 'test ' ,
10+ mode : 'record ' ,
1211 testId : 'testId' ,
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-
12+ mocks : [ ] ,
13+ deps : [ ] ,
4114 } ;
4215 createExecutionContext ( ctx )
43-
4416 const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
4517 const url = 'https://api.keploy.io/healthz' ;
4618 const options = {
4719 method : 'GET' ,
4820 } ;
4921 const response = await wrappedFetch ( url , options ) ;
5022 const updatedctx = getExecutionContext ( ) . context ;
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 ) ;
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 ) ;
5630 } ) ;
5731
58- it ( 'should return mocked response in test mode - case 2 ' , async ( ) => {
32+ it ( 'should return mocked response in test mode' , async ( ) => {
5933 const mockResponse = new Response ( 'mocked response' ) ;
6034 const ctx = {
6135 mode : 'test' ,
@@ -68,12 +42,12 @@ describe('wrappedNodeFetch', () => {
6842 Spec : {
6943 Metadata : {
7044 name : 'node-fetch' ,
71- url : 'https://api.keploy.io/status ' ,
45+ url : 'https://api.keploy.io/healthz ' ,
7246 options : { method : 'GET' } ,
7347 type : 'HTTP_CLIENT' ,
7448 } ,
7549 Req : {
76- URL : 'https://api.keploy.io/status ' ,
50+ URL : 'https://api.keploy.io/healthz ' ,
7751 Body : '' ,
7852 Header : { } ,
7953 Method : 'GET' ,
@@ -92,7 +66,7 @@ describe('wrappedNodeFetch', () => {
9266 createExecutionContext ( ctx )
9367
9468 const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
95- const url = 'https://api.keploy.io/status ' ;
69+ const url = 'https://api.keploy.io/healthz ' ;
9670 const options = {
9771 method : 'GET' ,
9872 } ;
@@ -104,52 +78,20 @@ describe('wrappedNodeFetch', () => {
10478 const mocks = updatedctx . mocks . length ;
10579 expect ( mocks ) . toBe ( 0 ) ;
10680 } ) ;
107- it ( 'should record an HTTP request in record mode' , async ( ) => {
108- const ctx = {
109- mode : 'record' ,
110- testId : 'testId' ,
111- mocks : [ ] ,
112- deps : [ ] ,
113- } ;
114- createExecutionContext ( ctx )
115- const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
116- const url = 'https://api.example.com/test' ;
117- const options = {
118- method : 'POST' ,
119- headers : {
120- 'Content-Type' : 'application/json' ,
121- } ,
122- body : JSON . stringify ( { data : 'test' } ) ,
123- } ;
124- const response = await wrappedFetch ( url , options ) ;
125- const updatedctx = getExecutionContext ( ) . context ;
126- const responseBody = await response . text ( ) ;
127- const recordedOutput = updatedctx . mocks [ 0 ] . Spec . Res . Body ;
128- expect ( response ) . toBeInstanceOf ( Response ) ;
129- expect ( updatedctx . mocks . length ) . toBeGreaterThan ( 0 ) ;
130- expect ( updatedctx . deps . length ) . toBeGreaterThan ( 0 ) ;
131- expect ( response ) . toHaveProperty ( 'body' ) ;
132- expect ( responseBody ) . toEqual ( recordedOutput ) ;
133- } ) ;
13481
135- it ( 'should handle an HTTP error 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://api.example.com/error' ;
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' ;
14587 const options = {
14688 method : 'GET' ,
14789 } ;
148- await expect ( wrappedFetch ( url , options ) ) . rejects . toThrow ( 'Not Found' ) ;
149- const updatedctx = getExecutionContext ( ) . context ;
150- expect ( updatedctx . mocks . length ) . toBeGreaterThan ( 0 ) ;
151- expect ( updatedctx . deps . length ) . toBeGreaterThan ( 0 ) ;
90+ const response = await wrappedFetch ( url , options ) ;
91+ expect ( consoleSpy ) . toHaveBeenCalledWith ( 'keploy context is not present to mock dependencies' ) ;
92+ expect ( response ) . toBeUndefined ( ) ;
15293 } ) ;
94+
15395 it ( 'should call fetch function with correct arguments in off mode' , async ( ) => {
15496 const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
15597 const ctx = {
@@ -170,49 +112,4 @@ describe('wrappedNodeFetch', () => {
170112 expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
171113 expect ( response ) . toBeInstanceOf ( Response ) ;
172114 } ) ;
173-
174- it ( 'should return an error if fetch is called in off mode with a mock' , async ( ) => {
175- const mockFetch = jest . fn ( ) . mockResolvedValue ( new Response ( ) ) ;
176- const consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ) ;
177- const ctx = {
178- context : 'off' ,
179- testId : 'testId' ,
180- mocks : [
181- {
182- Version : 'V1_BETA2' ,
183- Name : 'testId' ,
184- Kind : HTTP ,
185- Spec : {
186- Metadata : {
187- name : 'node-fetch' ,
188- url : 'https://api.keploy.io/healthz' ,
189- options : { method : 'GET' } ,
190- type : 'HTTP_CLIENT' ,
191- } ,
192- Req : {
193- URL : 'https://api.keploy.io/healthz' ,
194- Body : '' ,
195- Header : { } ,
196- Method : 'GET' ,
197- } ,
198- Res : {
199- StatusCode : 200 ,
200- Header : { 'content-type' : { Value : [ 'text/plain' ] } } ,
201- Body : 'mocked response' ,
202- } ,
203- } ,
204- } ,
205- ] ,
206- deps : [ ] ,
207- } ;
208- createExecutionContext ( ctx )
209- const wrappedFetch = ( wrappedNodeFetch ( mockFetch ) as any ) . bind ( { fetch : mockFetch } ) ;
210- const url = 'https://api.keploy.io/healthz' ;
211- const options = {
212- method : 'GET' ,
213- } ;
214- const response = await wrappedFetch ( url , options ) ;
215- expect ( consoleSpy ) . toHaveBeenCalledWith ( 'Cannot mock dependencies in off mode' ) ;
216- expect ( response ) . toBeUndefined ( ) ;
217- } ) ;
218- } ) ;
115+ } ) ;
0 commit comments