Skip to content

Commit 6d1559e

Browse files
authored
Merge branch 'master' into fix-falsy-formatValue
2 parents 5b92da1 + fa5d649 commit 6d1559e

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/http.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,8 @@ export default function http(url, request = {}) {
6464
})
6565
}
6666

67-
function shouldDownloadAsText(contentType) {
68-
return /json/.test(contentType) ||
69-
/xml/.test(contentType) ||
70-
/yaml/.test(contentType) ||
71-
/text/.test(contentType)
72-
}
67+
// exported for testing
68+
export const shouldDownloadAsText = (contentType = "") => /json|xml|yaml|text/.test(contentType)
7369

7470
// Serialize the response, returns a promise with headers and the body part of the hash
7571
export function serializeRes(oriRes, url, {loadSpec = false} = {}) {

test/http.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import expect from 'expect'
22
import xmock from 'xmock'
33
import fetchMock from 'fetch-mock'
4-
import http, {serializeHeaders, mergeInQueryOrForm, encodeFormOrQuery, serializeRes} from '../src/http'
4+
import http, {
5+
serializeHeaders, mergeInQueryOrForm, encodeFormOrQuery, serializeRes,
6+
shouldDownloadAsText
7+
} from '../src/http'
58

69
describe('http', () => {
710
let xapp
@@ -306,4 +309,31 @@ describe('http', () => {
306309
}).then(fetchMock.restore)
307310
})
308311
})
312+
313+
describe('shouldDownloadAsText', () => {
314+
it('should return true for json, xml, yaml, and text types', function() {
315+
const types = [
316+
"text/x-yaml", "application/xml", "text/xml", "application/json",
317+
"text/plain"
318+
]
319+
320+
types.forEach(v => {
321+
expect(`${v} ${shouldDownloadAsText(v)}`).toEqual(v + " true")
322+
})
323+
})
324+
325+
it('should return false for other common types', function() {
326+
const types = [
327+
"application/octet-stream", "application/x-binary"
328+
]
329+
330+
types.forEach(v => {
331+
expect(`${v} ${shouldDownloadAsText(v)}`).toEqual(v + " false")
332+
})
333+
})
334+
335+
it('should fail gracefully when called with no parameters', function() {
336+
expect(shouldDownloadAsText()).toEqual(false)
337+
})
338+
})
309339
})

0 commit comments

Comments
 (0)