Skip to content

Commit

Permalink
Use last modified header instead of Date, fix up delete tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauve Signweaver committed Nov 3, 2023
1 parent e6f66d7 commit 790d9cb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ The `body` can be any of the options supported by the Fetch API such as a `Strin
`NAME` can either be the 52 character [z32 encoded](https://github.com/mafintosh/z32) key for a Hyperdrive or Hypercore , or a domain to parse with the [DNSLink](https://www.dnslink.io/) standard.

The mtime metadata is automatically set to the current time when
uploading. To override this value, pass a `Date` header with a value
uploading. To override this value, pass a `Last-Modified` header with a value
set to a date string according to [RFC
7231](https://datatracker.ietf.org/doc/html/rfc7231#section-7.1.1.1).

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export default async function makeHyperFetch ({
const { hostname, pathname: rawPathname } = new URL(request.url)
const pathname = decodeURI(ensureLeadingSlash(rawPathname))
const contentType = request.headers.get('Content-Type') || ''
const mtime = Date.parse(request.headers.get('Date')) || Date.now()
const mtime = Date.parse(request.headers.get('Last-Modified')) || Date.now()
const isFormData = contentType.includes('multipart/form-data')

const drive = await getDrive(`hyper://${hostname}/`, true)
Expand Down
23 changes: 10 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ test('PUT file', async (t) => {
method: 'put',
body: SAMPLE_CONTENT,
headers: {
Date: fakeDate
'Last-Modified': fakeDate
}
})

Expand Down Expand Up @@ -291,7 +291,7 @@ test('DELETE a directory', async (t) => {
})
await checkResponse(uploadResponse, t)

const deleteResponse = await fetch(created, {
const deleteResponse = await fetch(uploadLocation, {
method: 'delete'
})
await checkResponse(deleteResponse, t, 'Able to DELETE')
Expand All @@ -301,7 +301,7 @@ test('DELETE a directory', async (t) => {
const entries = await listDirRequest.json()
t.deepEqual(entries, [], 'subfolder got deleted')
})
test.only('DELETE a drive from storage', async (t) => {
test('DELETE a drive from storage', async (t) => {
const created = await nextURL(t)

const uploadLocation = new URL('./subfolder/example.txt', created)
Expand Down Expand Up @@ -589,17 +589,10 @@ test('Handle empty string pathname', async (t) => {
await fetch(urlNoTrailingSlash, {
method: 'put',
body: formData
}), t
}),
t
)

// DELETE
await checkResponse(await fetch(urlNoTrailingSlash, { method: 'DELETE' }), t)

// HEAD
const headResponse = await fetch(urlNoTrailingSlash, { method: 'HEAD' })
await checkResponse(headResponse, t)
t.deepEqual(headResponse.headers.get('Etag'), '5', 'HEAD request returns correct Etag')

// HEAD (versioned)
const versionedHeadResponse = await fetch(versionedURLNoTrailingSlash, { method: 'HEAD' })
await checkResponse(versionedHeadResponse, t)
Expand All @@ -608,12 +601,16 @@ test('Handle empty string pathname', async (t) => {
// GET
const getResponse = await fetch(urlNoTrailingSlash)
await checkResponse(getResponse, t)
t.deepEqual(await getResponse.json(), [], 'Returns empty root directory')
t.deepEqual(await getResponse.json(), ['example.txt', 'example2.txt'], 'Returns directory listing')

// GET (versioned)
const versionedGetResponse = await fetch(versionedURLNoTrailingSlash)
await checkResponse(versionedGetResponse, t)
t.deepEqual(await versionedGetResponse.json(), ['example.txt', 'example2.txt'], 'Returns root directory prior to DELETE')


// DELETE
await checkResponse(await fetch(urlNoTrailingSlash, { method: 'DELETE' }), t, 'Able to delete root')
})

test('Return status 403 Forbidden on attempt to modify read-only hyperdrive', async (t) => {
Expand Down

0 comments on commit 790d9cb

Please sign in to comment.