Skip to content

Commit

Permalink
Merge pull request #216 from mschfh/only-display-text-content
Browse files Browse the repository at this point in the history
Only display textual Content-Types
  • Loading branch information
mishushakov committed May 1, 2024
2 parents 1b5f8e7 + 4554926 commit dea2774
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/lib/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ function renderSpaces (spaces: number) {
return ' '.repeat(spaces)
}

function shouldDisplayBody(contentType: string | undefined): boolean {
if (!contentType) {
// Treat empty content-type as displayable
return true;
}
const displayableTypes: RegExp[] = [
/^text\//, // Matches any MIME type starting with "text/"
/application\/(x-)?(json|xml|csv|javascript|ecmascript)/, // Matches both standard and non-standard textual application types
/application\/.*\+(json|xml)$/, // Matches MIME types that end with +json or +xml
];

// Normalize content type to handle cases with parameters like charset
const normalizedType = contentType.split(';')[0].trim();

return displayableTypes.some(pattern => pattern.test(normalizedType));
}

function renderHTTPRequest (request: HTTPStepRequest) {
const requestHeaders = request.headers ? Object.keys(request.headers).map(header => `${header}: ${request.headers ? request.headers[header] : ''}\n`) : ''
const requestBody = typeof request.body === 'string' ? '\n' + request.body : ''
Expand All @@ -37,7 +54,15 @@ function renderHTTPRequest (request: HTTPStepRequest) {

function renderHTTPResponse (response: HTTPStepResponse) {
const responseHeaders = response.headers ? Object.keys(response.headers).map(header => `${header}: ${response.headers ? response.headers[header] : ''}\n`) : ''
const responseBody = '\n' + Buffer.from(response.body).toString()
let responseBody
if (shouldDisplayBody(response.contentType || undefined) || process.env.STEPCI_DISPLAY_ALL_CONTENT_TYPES) {
responseBody = '\n' + Buffer.from(response.body).toString()
}

else {
responseBody = '\n[Response body not displayed due to Content-Type (Use STEPCI_DISPLAY_ALL_CONTENT_TYPES=1 to force display)]'
}

return `${response.protocol} ${response.status} ${response.statusText}\n${responseHeaders.toString().replaceAll(',', '')}${responseBody}`
}

Expand Down

0 comments on commit dea2774

Please sign in to comment.