-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathbucketGetCors.js
More file actions
136 lines (125 loc) · 4.67 KB
/
bucketGetCors.js
File metadata and controls
136 lines (125 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const assert = require('assert');
const crypto = require('crypto');
const { bucketPut } = require('../../../lib/api/bucketPut');
const bucketPutCors = require('../../../lib/api/bucketPutCors');
const bucketGetCors = require('../../../lib/api/bucketGetCors');
const { cleanup,
DummyRequestLogger,
makeAuthInfo }
= require('../helpers');
const log = new DummyRequestLogger();
const authInfo = makeAuthInfo('accessKey1');
const bucketName = 'bucketGetCorsTestBucket';
const testBucketPutRequest = {
bucketName,
headers: { host: `${bucketName}.s3.amazonaws.com` },
url: '/',
actionImplicitDenies: false,
};
function _makeCorsRequest(xml) {
const request = {
bucketName,
headers: {
host: `${bucketName}.s3.amazonaws.com`,
},
url: '/?cors',
query: { cors: '' },
actionImplicitDenies: false,
};
if (xml) {
request.post = xml;
request.headers['content-md5'] = crypto.createHash('md5')
.update(request.post, 'utf8').digest('base64');
}
return request;
}
const testGetCorsRequest = _makeCorsRequest();
function _comparePutGetXml(sampleXml, done) {
const fullXml = '<?xml version="1.0" encoding="UTF-8" ' +
'standalone="yes"?><CORSConfiguration>' +
`${sampleXml}</CORSConfiguration>`;
const testPutCorsRequest = _makeCorsRequest(fullXml);
bucketPutCors(authInfo, testPutCorsRequest, log, err => {
if (err) {
process.stdout.write(`Err putting cors config ${err}`);
return done(err);
}
return bucketGetCors(authInfo, testGetCorsRequest, log,
(err, res) => {
assert.strictEqual(err, null, `Unexpected err ${err}`);
assert.strictEqual(res, fullXml);
done();
});
});
}
describe('getBucketCors API', () => {
beforeEach(done => {
cleanup();
bucketPut(authInfo, testBucketPutRequest, log, done);
});
afterEach(() => cleanup());
it('should return same XML as uploaded for AllowedMethod and ' +
'AllowedOrigin', done => {
const sampleXml =
'<CORSRule>' +
'<AllowedMethod>PUT</AllowedMethod>' +
'<AllowedMethod>POST</AllowedMethod>' +
'<AllowedMethod>DELETE</AllowedMethod>' +
'<AllowedOrigin>http://www.example.com</AllowedOrigin>' +
'<AllowedOrigin>http://www.pusheen.com</AllowedOrigin>' +
'</CORSRule>';
_comparePutGetXml(sampleXml, done);
});
it('should return same XML as uploaded for multiple rules', done => {
const sampleXml =
'<CORSRule>' +
'<AllowedMethod>PUT</AllowedMethod>' +
'<AllowedOrigin>http://www.example.com</AllowedOrigin>' +
'</CORSRule>' +
'<CORSRule>' +
'<AllowedMethod>POST</AllowedMethod>' +
'<AllowedOrigin>http://www.pusheen.com</AllowedOrigin>' +
'</CORSRule>';
_comparePutGetXml(sampleXml, done);
});
it('should return same XML as uploaded for AllowedHeader\'s', done => {
const sampleXml =
'<CORSRule>' +
'<AllowedMethod>PUT</AllowedMethod>' +
'<AllowedOrigin>http://www.example.com</AllowedOrigin>' +
'<AllowedHeader>Content-Length</AllowedHeader>' +
'<AllowedHeader>Expires</AllowedHeader>' +
'<AllowedHeader>Content-Encoding</AllowedHeader>' +
'</CORSRule>';
_comparePutGetXml(sampleXml, done);
});
it('should return same XML as uploaded for ExposedHeader\'s', done => {
const sampleXml =
'<CORSRule>' +
'<AllowedMethod>PUT</AllowedMethod>' +
'<AllowedOrigin>http://www.example.com</AllowedOrigin>' +
'<ExposeHeader>Content-Length</ExposeHeader>' +
'<ExposeHeader>Expires</ExposeHeader>' +
'<ExposeHeader>Content-Encoding</ExposeHeader>' +
'</CORSRule>';
_comparePutGetXml(sampleXml, done);
});
it('should return same XML as uploaded for ID', done => {
const sampleXml =
'<CORSRule>' +
'<AllowedMethod>PUT</AllowedMethod>' +
'<AllowedOrigin>http://www.example.com</AllowedOrigin>' +
'<ID>testid</ID>' +
'</CORSRule>';
_comparePutGetXml(sampleXml, done);
});
it('should return same XML as uploaded for MaxAgeSeconds', done => {
const sampleXml =
'<CORSRule>' +
'<AllowedMethod>PUT</AllowedMethod>' +
'<AllowedOrigin>http://www.example.com</AllowedOrigin>' +
'<MaxAgeSeconds>600</MaxAgeSeconds>' +
'</CORSRule>';
_comparePutGetXml(sampleXml, done);
});
});