From b05d5d66dbffea32c9cdd67539ea7b5f286d6839 Mon Sep 17 00:00:00 2001 From: losthismind Date: Wed, 27 Mar 2019 15:59:52 -0400 Subject: [PATCH] Date generation functions (#3) * 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 --- aws.js | 14 ++---------- aws.test.js | 36 +++++++++++++++++------------- s3.test.js | 63 ++++++++++++++++++++++++++++++----------------------- 3 files changed, 59 insertions(+), 54 deletions(-) diff --git a/aws.js b/aws.js index 50f6c1d..e190d59 100644 --- a/aws.js +++ b/aws.js @@ -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 || "/"; @@ -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 */ diff --git a/aws.test.js b/aws.test.js index 3ac5c01..ed0f6a8 100644 --- a/aws.test.js +++ b/aws.test.js @@ -1,10 +1,14 @@ // 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); @@ -12,15 +16,16 @@ var name_of_object_that_does_not_exist = ""; // include leading slash // 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 { @@ -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(); diff --git a/s3.test.js b/s3.test.js index ce2f3b9..3824eec 100644 --- a/s3.test.js +++ b/s3.test.js @@ -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); @@ -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 { @@ -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(); @@ -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(); @@ -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();