Skip to content

Commit fa5d649

Browse files
authored
Merge pull request #1099 from Robbilie/patch-3
Change the regex, increase performance increadibly :D
2 parents f0f3b0b + 350ab36 commit fa5d649

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
@@ -303,4 +306,31 @@ describe('http', () => {
303306
}).then(fetchMock.restore)
304307
})
305308
})
309+
310+
describe('shouldDownloadAsText', () => {
311+
it('should return true for json, xml, yaml, and text types', function() {
312+
const types = [
313+
"text/x-yaml", "application/xml", "text/xml", "application/json",
314+
"text/plain"
315+
]
316+
317+
types.forEach(v => {
318+
expect(`${v} ${shouldDownloadAsText(v)}`).toEqual(v + " true")
319+
})
320+
})
321+
322+
it('should return false for other common types', function() {
323+
const types = [
324+
"application/octet-stream", "application/x-binary"
325+
]
326+
327+
types.forEach(v => {
328+
expect(`${v} ${shouldDownloadAsText(v)}`).toEqual(v + " false")
329+
})
330+
})
331+
332+
it('should fail gracefully when called with no parameters', function() {
333+
expect(shouldDownloadAsText()).toEqual(false)
334+
})
335+
})
306336
})

0 commit comments

Comments
 (0)