Skip to content

Commit

Permalink
Fix empty mime
Browse files Browse the repository at this point in the history
  • Loading branch information
YanagiEiichi committed Dec 26, 2018
1 parent 1663bf6 commit 9cc86fe
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class API extends Function { // eslint-disable-line no-unused-vars
Object.defineProperty(response, 'auto', {
configurable: true,
async value() {
const type = this.headers.get('Content-Type').match(/\b(json|text)\b/g).sort()[0] || 'blob';
const mime = this.headers.get('Content-Type') || 'unknown';
const type = mime.match(/\b(json|text|$)\b/g).filter(Boolean).sort()[0] || 'blob';
switch (true) {
case this.status === 204: return null;
case this.ok: return this[type]();
Expand Down
8 changes: 8 additions & 0 deletions tests/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script src="/tester.js"></script>
<script src="/index.js"></script>
<script>
let api = new API('.');
api.what_the_fuck.get().catch(content => {
Tester.feedback(true);
});
</script>
8 changes: 8 additions & 0 deletions tests/raw-404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script src="/tester.js"></script>
<script src="/index.js"></script>
<script>
let api = new API('.');
api.what_the_fuck.get({ rawResponse: true }).then(response => {
Tester.feedback(response.status === 404);
});
</script>
13 changes: 13 additions & 0 deletions tests/raw-basic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script src="/tester.js"></script>
<script src="/index.js"></script>
<script>
let api = new API();
api = location.pathname.match(/[^/]+/g).reduce((api, name) => api[name], api);
api.get({ rawResponse: true }).then(async response => {
let result = await response.auto();
Tester.assert(response.ok);
Tester.assert(response.status === 200);
Tester.assert(result === document.head.innerHTML);
Tester.feedback(true);
});
</script>
12 changes: 12 additions & 0 deletions tests/raw-json.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script src="/tester.js"></script>
<script src="/index.js"></script>
<script>
let api = new API('.');
api['test.json'].get({ rawResponse: true }).then(async response => {
let result = await response.auto();
Tester.assert(response.ok);
Tester.assert(response.status === 200);
Tester.assert(result.text === 'apisdk');
Tester.feedback(true);
});
</script>

0 comments on commit 9cc86fe

Please sign in to comment.