(expectation, toSanitize)
| 307 | }; |
| 308 | |
| 309 | const transformRecordedData = (expectation, toSanitize) => { |
| 310 | const requestBodySanitizer = httpRequest => { |
| 311 | let body; |
| 312 | if (httpRequest.body && httpRequest.body.type === 'JSON' && httpRequest.body.json) { |
| 313 | const bodyObject = |
| 314 | typeof httpRequest.body.json === 'string' |
| 315 | ? JSON.parse(httpRequest.body.json) |
| 316 | : httpRequest.body.json; |
| 317 | |
| 318 | if (bodyObject.encoding === 'base64') { |
| 319 | // sanitize encoded data |
| 320 | const decodedBody = Buffer.from(bodyObject.content, 'base64').toString('binary'); |
| 321 | const sanitizedContent = sanitizeString(decodedBody, toSanitize); |
| 322 | const sanitizedEncodedContent = Buffer.from(sanitizedContent, 'binary').toString('base64'); |
| 323 | bodyObject.content = sanitizedEncodedContent; |
| 324 | body = JSON.stringify(bodyObject); |
| 325 | } else { |
| 326 | body = JSON.stringify(bodyObject); |
| 327 | } |
| 328 | } else if (httpRequest.body && httpRequest.body.type === 'STRING' && httpRequest.body.string) { |
| 329 | body = httpRequest.body.string; |
| 330 | } else if (httpRequest.body) { |
| 331 | const str = |
| 332 | typeof httpRequest.body !== 'string' ? JSON.stringify(httpRequest.body) : httpRequest.body; |
| 333 | body = sanitizeString(str, toSanitize); |
| 334 | } |
| 335 | return body; |
| 336 | }; |
| 337 | |
| 338 | const responseBodySanitizer = (httpRequest, httpResponse) => { |
| 339 | let responseBody = null; |
| 340 | if (httpResponse.body && httpResponse.body.string) { |
| 341 | responseBody = httpResponse.body.string; |
| 342 | } else if ( |
| 343 | httpResponse.body && |
| 344 | httpResponse.body.type === 'BINARY' && |
| 345 | httpResponse.body.base64Bytes |
| 346 | ) { |
| 347 | responseBody = { |
| 348 | encoding: 'base64', |
| 349 | content: httpResponse.body.base64Bytes, |
| 350 | }; |
| 351 | } else if (httpResponse.body && httpResponse.body.json) { |
| 352 | responseBody = JSON.stringify(httpResponse.body.json); |
| 353 | } else { |
| 354 | responseBody = |
| 355 | typeof httpResponse.body === 'string' |
| 356 | ? httpResponse.body |
| 357 | : httpResponse.body && JSON.stringify(httpResponse.body); |
| 358 | } |
| 359 | |
| 360 | // replace recorded user with fake one |
| 361 | if ( |
| 362 | responseBody && |
| 363 | httpRequest.path === '/user' && |
| 364 | httpRequest.headers.Host.includes('api.github.com') |
| 365 | ) { |
| 366 | const parsed = JSON.parse(responseBody); |
no outgoing calls
no test coverage detected