Skip to content

Commit

Permalink
Date generation functions (#3)
Browse files Browse the repository at this point in the history
* Reorganized test function configuration and converted functions to private by appending underscore to function names so as not to expose them

* Changed date string generation to reduce function calls

* removed unused function
  • Loading branch information
losthismind authored Mar 27, 2019
1 parent e82c93b commit b05d5d6
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
14 changes: 2 additions & 12 deletions aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,8 @@ var AWS = (function() {

var Crypto = loadCrypto();

var d = new Date();
var dateString = d.getUTCFullYear()
+ addZero(d.getUTCMonth()+1)
+ addZero(d.getUTCDate());
var dateTimeString = dateString + "T"
+ addZero(d.getUTCHours())
+ addZero(d.getUTCMinutes())
+ addZero(d.getUTCSeconds()) + "Z";
var dateTimeString = Utilities.formatDate(new Date(), "GMT", "yyyyMMdd'T'HHmmss'Z'");
var dateString = dateTimeString.substring(0, 8);

method = method || "GET";
uri = uri || "/";
Expand Down Expand Up @@ -201,10 +195,6 @@ var AWS = (function() {
return /[a-z0-9-_.~=&]/i.test(c);
}

function addZero(s) {
return (Number(s) < 10 ? "0" : "") + String(s);
}

/**
* Source: http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-jscript
*/
Expand Down
36 changes: 21 additions & 15 deletions aws.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
// Set the following to values that make sense for testing
var access_key = "";
var secret_key = "";
var bucket_name = "";
var region = "";
var name_of_object_that_exists = ""; // include leading slash
var name_of_object_that_does_not_exist = ""; // include leading slash
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 testAws_() {
testAwsGetObjectExists_();
testAwsGetObjectNotExist_();
}

function testAwsGetObjectExists() {
AWS.init(access_key, secret_key);
function testAwsGetObjectExists_() {
var config = testAwsConfig_();
AWS.init(config.access_key, config.secret_key);
try {
var contents = AWS.request("s3", region, "GetObject", undefined, "GET", undefined, undefined, name_of_object_that_exists, { "Bucket": bucket_name });
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 {
Expand All @@ -33,10 +38,11 @@ function testAwsGetObjectExists() {
}
}

function testAwsGetObjectNotExist() {
AWS.init(access_key, secret_key);
function testAwsGetObjectNotExist_() {
var config = testAwsConfig_();
AWS.init(config.access_key, config.secret_key);
try {
AWS.request("s3", region, "GetObject", undefined, "GET", undefined, undefined, name_of_object_that_does_not_exist, { "Bucket": bucket_name });
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();
Expand Down
63 changes: 36 additions & 27 deletions s3.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
// Set the following to values that make sense for testing
var access_key = "";
var secret_key = "";
var bucket_name = "";
var region = "";
var name_of_object_that_exists = ""; // do NOT include leading slash
var name_of_object_that_does_not_exist = ""; // do NOT include leading slash
var name_of_file_create = ""; // do NOT include leading slash
function testS3Config_() {
return {
"access_key": "",
"secret_key": "",
"bucket_name": "",
"region": "",
"name_of_object_that_exists": "", // do NOT include leading slash
"name_of_object_that_does_not_exist": "", // do NOT include leading slash
"name_of_file_create": "" // do NOT include leading slash
}
}

// Initialize connection:
// S3.init(access_key, secret_key);

function testS3() {
testS3ListAllBuckets();
testS3GetObjectExists();
testS3GetObjectNotExist();
testS3PutObject();
testS3DeleteObject();
function testS3_() {
testS3ListAllBuckets_();
testS3GetObjectExists_();
testS3GetObjectNotExist_();
testS3PutObject_();
testS3DeleteObject_();
}

function testS3ListAllBuckets() {
function testS3ListAllBuckets_() {
var config = testS3Config_();
try {
S3.init(access_key, secret_key);
S3.init(config.access_key, config.secret_key);
var result = S3.listAllMyBuckets();
Logger.log("testS3ListAllBuckets - Result:");
Logger.log(result);
Expand All @@ -32,10 +37,11 @@ function testS3ListAllBuckets() {
}
}

function testS3GetObjectExists() {
function testS3GetObjectExists_() {
var config = testS3Config_();
try {
S3.init(access_key, secret_key);
var contents = S3.getObject(bucket_name, name_of_object_that_exists, region);
S3.init(config.access_key, config.secret_key);
var contents = S3.getObject(config.bucket_name, config.name_of_object_that_exists, config.region);
if (contents == "file") {
Logger.log("testS3GetObjectExists - PASS");
} else {
Expand All @@ -48,10 +54,11 @@ function testS3GetObjectExists() {
}
}

function testS3GetObjectNotExist() {
function testS3GetObjectNotExist_() {
var config = testS3Config_();
try {
S3.init(access_key, secret_key);
S3.getObject(bucket_name, name_of_object_that_does_not_exist, region);
S3.init(config.access_key, config.secret_key);
S3.getObject(config.bucket_name, config.name_of_object_that_does_not_exist, config.region);
Logger.log("testS3GetObjectNotExist - FAIL - exception should have been thrown");
} catch(e) {
var message = e.toString();
Expand All @@ -64,10 +71,11 @@ function testS3GetObjectNotExist() {
}
}

function testS3PutObject() {
function testS3PutObject_() {
var config = testS3Config_();
try {
S3.init(access_key, secret_key);
S3.putObject(bucket_name, name_of_file_create, { "data": "contents" }, region);
S3.init(config.access_key, config.secret_key);
S3.putObject(config.bucket_name, config.name_of_file_create, { "data": "contents" }, config.region);
Logger.log("testS3PutObject - PASS");
} catch(e) {
var message = e.toString();
Expand All @@ -76,10 +84,11 @@ function testS3PutObject() {
}
}

function testS3DeleteObject() {
function testS3DeleteObject_() {
var config = testS3Config_();
try {
S3.init(access_key, secret_key);
S3.deleteObject(bucket_name, name_of_file_create, region);
S3.init(config.access_key, config.secret_key);
S3.deleteObject(config.bucket_name, config.name_of_file_create, config.region);
Logger.log("testS3DeleteObject - PASS");
} catch(e) {
var message = e.toString();
Expand Down

0 comments on commit b05d5d6

Please sign in to comment.