Skip to content

Commit 868a975

Browse files
committed
Extract content type from headers when fetching remote graphs
1 parent adb0691 commit 868a975

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

lib/ldp.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ const error = require('./http-error')
99
const stringToStream = require('./utils').stringToStream
1010
const serialize = require('./utils').serialize
1111
const overQuota = require('./utils').overQuota
12+
const getContentType = require('./utils').getContentType
1213
const extend = require('extend')
1314
const rimraf = require('rimraf')
1415
const ldpContainer = require('./ldp-container')
1516
const parse = require('./utils').parse
1617
const fetch = require('node-fetch')
1718
const { promisify } = require('util')
1819

19-
const DEFAULT_REMOTE_CONTENT_TYPE = 'text/turtle'
20-
2120
const RDF_MIME_TYPES = [
2221
'text/turtle', // .ttl
2322
'text/n3', // .n3
@@ -286,9 +285,7 @@ class LDP {
286285
}
287286
const body = await response.text()
288287

289-
const contentType = options.contentType || DEFAULT_REMOTE_CONTENT_TYPE
290-
291-
return promisify(parse)(body, uri, contentType)
288+
return promisify(parse)(body, uri, getContentType(response.headers))
292289
}
293290

294291
/**

lib/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ function _asyncReadfile (filename) {
226226

227227
/**
228228
* Get the content type from a headers object
229-
* @param headers An Express headers object
229+
* @param headers An Express or Fetch API headers object
230230
* @return {string} A content type string
231231
*/
232232
function getContentType (headers) {
233-
const headerValue = headers['content-type']
233+
const headerValue = headers.get ? headers.get('content-type') : headers['content-type']
234234

235235
// Default content type as stated by RFC 822
236236
if (!headerValue) {

test/unit/utils-test.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,35 @@ describe('Utility functions', function () {
7272
})
7373

7474
describe('getContentType()', () => {
75-
it('should default to text/plain', () => {
76-
assert.equal(utils.getContentType({}), 'text/plain')
77-
})
75+
describe('for Express headers', () => {
76+
it('should default to text/plain', () => {
77+
assert.equal(utils.getContentType({}), 'text/plain')
78+
})
79+
80+
it('should get a basic content type', () => {
81+
assert.equal(utils.getContentType({'content-type': 'text/html'}), 'text/html')
82+
})
7883

79-
it('should get a basic content type', () => {
80-
assert.equal(utils.getContentType({ 'content-type': 'text/html' }), 'text/html')
84+
it('should get a content type without its charset', () => {
85+
assert.equal(utils.getContentType({'content-type': 'text/html; charset=us-ascii'}), 'text/html')
86+
})
8187
})
8288

83-
it('should get a content type without its charset', () => {
84-
assert.equal(utils.getContentType({ 'content-type': 'text/html; charset=us-ascii' }), 'text/html')
89+
describe('for Fetch API headers', () => {
90+
it('should default to text/plain', () => {
91+
// eslint-disable-next-line no-undef
92+
assert.equal(utils.getContentType(new Headers({})), 'text/plain')
93+
})
94+
95+
it('should get a basic content type', () => {
96+
// eslint-disable-next-line no-undef
97+
assert.equal(utils.getContentType(new Headers({'content-type': 'text/html'})), 'text/html')
98+
})
99+
100+
it('should get a content type without its charset', () => {
101+
// eslint-disable-next-line no-undef
102+
assert.equal(utils.getContentType(new Headers({'content-type': 'text/html; charset=us-ascii'})), 'text/html')
103+
})
85104
})
86105
})
87106
})

0 commit comments

Comments
 (0)