Skip to content

Commit 0c1ce05

Browse files
committed
Make text/turtle the default content type for mapping .acl files
1 parent f76710e commit 0c1ce05

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

lib/resource-mapper.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ const readdir = promisify(fs.readdir)
88
// following the principles of the “sweet spot” discussed in
99
// https://www.w3.org/DesignIssues/HTTPFilenameMapping.html
1010
class ResourceMapper {
11-
constructor ({ rootUrl, rootPath, includeHost, defaultContentType, indexName = 'index' }) {
11+
constructor ({
12+
rootUrl,
13+
rootPath,
14+
includeHost,
15+
defaultContentType,
16+
indexName = 'index',
17+
overrideTypes = { acl: 'text/turtle' }
18+
}) {
1219
this._rootUrl = this._removeTrailingSlash(rootUrl)
1320
this._rootPath = this._removeTrailingSlash(rootPath)
1421
this._includeHost = includeHost
1522
this._readdir = readdir
1623
this._defaultContentType = defaultContentType
1724
this._indexName = indexName
25+
this._types = { ...types, ...overrideTypes }
1826

1927
// If the host needs to be replaced on every call, pre-split the root URL
2028
if (includeHost) {
@@ -128,7 +136,7 @@ class ResourceMapper {
128136
// Gets the expected content type based on the extension of the path
129137
_getContentTypeByExtension (path) {
130138
const extension = /\.([^/.]+)$/.exec(path)
131-
return extension && types[extension[1].toLowerCase()] || this._defaultContentType
139+
return extension && this._types[extension[1].toLowerCase()] || this._defaultContentType
132140
}
133141

134142
// Removes a possible trailing slash from a path

test/unit/resource-mapper-test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,28 @@ describe('ResourceMapper', () => {
9898
contentType: 'image/jpeg'
9999
})
100100

101+
itMapsUrl(mapper, 'a URL with an overridden extension that matches the content type',
102+
{
103+
url: 'http://localhost/space/foo.acl',
104+
contentType: 'text/turtle',
105+
createIfNotExists: true
106+
},
107+
{
108+
path: `${rootPath}space/foo.acl`,
109+
contentType: 'text/turtle'
110+
})
111+
112+
itMapsUrl(mapper, 'a URL with an alternative overridden extension that matches the content type',
113+
{
114+
url: 'http://localhost/space/foo.acl',
115+
contentType: 'text/n3',
116+
createIfNotExists: true
117+
},
118+
{
119+
path: `${rootPath}space/foo.acl$.n3`,
120+
contentType: 'text/n3'
121+
})
122+
101123
// GET/HEAD/POST/DELETE/PATCH base cases
102124

103125
itMapsUrl(mapper, 'a URL of a non-existing file',
@@ -192,6 +214,30 @@ describe('ResourceMapper', () => {
192214
contentType: 'text/html'
193215
})
194216

217+
itMapsUrl(mapper, 'a URL of an existing .acl file',
218+
{
219+
url: 'http://localhost/space/.acl'
220+
},
221+
[
222+
`${rootPath}space/.acl`
223+
],
224+
{
225+
path: `${rootPath}space/.acl`,
226+
contentType: 'text/turtle'
227+
})
228+
229+
itMapsUrl(mapper, 'a URL of an existing .acl file with a different content type',
230+
{
231+
url: 'http://localhost/space/.acl'
232+
},
233+
[
234+
`${rootPath}space/.acl$.n3`
235+
],
236+
{
237+
path: `${rootPath}space/.acl$.n3`,
238+
contentType: 'text/n3'
239+
})
240+
195241
itMapsUrl(mapper, 'a URL ending with a slash to an index file when index.html is available',
196242
{
197243
url: 'http://localhost/space/'
@@ -290,6 +336,13 @@ describe('ResourceMapper', () => {
290336
contentType: 'text/turtle'
291337
})
292338

339+
itMapsFile(mapper, 'an ACL file',
340+
{ path: `${rootPath}space/.acl` },
341+
{
342+
url: 'http://localhost/space/.acl',
343+
contentType: 'text/turtle'
344+
})
345+
293346
itMapsFile(mapper, 'an unknown file type',
294347
{ path: `${rootPath}space/foo.bar` },
295348
{

0 commit comments

Comments
 (0)