Skip to content

Commit

Permalink
Use same error messages as AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
lauriro committed Jan 18, 2024
1 parent 0380311 commit 507615e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
22 changes: 11 additions & 11 deletions s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,18 @@ function req(method, path, opts, next, data) {
data += chunk.toString("utf8")
})
res.on("end", function() {
if (method === "HEAD") {
data = res.statusCode < 400 ? {
size: +res.headers["content-length"],
mtime: new Date(res.headers["last-modified"]),
etag: res.headers.etag
} : Error(path ? "File not found" : "Bucket not found")
} else if (res.headers["content-type"] === "application/xml") {
data = parseXml(data)
data = data.listBucketResult || data.error || data
if (res.statusCode > 299) {
data = method !== "HEAD" && parseXml(data).error || (
path ? "The specified key does not exist." : "The specified bucket is not valid."
)
return reject(Error(data.message || data))
}
if (res.statusCode > 299) return reject(data)
resolve(data)
data = method === "HEAD" ? {
size: +res.headers["content-length"],
mtime: new Date(res.headers["last-modified"]),
etag: res.headers.etag
} : parseXml(data)
resolve(data.listBucketResult || data.error || data)
})
res.on("error", reject)
}
Expand Down
47 changes: 37 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ describe("S3 Mock", function() {
, fakeStorage = {
"https://s3-eu-central-1.amazonaws.com/buck-1/": "<>",
"https://s3-eu-central-1.amazonaws.com/buck-1/file1.txt": "Hello",
"https://s3-eu-central-1.amazonaws.com/a/?list-type=2&max-keys=10&prefix=":
'<?xml version="1.0" encoding="UTF-8"?><Error><Code>InvalidBucketName</Code><Message>The specified bucket is not valid.</Message><BucketName>a</BucketName><RequestId>Q4T6PZWDG38ABGDS</RequestId><HostId>yQ1ESNjLMN5eOb5nJIdgHHvtxclAX5t2DElg0UqVJZ+DR5aMocJkzmtwWIAcwcErWcpqQFjXEE0=</HostId></Error>',
"https://s3-eu-central-1.amazonaws.com/a/?list-type=2&max-keys=10&prefix=": {
code: 404,
body: "<?xml version='1.0' encoding='UTF-8'?><Error><Code>InvalidBucketName</Code><Message>The specified bucket is not valid.</Message><BucketName>a</BucketName><RequestId>Q4T6PZWDG38ABGDS</RequestId><HostId>yQ1ESNjLMN5eOb5nJIdgHHvtxclAX5t2DElg0UqVJZ+DR5aMocJkzmtwWIAcwcErWcpqQFjXEE0=</HostId></Error>",
},
"https://s3-eu-central-1.amazonaws.com/aaa/?list-type=2&max-keys=10&prefix=":
'<?xml version="1.0" encoding="UTF-8"?><Error><Code>PermanentRedirect</Code><Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message><Endpoint>s3.amazonaws.com</Endpoint><Bucket>aaa</Bucket><RequestId>EMZH6M0NJJZVRAVT</RequestId><HostId>ugvCvJQ9gZhsWgZqK4qxXuF75k3XEbEtjP8OEEcSikCc5CHRk1Dr3ijH0M7nHEtzLkfSmwmAOEA=</HostId></Error>',
"https://s3-eu-central-1.amazonaws.com/aaa-bbb/?list-type=2&max-keys=10&prefix=":
Expand All @@ -37,10 +39,11 @@ describe("S3 Mock", function() {
function respond() {
response.statusCode = 404
if (fakeStorage[url]) {
response.statusCode = 200
response.headers["content-length"] = fakeStorage[url].length
if (fakeStorage[url].charAt(0) === "<") response.headers["content-type"] = "application/xml"
listeners.data(fakeStorage[url])
var body = fakeStorage[url].body || fakeStorage[url]
response.statusCode = fakeStorage[url].code || 200
response.headers["content-length"] = body.length
if (body.charAt(0) === "<") response.headers["content-type"] = "application/xml"
listeners.data(body)
}
listeners.end()
}
Expand Down Expand Up @@ -132,7 +135,7 @@ describe("S3 Mock", function() {
var s3client = mockedClient(mock, {bucket: "buck-2"})

s3client.stat(null, null, function(err, data) {
assert.equal(err, Error("Bucket not found"))
assert.equal(err, Error("The specified bucket is not valid."))
assert.notOk(data)
assert.own(s3client.client.request, {
called: 1,
Expand Down Expand Up @@ -180,7 +183,7 @@ describe("S3 Mock", function() {
var s3client = mockedClient(mock, {bucket: "buck-1"})

s3client.stat("none.txt", null, function(err, data) {
assert.equal(err, Error("File not found"))
assert.equal(err, Error("The specified key does not exist."))
assert.notOk(data)
assert.own(s3client.client.request, {
called: 1,
Expand Down Expand Up @@ -271,11 +274,11 @@ describe("S3 Mock", function() {
mock.tick()
})

it("should handle invalid bucket name", function(assert, mock) {
it("should stat invalid bucket", function(assert, mock) {
var s3client = mockedClient(mock, {bucket: "a"})

s3client.stat("", null, function(err, data) {
assert.own(err, { message: "Bucket not found" })
assert.own(err, { message: "The specified bucket is not valid." })
assert.notOk(data)
assert.own(s3client.client.request, {
called: 1,
Expand All @@ -295,6 +298,30 @@ describe("S3 Mock", function() {
mock.tick()
})

it("should list invalid bucket", function(assert, mock) {
var s3client = mockedClient(mock, {bucket: "a"})

s3client.list("", null, function(err, data) {
assert.own(err, { message: "The specified bucket is not valid." })
assert.notOk(data)
assert.own(s3client.client.request, {
called: 1,
errors: 0
})
assert.fnCalled(s3client, 0, "https://s3-eu-central-1.amazonaws.com/a/?list-type=2&max-keys=10&prefix=", {
method: "GET",
headers: {
"host": "s3-eu-central-1.amazonaws.com",
"x-amz-date": "20220423T130929Z",
"x-amz-content-sha256": "UNSIGNED-PAYLOAD",
"Authorization": "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20220423/eu-central-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=910e361836dd01e54e022fca928db767b4108554f7ed22bfaf901db40233bd25"
}
})
assert.end()
})
mock.tick()
})

it("should get signed url", function(assert, mock) {
var s3client = mockedClient(mock, {bucket: "buck-1"})

Expand Down
4 changes: 2 additions & 2 deletions test/live.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("S3 live on {0} {1}", [
})

it("should stat file", [
[ "none.txt", Error("File not found"), undefined ],
[ "none.txt", Error("The specified key does not exist."), undefined ],
], function(name, expErr, expData, assert) {
assert.setTimeout(5000)
s3client.stat("none.txt", null, function(err, data) {
Expand All @@ -60,7 +60,7 @@ describe("S3 live on {0} {1}", [

it("should get error on non-existing file", function(assert, mock) {
s3client.get("non-existing-" + fileName, function(err, data) {
assert.equal(err, Error("File not found"))
assert.own(err, {"message": "The specified key does not exist."})
assert.notOk(data)
assert.end()
})
Expand Down

0 comments on commit 507615e

Please sign in to comment.