mockttp
Version:
Mock HTTP server for testing HTTP clients and stubbing webservices
113 lines • 4.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withSerializedBodyReader = withSerializedBodyReader;
exports.withDeserializedBodyReader = withDeserializedBodyReader;
exports.deserializeBodyReader = deserializeBodyReader;
exports.withSerializedCallbackBuffers = withSerializedCallbackBuffers;
exports.withDeserializedCallbackBuffers = withDeserializedCallbackBuffers;
const buffer_1 = require("buffer");
const _ = require("lodash");
const base64_arraybuffer_1 = require("base64-arraybuffer");
const util_1 = require("@httptoolkit/util");
const buffer_utils_1 = require("../util/buffer-utils");
const request_utils_1 = require("../util/request-utils");
const serialization_1 = require("./serialization");
// Server-side: serialize a body, so it can become a CompletedBody on the client side
async function withSerializedBodyReader(input, bodySerializer) {
return {
...input,
body: await bodySerializer(input.body, input.headers)
};
}
// Client-side: turn a serialized body back into a CompletedBody (body to be exposed for convenient access)
function withDeserializedBodyReader(input) {
let encodedBodyString;
let decodedBodyString;
let decodedBodyError;
// We don't need to know the expected serialization format: we can detect it, and just
// use what we get sensibly regardless:
if (typeof input.body === 'string') {
// If the body is a string, it is a base64-encoded string
encodedBodyString = input.body;
}
else if (typeof input.body === 'object') {
encodedBodyString = input.body.encoded;
decodedBodyString = input.body.decoded;
decodedBodyError = input.body.decodingError;
}
else {
throw new util_1.UnreachableCheck(input.body);
}
return {
...input,
body: deserializeBodyReader(encodedBodyString, decodedBodyString, decodedBodyError, input.headers),
};
}
function deserializeBodyReader(encodedBodyString, decodedBodyString, decodingError, headers) {
const encodedBody = (0, serialization_1.deserializeBuffer)(encodedBodyString);
const decodedBody = decodedBodyString ? (0, serialization_1.deserializeBuffer)(decodedBodyString) : undefined;
const decoder = !!decodedBody
// If the server provides a pre-decoded body, we use it.
? async () => decodedBody
// If not, all encoded bodies are non-decodeable on the client side. This should
// only happen with messageBodyDecoding = 'none' (or with v4+ clients + <v4 servers).
: failIfDecodingRequired.bind(null, decodingError);
return (0, request_utils_1.buildBodyReader)(encodedBody, headers, decoder);
}
function failIfDecodingRequired(errorMessage, buffer, headers) {
if (!headers['content-encoding'] || headers['content-encoding'] === 'identity') {
return buffer;
}
const error = errorMessage
? new Error(`Decoding error (${headers['content-encoding']}): ${errorMessage}`)
: new Error("Can't read encoded message body without server-side decoding");
console.warn(error.message);
throw error;
}
/**
* Serialize a callback result (callback handlers, BeforeRequest/Response etc)
* to transform all the many possible buffer formats into either base64-encoded
* buffer data, or undefined.
*/
function withSerializedCallbackBuffers(input) {
let serializedBody;
if (!input.body) {
serializedBody = undefined;
}
else if (_.isString(input.body)) {
serializedBody = (0, serialization_1.serializeBuffer)((0, buffer_utils_1.asBuffer)(input.body));
}
else if (_.isBuffer(input.body)) {
serializedBody = (0, serialization_1.serializeBuffer)(input.body);
}
else if (_.isArrayBuffer(input.body) || _.isTypedArray(input.body)) {
serializedBody = (0, base64_arraybuffer_1.encode)(input.body);
}
else if ((0, request_utils_1.isMockttpBody)(input.body)) {
serializedBody = (0, serialization_1.serializeBuffer)((0, buffer_utils_1.asBuffer)(input.body.buffer));
}
return {
...input,
body: serializedBody,
rawBody: input.rawBody
? (0, serialization_1.serializeBuffer)((0, buffer_utils_1.asBuffer)(input.rawBody))
: undefined
};
}
/**
* Deserialize a callback result (callback handlers, BeforeRequest/Response etc)
* to build buffer data (or undefined) from the base64-serialized data
* produced by withSerializedCallbackBuffers
*/
function withDeserializedCallbackBuffers(input) {
return {
...input,
body: input.body !== undefined
? buffer_1.Buffer.from(input.body, 'base64')
: undefined,
rawBody: input.rawBody !== undefined
? buffer_1.Buffer.from(input.rawBody, 'base64')
: undefined
};
}
//# sourceMappingURL=body-serialization.js.map