(expectation, toSanitize)
| 250 | }; |
| 251 | |
| 252 | const transformRecordedData = (expectation, toSanitize) => { |
| 253 | const requestBodySanitizer = httpRequest => { |
| 254 | let body; |
| 255 | if (httpRequest.body && httpRequest.body.type === 'JSON' && httpRequest.body.json) { |
| 256 | const bodyObject = JSON.parse(httpRequest.body.json); |
| 257 | if (bodyObject.encoding === 'base64') { |
| 258 | // sanitize encoded data |
| 259 | const decodedBody = Buffer.from(bodyObject.content, 'base64').toString('binary'); |
| 260 | const sanitizedContent = sanitizeString(decodedBody, toSanitize); |
| 261 | const sanitizedEncodedContent = Buffer.from(sanitizedContent, 'binary').toString('base64'); |
| 262 | bodyObject.content = sanitizedEncodedContent; |
| 263 | body = JSON.stringify(bodyObject); |
| 264 | } else { |
| 265 | body = httpRequest.body.json; |
| 266 | } |
| 267 | } else if (httpRequest.body && httpRequest.body.type === 'STRING' && httpRequest.body.string) { |
| 268 | body = httpRequest.body.string; |
| 269 | } else if ( |
| 270 | httpRequest.body && |
| 271 | httpRequest.body.type === 'BINARY' && |
| 272 | httpRequest.body.base64Bytes |
| 273 | ) { |
| 274 | body = { |
| 275 | encoding: 'base64', |
| 276 | content: httpRequest.body.base64Bytes, |
| 277 | contentType: httpRequest.body.contentType, |
| 278 | }; |
| 279 | } |
| 280 | return body; |
| 281 | }; |
| 282 | |
| 283 | const responseBodySanitizer = (httpRequest, httpResponse) => { |
| 284 | let responseBody = null; |
| 285 | if (httpResponse.body && httpResponse.body.string) { |
| 286 | responseBody = httpResponse.body.string; |
| 287 | } else if ( |
| 288 | httpResponse.body && |
| 289 | httpResponse.body.type === 'BINARY' && |
| 290 | httpResponse.body.base64Bytes |
| 291 | ) { |
| 292 | responseBody = { |
| 293 | encoding: 'base64', |
| 294 | content: httpResponse.body.base64Bytes, |
| 295 | }; |
| 296 | } else if (httpResponse.body) { |
| 297 | responseBody = httpResponse.body; |
| 298 | } |
| 299 | |
| 300 | // replace recorded user with fake one |
| 301 | if ( |
| 302 | responseBody && |
| 303 | httpRequest.path === '/2.0/user' && |
| 304 | httpRequest.headers.Host.includes('api.bitbucket.org') |
| 305 | ) { |
| 306 | responseBody = JSON.stringify(FAKE_OWNER_USER); |
| 307 | } |
| 308 | |
| 309 | return responseBody; |
no outgoing calls
no test coverage detected