Skip to content

Commit

Permalink
text/turtle defaultContainerContentType
Browse files Browse the repository at this point in the history
  • Loading branch information
bourgeoa committed Jan 17, 2024
1 parent 33f7354 commit fbd30b0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
6 changes: 3 additions & 3 deletions lib/ldp.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,9 @@ class LDP {
throw err
}
const stream = stringToStream(data)
// TODO 'text/turtle' is fixed, should be contentType instead
// This forces one more translation turtle -> desired
return { stream, contentType: 'text/turtle', container: true }
// TODO contentType is defaultContainerContentType ('text/turtle'),
// This forces one translation turtle -> desired
return { stream, contentType, container: true }
} else {
let chunksize, contentRange, start, end
if (options.range) {
Expand Down
7 changes: 5 additions & 2 deletions lib/resource-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ResourceMapper {
rootPath,
includeHost = false,
defaultContentType = 'application/octet-stream',
defaultContainerContentType = 'text/turtle',
indexFilename = 'index.html',
overrideTypes = { acl: 'text/turtle', meta: 'text/turtle' }
}) {
Expand All @@ -33,6 +34,7 @@ class ResourceMapper {
this._includeHost = includeHost
this._readdir = readdir
this._defaultContentType = defaultContentType
this._defaultContainerContentType = defaultContainerContentType
this._types = { ...types, ...overrideTypes }
this._indexFilename = indexFilename
this._indexContentType = this._getContentTypeFromExtension(indexFilename)
Expand Down Expand Up @@ -187,10 +189,11 @@ class ResourceMapper {
return url
}

// Gets the expected content type based on the extension of the path
// Gets the expected content type based on resource type and the extension of the path
_getContentTypeFromExtension (path) {
const defaultContentType = (path === '' || path.endsWith('/')) ? this._defaultContainerContentType : this._defaultContentType
const extension = /\.([^/.]+)$/.exec(path)
return extension && this._types[extension[1].toLowerCase()] || this._defaultContentType
return extension && this._types[extension[1].toLowerCase()] || defaultContentType
}

// Appends an extension for the specific content type, if needed
Expand Down
19 changes: 15 additions & 4 deletions test/integration/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ describe('HTTP APIs', function () {
.expect('Content-Type', 'application/octet-stream; charset=utf-8')
.end(done)
})
it('should return content-type text/turtle for container', function (done) {
server.head('/sampleContainer2/')
.expect('Content-Type', 'text/turtle; charset=utf-8')
.end(done)
})
it('should have set content-type for turtle files',
function (done) {
server.head('/sampleContainer2/example1.ttl')
Expand Down Expand Up @@ -456,7 +461,7 @@ describe('HTTP APIs', function () {
.expect(200, done)
})
it('should have set Updates-Via to use WebSockets', function (done) {
server.get('/sampleContainer2/example1.ttl')
server.head('/sampleContainer2/example1.ttl')
.expect('updates-via', /wss?:\/\//)
.expect(200, done)
})
Expand All @@ -467,21 +472,27 @@ describe('HTTP APIs', function () {
})
it('should have set acl and describedBy Links for resource',
function (done) {
server.get('/sampleContainer2/example1.ttl')
server.head('/sampleContainer2/example1.ttl') // get
.expect(hasHeader('acl', 'example1.ttl' + suffixAcl))
.expect(hasHeader('describedBy', 'example1.ttl' + suffixMeta))
.end(done)
})
it('should have set Content-Type as text/turtle for Container',
function (done) {
server.head('/sampleContainer2/')
.expect('Content-Type', 'text/turtle; charset=utf-8')
.expect(200, done)
})
it('should have set Link as Container/BasicContainer',
function (done) {
server.get('/sampleContainer2/')
server.head('/sampleContainer2/')
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#BasicContainer>; rel="type"/)
.expect('Link', /<http:\/\/www.w3.org\/ns\/ldp#Container>; rel="type"/)
.expect(200, done)
})
it('should have set acl and describedBy Links for container',
function (done) {
server.get('/sampleContainer2/')
server.head('/sampleContainer2/')
.expect(hasHeader('acl', suffixAcl))
.expect(hasHeader('describedBy', suffixMeta))
.end(done)
Expand Down
16 changes: 8 additions & 8 deletions test/unit/resource-mapper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ describe('ResourceMapper', () => {
],
{
path: `${rootPath}space/`,
contentType: 'application/octet-stream'
contentType: 'text/turtle' // 'application/octet-stream'
})

itMapsUrl(mapper, 'a URL ending with a slash when index$.html is available',
Expand All @@ -295,7 +295,7 @@ describe('ResourceMapper', () => {
],
{
path: `${rootPath}space/`,
contentType: 'application/octet-stream'
contentType: 'text/turtle' // 'application/octet-stream'
})

itMapsUrl(mapper, 'a URL ending with a slash when index$.ttl is available',
Expand All @@ -307,7 +307,7 @@ describe('ResourceMapper', () => {
],
{
path: `${rootPath}space/`,
contentType: 'application/octet-stream'
contentType: 'text/turtle' // 'application/octet-stream'
})

itMapsUrl(mapper, 'a URL ending with a slash to a folder when index.html is available but index is skipped',
Expand All @@ -321,7 +321,7 @@ describe('ResourceMapper', () => {
],
{
path: `${rootPath}space/`,
contentType: 'application/octet-stream'
contentType: 'text/turtle' // 'application/octet-stream'
})

itMapsUrl(mapper, 'a URL ending with a slash to a folder when no index is available',
Expand All @@ -330,7 +330,7 @@ describe('ResourceMapper', () => {
},
{
path: `${rootPath}space/`,
contentType: 'application/octet-stream'
contentType: 'text/turtle' // 'application/octet-stream'
})

itMapsUrl(mapper, 'a URL of that has an accompanying acl file, but no actual file',
Expand All @@ -342,7 +342,7 @@ describe('ResourceMapper', () => {
],
{
path: `${rootPath}space/`,
contentType: 'application/octet-stream'
contentType: 'text/turtle' // 'application/octet-stream'
})

itMapsUrl(mapper, 'a URL ending with a slash for text/html when index.html is not available',
Expand Down Expand Up @@ -373,13 +373,13 @@ describe('ResourceMapper', () => {
itMapsUrl(mapper, 'a URL ending with a slash to a folder when index is skipped',
{
url: 'http://localhost/space/',
contentType: 'application/octet-stream',
contentType: 'text/turtle', // 'application/octet-stream',
createIfNotExists: true,
searchIndex: false
},
{
path: `${rootPath}space/`,
contentType: 'application/octet-stream'
contentType: 'text/turtle' // 'application/octet-stream'
})

itMapsUrl(mapper, 'a URL ending with a slash for text/turtle',
Expand Down

0 comments on commit fbd30b0

Please sign in to comment.