github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/utils_test.go (about)

     1  package aws
     2  
     3  import "testing"
     4  
     5  var base64encodingTests = []struct {
     6  	in  []byte
     7  	out string
     8  }{
     9  	// normal encoding case
    10  	{[]byte("data should be encoded"), "ZGF0YSBzaG91bGQgYmUgZW5jb2RlZA=="},
    11  	// base64 encoded input should result in no change of output
    12  	{[]byte("ZGF0YSBzaG91bGQgYmUgZW5jb2RlZA=="), "ZGF0YSBzaG91bGQgYmUgZW5jb2RlZA=="},
    13  }
    14  
    15  func TestBase64Encode(t *testing.T) {
    16  	for _, tt := range base64encodingTests {
    17  		out := base64Encode(tt.in)
    18  		if out != tt.out {
    19  			t.Errorf("base64Encode(%s) => %s, want %s", tt.in, out, tt.out)
    20  		}
    21  	}
    22  }
    23  
    24  func TestLooksLikeJsonString(t *testing.T) {
    25  	looksLikeJson := ` {"abc":"1"} `
    26  	doesNotLookLikeJson := `abc: 1`
    27  
    28  	if !looksLikeJsonString(looksLikeJson) {
    29  		t.Errorf("Expected looksLikeJson to return true for %s", looksLikeJson)
    30  	}
    31  	if looksLikeJsonString(doesNotLookLikeJson) {
    32  		t.Errorf("Expected looksLikeJson to return false for %s", doesNotLookLikeJson)
    33  	}
    34  }
    35  
    36  func TestJsonBytesEqualQuotedAndUnquoted(t *testing.T) {
    37  	unquoted := `{"test": "test"}`
    38  	quoted := "{\"test\": \"test\"}"
    39  
    40  	if !jsonBytesEqual([]byte(unquoted), []byte(quoted)) {
    41  		t.Errorf("Expected jsonBytesEqual to return true for %s == %s", unquoted, quoted)
    42  	}
    43  
    44  	unquotedDiff := `{"test": "test"}`
    45  	quotedDiff := "{\"test\": \"tested\"}"
    46  
    47  	if jsonBytesEqual([]byte(unquotedDiff), []byte(quotedDiff)) {
    48  		t.Errorf("Expected jsonBytesEqual to return false for %s == %s", unquotedDiff, quotedDiff)
    49  	}
    50  }
    51  
    52  func TestJsonBytesEqualWhitespaceAndNoWhitespace(t *testing.T) {
    53  	noWhitespace := `{"test":"test"}`
    54  	whitespace := `
    55  {
    56    "test": "test"
    57  }`
    58  
    59  	if !jsonBytesEqual([]byte(noWhitespace), []byte(whitespace)) {
    60  		t.Errorf("Expected jsonBytesEqual to return true for %s == %s", noWhitespace, whitespace)
    61  	}
    62  
    63  	noWhitespaceDiff := `{"test":"test"}`
    64  	whitespaceDiff := `
    65  {
    66    "test": "tested"
    67  }`
    68  
    69  	if jsonBytesEqual([]byte(noWhitespaceDiff), []byte(whitespaceDiff)) {
    70  		t.Errorf("Expected jsonBytesEqual to return false for %s == %s", noWhitespaceDiff, whitespaceDiff)
    71  	}
    72  }