Skip to content

Commit 34bd799

Browse files
committed
Tighten up admin API handling of server-side decoding
This will now throw an error if you mix server-side & none handling (setting none for rules but then requesting server-side decoded data in events), since there's no good reason this should ever happen. This also refactors the actual rule body encoding logic out a bit for clarity.
1 parent cc7b286 commit 34bd799

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/admin/mockttp-admin-model.ts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,23 @@ const decodeAndSerializeBody = async (body: CompletedBody, headers: Headers): Pr
7373
}
7474
};
7575

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+
7693
export function buildAdminServerModel(
7794
mockServer: MockttpServer,
7895
stream: Duplex,
@@ -86,20 +103,8 @@ export function buildAdminServerModel(
86103

87104
const ruleDeserializationOptions: MockttpDeserializationOptions = {
88105
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,
103108
ruleParams
104109
};
105110

@@ -173,6 +178,9 @@ export function buildAdminServerModel(
173178
return request.body.buffer;
174179
},
175180
decodedBody: async (request: CompletedRequest) => {
181+
if (messageBodyDecoding === 'none') {
182+
throw new Error('Decoded body requested, but messageBodyDecoding is set to "none"');
183+
}
176184
return (await decodeAndSerializeBody(request.body, request.headers))
177185
|| {}; // No decoding required
178186
}
@@ -183,6 +191,9 @@ export function buildAdminServerModel(
183191
return response.body.buffer;
184192
},
185193
decodedBody: async (response: CompletedResponse) => {
194+
if (messageBodyDecoding === 'none') {
195+
throw new Error('Decoded body requested, but messageBodyDecoding is set to "none"');
196+
}
186197
return (await decodeAndSerializeBody(response.body, response.headers))
187198
|| {}; // No decoding required
188199
}

0 commit comments

Comments
 (0)