forked from losthismind/aws-apps-scripts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
aws.test.js
56 lines (52 loc) · 1.91 KB
/
aws.test.js
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
// Set the following to values that make sense for testing
function testAwsConfig_() {
return {
"access_key": "",
"secret_key": "",
"bucket_name": "",
"region": "",
"name_of_object_that_exists": "", // include leading slash
"name_of_object_that_does_not_exist": "" // include leading slash
}
}
// Initialize connection:
// AWS.init(access_key, secret_key);
//
// Call function:
// AWS.request(service, region, action, params, method, payload, headers, uri);
function testAws_() {
testAwsGetObjectExists_();
testAwsGetObjectNotExist_();
}
function testAwsGetObjectExists_() {
var config = testAwsConfig_();
AWS.init(config.access_key, config.secret_key);
try {
var contents = AWS.request("s3", config.region, "GetObject", undefined, "GET", undefined, undefined, config.name_of_object_that_exists, { "Bucket": config.bucket_name });
if (contents == "file") {
Logger.log("testAwsGetObjectExists - PASS");
} else {
Logger.log("testAwsGetObjectExists - FAIL - contents [" + contents + "] did not match expected result");
}
} catch(e) {
var message = e.toString();
Logger.log("testAwsGetObjectExists - FAIL - unexpected message [" + message + "]");
Logger.log(e);
}
}
function testAwsGetObjectNotExist_() {
var config = testAwsConfig_();
AWS.init(config.access_key, config.secret_key);
try {
AWS.request("s3", config.region, "GetObject", undefined, "GET", undefined, undefined, config.name_of_object_that_does_not_exist, { "Bucket": config.bucket_name });
Logger.log("testAwsGetObjectNotExist - FAIL - exception should have been thrown");
} catch(e) {
var message = e.toString();
if (message == "AWS Error - NoSuchKey: The specified key does not exist.") {
Logger.log("testAwsGetObjectNotExist - PASS");
} else {
Logger.log("testAwsGetObjectNotExist - FAIL - unexpected message [" + message + "]");
Logger.log(e);
}
}
}