diff --git a/s3.js b/s3.js index 2acc549..c7c6d9b 100644 --- a/s3.js +++ b/s3.js @@ -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) } diff --git a/test/index.js b/test/index.js index 7060b2d..9c9b3a4 100644 --- a/test/index.js +++ b/test/index.js @@ -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=": - 'InvalidBucketNameThe specified bucket is not valid.aQ4T6PZWDG38ABGDSyQ1ESNjLMN5eOb5nJIdgHHvtxclAX5t2DElg0UqVJZ+DR5aMocJkzmtwWIAcwcErWcpqQFjXEE0=', + "https://s3-eu-central-1.amazonaws.com/a/?list-type=2&max-keys=10&prefix=": { + code: 404, + body: "InvalidBucketNameThe specified bucket is not valid.aQ4T6PZWDG38ABGDSyQ1ESNjLMN5eOb5nJIdgHHvtxclAX5t2DElg0UqVJZ+DR5aMocJkzmtwWIAcwcErWcpqQFjXEE0=", + }, "https://s3-eu-central-1.amazonaws.com/aaa/?list-type=2&max-keys=10&prefix=": 'PermanentRedirectThe bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.s3.amazonaws.comaaaEMZH6M0NJJZVRAVTugvCvJQ9gZhsWgZqK4qxXuF75k3XEbEtjP8OEEcSikCc5CHRk1Dr3ijH0M7nHEtzLkfSmwmAOEA=', "https://s3-eu-central-1.amazonaws.com/aaa-bbb/?list-type=2&max-keys=10&prefix=": @@ -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() } @@ -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, @@ -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, @@ -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, @@ -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"}) diff --git a/test/live.js b/test/live.js index de99e9f..b93ee2b 100644 --- a/test/live.js +++ b/test/live.js @@ -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) { @@ -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() })