@@ -73,6 +73,23 @@ const decodeAndSerializeBody = async (body: CompletedBody, headers: Headers): Pr
73
73
}
74
74
} ;
75
75
76
+ const serverSideRuleBodySerializer = async ( body : CompletedBody , headers : Headers ) => {
77
+ const encoded = body . buffer . toString ( 'base64' ) ;
78
+ const result = await decodeAndSerializeBody ( body , headers ) ;
79
+ if ( result === false ) { // No decoding required - no-op.
80
+ return { encoded } ;
81
+ } else if ( result . decodingError !== undefined ) { // Failed decoding - we just return the error message.
82
+ return { encoded, decodingError : result . decodingError } ;
83
+ } else if ( result . decoded ) { // Success - we return both formats to the client
84
+ return { encoded, decoded : result . decoded . toString ( 'base64' ) } ;
85
+ } else {
86
+ throw new UnreachableCheck ( result ) ;
87
+ }
88
+ }
89
+
90
+ // messageBodyDecoding === 'None' => Just send encoded body as base64
91
+ const noopRuleBodySerializer = ( body : CompletedBody ) => body . buffer . toString ( 'base64' )
92
+
76
93
export function buildAdminServerModel (
77
94
mockServer : MockttpServer ,
78
95
stream : Duplex ,
@@ -86,20 +103,8 @@ export function buildAdminServerModel(
86
103
87
104
const ruleDeserializationOptions : MockttpDeserializationOptions = {
88
105
bodySerializer : messageBodyDecoding === 'server-side'
89
- ? async ( body , headers ) => {
90
- const encoded = body . buffer . toString ( 'base64' ) ;
91
- const result = await decodeAndSerializeBody ( body , headers ) ;
92
- if ( result === false ) { // No decoding required - no-op.
93
- return { encoded } ;
94
- } else if ( result . decodingError !== undefined ) { // Failed decoding - we just return the error message.
95
- return { encoded, decodingError : result . decodingError } ;
96
- } else if ( result . decoded ) { // Success - we return both formats to the client
97
- return { encoded, decoded : result . decoded . toString ( 'base64' ) } ;
98
- } else {
99
- throw new UnreachableCheck ( result ) ;
100
- }
101
- }
102
- : ( body ) => body . buffer . toString ( 'base64' ) , // 'None' = just send encoded body (as base64).
106
+ ? serverSideRuleBodySerializer
107
+ : noopRuleBodySerializer ,
103
108
ruleParams
104
109
} ;
105
110
@@ -173,6 +178,9 @@ export function buildAdminServerModel(
173
178
return request . body . buffer ;
174
179
} ,
175
180
decodedBody : async ( request : CompletedRequest ) => {
181
+ if ( messageBodyDecoding === 'none' ) {
182
+ throw new Error ( 'Decoded body requested, but messageBodyDecoding is set to "none"' ) ;
183
+ }
176
184
return ( await decodeAndSerializeBody ( request . body , request . headers ) )
177
185
|| { } ; // No decoding required
178
186
}
@@ -183,6 +191,9 @@ export function buildAdminServerModel(
183
191
return response . body . buffer ;
184
192
} ,
185
193
decodedBody : async ( response : CompletedResponse ) => {
194
+ if ( messageBodyDecoding === 'none' ) {
195
+ throw new Error ( 'Decoded body requested, but messageBodyDecoding is set to "none"' ) ;
196
+ }
186
197
return ( await decodeAndSerializeBody ( response . body , response . headers ) )
187
198
|| { } ; // No decoding required
188
199
}
0 commit comments