Skip to content

Commit

Permalink
fix(response-body): remove download button when content is empty (#8579)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimír Gorej <vladimir.gorej@gmail.com>
kowalczyk-krzysztof and char0n authored Apr 26, 2023

Partially verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
1 parent 043d5ea commit cdfc4de
Showing 3 changed files with 26 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/core/components/response-body.jsx
Original file line number Diff line number Diff line change
@@ -59,11 +59,13 @@ export default class ResponseBody extends React.PureComponent {
url = url || ""

if (
/^application\/octet-stream/i.test(contentType) ||
(headers["Content-Disposition"] && (/attachment/i).test(headers["Content-Disposition"])) ||
(headers["content-disposition"] && (/attachment/i).test(headers["content-disposition"])) ||
(headers["Content-Description"] && (/File Transfer/i).test(headers["Content-Description"])) ||
(headers["content-description"] && (/File Transfer/i).test(headers["content-description"]))) {
(/^application\/octet-stream/i.test(contentType) ||
(headers["Content-Disposition"] && /attachment/i.test(headers["Content-Disposition"])) ||
(headers["content-disposition"] && /attachment/i.test(headers["content-disposition"])) ||
(headers["Content-Description"] && /File Transfer/i.test(headers["Content-Description"])) ||
(headers["content-description"] && /File Transfer/i.test(headers["content-description"]))) &&
content.size > 0
) {
// Download

if ("Blob" in window) {
14 changes: 14 additions & 0 deletions test/unit/components/response-body.jsx
Original file line number Diff line number Diff line change
@@ -39,4 +39,18 @@ describe("<ResponseBody />", function () {
console.warn(wrapper.debug())
expect(wrapper.find("highlightCodeComponent[canCopy]").length).toEqual(1)
})

it("should render Download file link for non-empty response", function () {
props.contentType = "application/octet-stream"
props.content = new Blob(["\"test\""], { type: props.contentType })
const wrapper = shallow(<ResponseBody {...props} />)
expect(wrapper.text()).toMatch(/Download file/)
})

it("should not render Download file link for empty response", function () {
props.contentType = "application/octet-stream"
props.content = new Blob()
const wrapper = shallow(<ResponseBody {...props} />)
expect(wrapper.text()).not.toMatch(/Download file/)
})
})
5 changes: 5 additions & 0 deletions test/unit/setup.js
Original file line number Diff line number Diff line change
@@ -29,6 +29,11 @@ function setUpDomEnvironment() {
}
copyProps(win, window) // use UI's built-in window wrapper
copyProps(window, global)

// https://github.com/jsdom/jsdom/issues/1721
if (typeof global.window.URL.createObjectURL === "undefined") {
Object.defineProperty(global.window.URL, "createObjectURL", { value: () => "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" })
}
}

setUpDomEnvironment()

0 comments on commit cdfc4de

Please sign in to comment.