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 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' ) ;
910 const ctx = {
10- mode : 'record ' ,
11+ mode : 'test ' ,
1112 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+
1441 } ;
1542 createExecutionContext ( ctx )
43+
1644 const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
1745 const url = 'https://api.keploy.io/healthz' ;
1846 const options = {
1947 method : 'GET' ,
2048 } ;
2149 const response = await wrappedFetch ( url , options ) ;
2250 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 ) ;
3056 } ) ;
3157
32- it ( 'should return mocked response in test mode' , async ( ) => {
58+ it ( 'should return mocked response in test mode - case 2 ' , async ( ) => {
3359 const mockResponse = new Response ( 'mocked response' ) ;
3460 const ctx = {
3561 mode : 'test' ,
@@ -42,12 +68,12 @@ describe('wrappedNodeFetch', () => {
4268 Spec : {
4369 Metadata : {
4470 name : 'node-fetch' ,
45- url : 'https://api.keploy.io/healthz ' ,
71+ url : 'https://api.keploy.io/status ' ,
4672 options : { method : 'GET' } ,
4773 type : 'HTTP_CLIENT' ,
4874 } ,
4975 Req : {
50- URL : 'https://api.keploy.io/healthz ' ,
76+ URL : 'https://api.keploy.io/status ' ,
5177 Body : '' ,
5278 Header : { } ,
5379 Method : 'GET' ,
@@ -61,12 +87,13 @@ describe('wrappedNodeFetch', () => {
6187 } ,
6288 ] ,
6389 deps : [ ] ,
90+
6491
6592 } ;
6693 createExecutionContext ( ctx )
6794
6895 const wrappedFetch = ( wrappedNodeFetch ( fetch ) as any ) . bind ( { fetch } ) ;
69- const url = 'https://api.keploy.io/healthz ' ;
96+ const url = 'https://api.keploy.io/status ' ;
7097 const options = {
7198 method : 'GET' ,
7299 } ;
@@ -78,20 +105,58 @@ describe('wrappedNodeFetch', () => {
78105 const mocks = updatedctx . mocks . length ;
79106 expect ( mocks ) . toBe ( 0 ) ;
80107 } ) ;
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' ;
87145 const options = {
88146 method : 'GET' ,
89147 } ;
90148 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 ) ;
93158 } ) ;
94-
159+
95160 it ( 'should call fetch function with correct arguments in off mode' , async ( ) => {
96161 const mockFetch = jest . fn ( ) . mockResolvedValueOnce ( new Response ( ) ) ;
97162 const ctx = {
@@ -112,4 +177,29 @@ describe('wrappedNodeFetch', () => {
112177 expect ( mockFetch ) . toHaveBeenCalledWith ( url , options ) ;
113178 expect ( response ) . toBeInstanceOf ( Response ) ;
114179 } ) ;
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