| 214 | |
| 215 | const transformRecordedData = (expectation, toSanitize) => { |
| 216 | const requestBodySanitizer = httpRequest => { |
| 217 | let body; |
| 218 | if (httpRequest.body && httpRequest.body.type === 'JSON' && httpRequest.body.json) { |
| 219 | const bodyObject = |
| 220 | typeof httpRequest.body.json === 'string' |
| 221 | ? JSON.parse(httpRequest.body.json) |
| 222 | : httpRequest.body.json; |
| 223 | |
| 224 | if (bodyObject.encoding === 'base64') { |
| 225 | // sanitize encoded data |
| 226 | const decodedBody = Buffer.from(bodyObject.content, 'base64').toString('binary'); |
| 227 | const sanitizedContent = sanitizeString(decodedBody, toSanitize); |
| 228 | const sanitizedEncodedContent = Buffer.from(sanitizedContent, 'binary').toString('base64'); |
| 229 | bodyObject.content = sanitizedEncodedContent; |
| 230 | body = JSON.stringify(bodyObject); |
| 231 | } else { |
| 232 | body = JSON.stringify(bodyObject); |
| 233 | } |
| 234 | } else if (httpRequest.body && httpRequest.body.type === 'STRING' && httpRequest.body.string) { |
| 235 | body = sanitizeString(httpRequest.body.string, toSanitize); |
| 236 | } else if (httpRequest.body) { |
| 237 | const str = |
| 238 | typeof httpRequest.body !== 'string' ? JSON.stringify(httpRequest.body) : httpRequest.body; |
| 239 | body = sanitizeString(str, toSanitize); |
| 240 | } |
| 241 | return body; |
| 242 | }; |
| 243 | |
| 244 | const responseBodySanitizer = (httpRequest, httpResponse) => { |
| 245 | let responseBody = null; |
no test coverage detected