Skip to content

Commit 530f740

Browse files
committed
Fix multi-POST test failing with directory as target
1 parent 2343021 commit 530f740

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

lib/handlers/post.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async function handler (req, res, next) {
2929
// Check if container exists
3030
let stats
3131
try {
32-
const ret = await ldp.exists(req.hostname, containerPath)
32+
const ret = await ldp.exists(req.hostname, containerPath, false)
3333
if (ret) {
3434
stats = ret.stream
3535
}

lib/ldp.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,9 @@ class LDP {
251251
})
252252
}
253253

254-
async exists (hostname, path) {
255-
const options = { hostname, path, includeBody: false }
256-
await this.get(options)
257-
return true
254+
async exists (hostname, path, searchIndex = true) {
255+
const options = { hostname, path, includeBody: false, searchIndex }
256+
return await this.get(options, searchIndex)
258257
}
259258

260259
/**
@@ -322,10 +321,10 @@ class LDP {
322321
})
323322
}
324323

325-
async get (options) {
324+
async get (options, searchIndex = true) {
326325
let filename, contentType, stats
327326
try {
328-
({ path: filename, contentType } = await this.resourceMapper.mapUrlToFile({ url: options }))
327+
({ path: filename, contentType } = await this.resourceMapper.mapUrlToFile({ url: options, searchIndex }))
329328
stats = await this.stat(filename)
330329
} catch (err) {
331330
throw error(404, 'Can\'t find file requested: ' + options)

lib/resource-mapper.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class ResourceMapper {
3939

4040
// Maps the request for a given resource and representation format to a server file
4141
// When the URL ends with a '/', then files with the prefix 'index.' will be matched,
42-
// such as 'index.html' and 'index.ttl'.
43-
async mapUrlToFile ({ url, contentType, createIfNotExists }) {
42+
// such as 'index.html' and 'index.ttl', unless searchIndex is false.
43+
async mapUrlToFile ({ url, contentType, createIfNotExists, searchIndex = true }) {
4444
let fullPath = this._getFullPath(url)
45-
let isIndex = fullPath.endsWith('/')
45+
let isIndex = searchIndex && fullPath.endsWith('/')
4646
let path
4747

4848
// Append index filename if the URL ends with a '/'
@@ -54,7 +54,7 @@ class ResourceMapper {
5454
if (createIfNotExists) {
5555
path = fullPath
5656
// If the extension is not correct for the content type, append the correct extension
57-
if (this._getContentTypeByExtension(path) !== contentType) {
57+
if (searchIndex && this._getContentTypeByExtension(path) !== contentType) {
5858
// Append a '$', unless we map for the index
5959
// Because the index must _always_ have a proper extension when it is being created
6060
if (!isIndex) {
@@ -70,9 +70,9 @@ class ResourceMapper {
7070
const files = await this._readdir(folder)
7171

7272
// Find a file with the same name (minus the dollar extension)
73-
let match = files.find(f => this._removeDollarExtension(f) === filename ||
74-
(isIndex && f.startsWith(this._indexFilename + '.')))
75-
if (!match) {
73+
let match = !searchIndex ? '' : (files.find(f => this._removeDollarExtension(f) === filename ||
74+
(isIndex && f.startsWith(this._indexFilename + '.'))))
75+
if (match === undefined) {
7676
// Error if no match was found,
7777
// unless the URL ends with a '/',
7878
// in that case we fallback to the folder itself.

test/unit/resource-mapper-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ describe('ResourceMapper', () => {
263263
contentType: 'text/html'
264264
})
265265

266+
itMapsUrl(mapper, 'a URL ending with a slash to a folder when index.html is available but index is skipped',
267+
{
268+
url: 'http://localhost/space/',
269+
searchIndex: false
270+
},
271+
[
272+
`${rootPath}space/index.html`,
273+
`${rootPath}space/index$.ttl`
274+
],
275+
{
276+
path: `${rootPath}space/`,
277+
contentType: 'application/octet-stream'
278+
})
279+
266280
itMapsUrl(mapper, 'a URL ending with a slash to a folder when no index is available',
267281
{
268282
url: 'http://localhost/space/'
@@ -283,6 +297,18 @@ describe('ResourceMapper', () => {
283297
contentType: 'text/html'
284298
})
285299

300+
itMapsUrl(mapper, 'a URL ending with a slash to a folder when index is skipped',
301+
{
302+
url: 'http://localhost/space/',
303+
contentType: 'application/octet-stream',
304+
createIfNotExists: true,
305+
searchIndex: false
306+
},
307+
{
308+
path: `${rootPath}space/`,
309+
contentType: 'application/octet-stream'
310+
})
311+
286312
itMapsUrl(mapper, 'a URL ending with a slash to an index file for text/turtle when index.ttl not is available',
287313
{
288314
url: 'http://localhost/space/',

0 commit comments

Comments
 (0)