File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { findRule } from './utils/findRule.js' ;
22import { notifyClients } from './utils/notifyClients.js' ;
3+ import { mockResponse } from './utils/mockResponse.js' ;
34
45/**
56 * List of currently active mock rules.
@@ -57,10 +58,11 @@ export const handleFetch = async (event) => {
5758 notifyClients ( clients , rule , body ) ;
5859 } ) ;
5960
60- return new Response ( JSON . stringify ( rule . response ) , {
61- status : rule . status || 200 ,
62- headers : rule . responseHeaders || { "Content-Type" : "application/json" } ,
63- } ) ;
61+ return mockResponse (
62+ rule . response ,
63+ rule . status ?? 200 ,
64+ rule . responseHeaders
65+ ) ;
6466 } ) ( )
6567 ) ;
6668 }
Original file line number Diff line number Diff line change 1+
2+ /**
3+ * Mocks a Response object based on the given status and headers.
4+ * If the status code does not allow a body, the body will be null.
5+ * Otherwise, it will return the rule's response as a JSON string.
6+ * @param {unknown } response
7+ * @param {number } status
8+ * @param {Record<string, string> } [headers]
9+ * @returns {Response }
10+ */
11+ export const mockResponse = ( response , status , headers ) => {
12+ // Status codes that cannot have a body
13+ const statusCodesWithoutBody = [ 204 , 205 , 304 ] ;
14+ const shouldIncludeBody = ! statusCodesWithoutBody . includes ( status ) ;
15+
16+ const body = shouldIncludeBody ? JSON . stringify ( response ) : null ;
17+
18+ return new Response (
19+ body ,
20+ {
21+ status,
22+ headers : shouldIncludeBody
23+ ? ( headers || { "Content-Type" : "application/json" } )
24+ : ( headers || { } ) ,
25+ }
26+ ) ;
27+ } ;
Original file line number Diff line number Diff line change 1+ import { describe , it , expect , vi } from 'vitest' ;
2+ import { mockResponse } from '../../cli/utils/mockResponse.js' ;
3+
4+ describe ( 'mockResponse' , ( ) => {
5+ const responseBody = { message : 'ok' } ;
6+ it ( 'should create a Response with body for status 200' , ( ) => {
7+ const response = mockResponse ( responseBody , 200 , { 'Content-Type' : 'application/json' } ) ;
8+ expect ( response . status ) . toBe ( 200 ) ;
9+ expect ( response . headers . get ( 'Content-Type' ) ) . toBe ( 'application/json' ) ;
10+ return response . json ( ) . then ( ( data ) => {
11+ expect ( data ) . toEqual ( responseBody ) ;
12+ } ) ;
13+ } ) ;
14+
15+ it ( 'should create a Response without body for status 204' , ( ) => {
16+ const response = mockResponse ( responseBody , 204 , { 'Content-Type' : 'application/json' } ) ;
17+ expect ( response . status ) . toBe ( 204 ) ;
18+ expect ( response . headers . get ( 'Content-Type' ) ) . toBe ( 'application/json' ) ;
19+ return response . text ( ) . then ( ( data ) => {
20+ expect ( data ) . toBe ( '' ) ;
21+ } ) ;
22+ } ) ;
23+
24+ it ( 'should use default headers if none provided' , ( ) => {
25+ const response = mockResponse ( responseBody , 200 ) ;
26+ expect ( response . status ) . toBe ( 200 ) ;
27+ expect ( response . headers . get ( 'Content-Type' ) ) . toBe ( 'application/json' ) ;
28+ } ) ;
29+ } ) ;
You can’t perform that action at this time.
0 commit comments