Skip to content

Commit 1f855e7

Browse files
committed
feat(lib/AWSstub): exposed the mocked node aws client
Docs(ReadMe): added info about requiring AWSstub
1 parent df279bf commit 1f855e7

File tree

3 files changed

+121
-109
lines changed

3 files changed

+121
-109
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,14 @@ var AWS = require('aws-sdk');
248248
AWS.config.httpOptions = {timeout: 5000};
249249
```
250250

251+
### Mocking the AWS SDK for testing
252+
253+
For convience the aws sdk has been stubbed out and exposed via:
254+
255+
```js
256+
var AWSstub = require('s3-upload-stream/lib/AWSstub')
257+
```
258+
251259
### Installation
252260

253261
```

lib/AWSstub.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
var expect = require('chai').expect;
2+
3+
4+
/**
5+
* Defines a stubbed out version of the AWS S3 Node.js client.
6+
*/
7+
module.exports = {
8+
S3: function () {
9+
this.createMultipartUpload = function (details, callback) {
10+
// Make sure that this AWS function was called with the right parameters.
11+
expect(details).to.have.property('Bucket');
12+
expect(details.Key).to.be.a('string');
13+
14+
expect(details).to.have.property('Key');
15+
expect(details.Key).to.be.a('string');
16+
17+
if (details.Key == 'create-fail') {
18+
// Trigger a simulated error when a magic file name is used.
19+
callback('Simulated failure from mocked API');
20+
}
21+
else {
22+
callback(null, {
23+
UploadId: 'upload-id'
24+
});
25+
}
26+
};
27+
28+
this.uploadPart = function (details, callback) {
29+
// Make sure that all the properties are there
30+
expect(details).to.have.property('Body');
31+
expect(details.Body).to.be.instanceof(Buffer);
32+
33+
expect(details).to.have.property('Bucket');
34+
expect(details.Bucket).to.equal('test-bucket-name');
35+
36+
expect(details).to.have.property('Key');
37+
expect(details.Key).to.be.a('string');
38+
39+
expect(details).to.have.property('UploadId');
40+
expect(details.UploadId).to.contain('upload-id');
41+
42+
expect(details).to.have.property('PartNumber');
43+
expect(details.PartNumber).to.be.an.integer;
44+
45+
if (details.Key == 'upload-fail') {
46+
callback('Simulated failure from mocked API');
47+
}
48+
else {
49+
// Return an ETag
50+
callback(null, {
51+
ETag: 'etag'
52+
});
53+
}
54+
};
55+
56+
this.abortMultipartUpload = function (details, callback) {
57+
// Make sure that all the properties are there
58+
expect(details).to.have.property('Bucket');
59+
expect(details.Bucket).to.equal('test-bucket-name');
60+
61+
expect(details).to.have.property('Key');
62+
expect(details.Key).to.be.a('string');
63+
64+
expect(details).to.have.property('UploadId');
65+
expect(details.UploadId).to.contain('upload-id');
66+
67+
if (details.Key == 'abort-fail') {
68+
// Trigger a simulated error when a magic file name is used.
69+
callback('Simulated failure from mocked API');
70+
}
71+
else {
72+
callback();
73+
}
74+
};
75+
76+
this.completeMultipartUpload = function (details, callback) {
77+
// Make sure that all the properties are there
78+
expect(details).to.have.property('Bucket');
79+
expect(details.Bucket).to.equal('test-bucket-name');
80+
81+
expect(details).to.have.property('Key');
82+
expect(details.Key).to.be.a('string');
83+
84+
expect(details).to.have.property('UploadId');
85+
expect(details.UploadId).to.contain('upload-id');
86+
87+
expect(details).to.have.property('MultipartUpload');
88+
expect(details.MultipartUpload).to.an.object;
89+
90+
expect(details.MultipartUpload).to.have.property('Parts');
91+
expect(details.MultipartUpload.Parts).to.an.array;
92+
93+
details.MultipartUpload.Parts.forEach(function (partNumber) {
94+
expect(partNumber).to.be.an.integer;
95+
});
96+
97+
if (details.Key == 'complete-fail' || details.Key == 'abort-fail') {
98+
// Trigger a simulated error when a magic file name is used.
99+
callback('Simulated failure from mocked API');
100+
}
101+
else {
102+
callback(null, {
103+
ETag: 'etag'
104+
});
105+
}
106+
};
107+
}
108+
};

tests/test.js

Lines changed: 5 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,9 @@
1-
var expect = require('chai').expect,
2-
fs = require('fs'),
3-
Writable = require('stream').Writable;
4-
5-
// Define a stubbed out version of the AWS S3 Node.js client
6-
var AWSstub = {
7-
S3: function () {
8-
this.createMultipartUpload = function (details, callback) {
9-
// Make sure that this AWS function was called with the right parameters.
10-
expect(details).to.have.property('Bucket');
11-
expect(details.Key).to.be.a('string');
12-
13-
expect(details).to.have.property('Key');
14-
expect(details.Key).to.be.a('string');
15-
16-
if (details.Key == 'create-fail') {
17-
// Trigger a simulated error when a magic file name is used.
18-
callback('Simulated failure from mocked API');
19-
}
20-
else {
21-
callback(null, {
22-
UploadId: 'upload-id'
23-
});
24-
}
25-
};
26-
27-
this.uploadPart = function (details, callback) {
28-
// Make sure that all the properties are there
29-
expect(details).to.have.property('Body');
30-
expect(details.Body).to.be.instanceof(Buffer);
31-
32-
expect(details).to.have.property('Bucket');
33-
expect(details.Bucket).to.equal('test-bucket-name');
34-
35-
expect(details).to.have.property('Key');
36-
expect(details.Key).to.be.a('string');
37-
38-
expect(details).to.have.property('UploadId');
39-
expect(details.UploadId).to.contain('upload-id');
40-
41-
expect(details).to.have.property('PartNumber');
42-
expect(details.PartNumber).to.be.an.integer;
43-
44-
if (details.Key == 'upload-fail') {
45-
callback('Simulated failure from mocked API');
46-
}
47-
else {
48-
// Return an ETag
49-
callback(null, {
50-
ETag: 'etag'
51-
});
52-
}
53-
};
54-
55-
this.abortMultipartUpload = function (details, callback) {
56-
// Make sure that all the properties are there
57-
expect(details).to.have.property('Bucket');
58-
expect(details.Bucket).to.equal('test-bucket-name');
1+
var expect = require('chai').expect,
2+
fs = require('fs'),
3+
Writable = require('stream').Writable,
4+
AWSstub = require('../lib/AWSstub'),
5+
s3StreamClient = require('../lib/s3-upload-stream.js')(new AWSstub.S3());
596

60-
expect(details).to.have.property('Key');
61-
expect(details.Key).to.be.a('string');
62-
63-
expect(details).to.have.property('UploadId');
64-
expect(details.UploadId).to.contain('upload-id');
65-
66-
if (details.Key == 'abort-fail') {
67-
// Trigger a simulated error when a magic file name is used.
68-
callback('Simulated failure from mocked API');
69-
}
70-
else {
71-
callback();
72-
}
73-
};
74-
75-
this.completeMultipartUpload = function (details, callback) {
76-
// Make sure that all the properties are there
77-
expect(details).to.have.property('Bucket');
78-
expect(details.Bucket).to.equal('test-bucket-name');
79-
80-
expect(details).to.have.property('Key');
81-
expect(details.Key).to.be.a('string');
82-
83-
expect(details).to.have.property('UploadId');
84-
expect(details.UploadId).to.contain('upload-id');
85-
86-
expect(details).to.have.property('MultipartUpload');
87-
expect(details.MultipartUpload).to.an.object;
88-
89-
expect(details.MultipartUpload).to.have.property('Parts');
90-
expect(details.MultipartUpload.Parts).to.an.array;
91-
92-
details.MultipartUpload.Parts.forEach(function (partNumber) {
93-
expect(partNumber).to.be.an.integer;
94-
});
95-
96-
if (details.Key == 'complete-fail' || details.Key == 'abort-fail') {
97-
// Trigger a simulated error when a magic file name is used.
98-
callback('Simulated failure from mocked API');
99-
}
100-
else {
101-
callback(null, {
102-
ETag: 'etag'
103-
});
104-
}
105-
};
106-
}
107-
};
108-
109-
var s3StreamClient = require('../lib/s3-upload-stream.js')(new AWSstub.S3());
1107

1118
describe('Creating upload client', function () {
1129
describe('Without specifying an S3 client', function () {
@@ -120,7 +17,6 @@ describe('Creating upload client', function () {
12017
"Bucket": "test-bucket-name",
12118
"Key": "test-file-name"
12219
});
123-
12420
done();
12521
}
12622
catch (e) {

0 commit comments

Comments
 (0)