|
14 | 14 | package config |
15 | 15 |
|
16 | 16 | import ( |
| 17 | + "bytes" |
17 | 18 | "encoding/json" |
| 19 | + "net/http" |
| 20 | + "reflect" |
18 | 21 | "testing" |
| 22 | + |
| 23 | + "gopkg.in/yaml.v2" |
19 | 24 | ) |
20 | 25 |
|
21 | 26 | func TestJSONMarshalSecret(t *testing.T) { |
@@ -51,3 +56,110 @@ func TestJSONMarshalSecret(t *testing.T) { |
51 | 56 | }) |
52 | 57 | } |
53 | 58 | } |
| 59 | + |
| 60 | +func TestHeaderHttpHeader(t *testing.T) { |
| 61 | + testcases := map[string]struct { |
| 62 | + header Header |
| 63 | + expected http.Header |
| 64 | + }{ |
| 65 | + "basic": { |
| 66 | + header: Header{ |
| 67 | + "single": []Secret{"v1"}, |
| 68 | + "multi": []Secret{"v1", "v2"}, |
| 69 | + "empty": []Secret{}, |
| 70 | + "nil": nil, |
| 71 | + }, |
| 72 | + expected: http.Header{ |
| 73 | + "single": []string{"v1"}, |
| 74 | + "multi": []string{"v1", "v2"}, |
| 75 | + "empty": []string{}, |
| 76 | + "nil": nil, |
| 77 | + }, |
| 78 | + }, |
| 79 | + "nil": { |
| 80 | + header: nil, |
| 81 | + expected: nil, |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + for name, tc := range testcases { |
| 86 | + t.Run(name, func(t *testing.T) { |
| 87 | + actual := tc.header.HttpHeader() |
| 88 | + if !reflect.DeepEqual(actual, tc.expected) { |
| 89 | + t.Fatalf("expecting: %#v, actual: %#v", tc.expected, actual) |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestHeaderUnmarshal(t *testing.T) { |
| 96 | + testcases := map[string]struct { |
| 97 | + input string |
| 98 | + expected Header |
| 99 | + }{ |
| 100 | + "void": { |
| 101 | + input: ``, |
| 102 | + }, |
| 103 | + "simple": { |
| 104 | + input: "single:\n- a\n", |
| 105 | + expected: Header{"single": []Secret{"a"}}, |
| 106 | + }, |
| 107 | + "multi": { |
| 108 | + input: "multi:\n- a\n- b\n", |
| 109 | + expected: Header{"multi": []Secret{"a", "b"}}, |
| 110 | + }, |
| 111 | + "empty": { |
| 112 | + input: "empty:\n", |
| 113 | + expected: Header{"empty": nil}, |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for name, tc := range testcases { |
| 118 | + t.Run(name, func(t *testing.T) { |
| 119 | + var actual Header |
| 120 | + err := yaml.Unmarshal([]byte(tc.input), &actual) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("error unmarshaling %s: %s", tc.input, err) |
| 123 | + } |
| 124 | + if !reflect.DeepEqual(actual, tc.expected) { |
| 125 | + t.Fatalf("expecting: %#v, actual: %#v", tc.expected, actual) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestHeaderMarshal(t *testing.T) { |
| 132 | + testcases := map[string]struct { |
| 133 | + input Header |
| 134 | + expected []byte |
| 135 | + }{ |
| 136 | + "void": { |
| 137 | + input: nil, |
| 138 | + expected: []byte("{}\n"), |
| 139 | + }, |
| 140 | + "simple": { |
| 141 | + input: Header{"single": []Secret{"a"}}, |
| 142 | + expected: []byte("single:\n- <secret>\n"), |
| 143 | + }, |
| 144 | + "multi": { |
| 145 | + input: Header{"multi": []Secret{"a", "b"}}, |
| 146 | + expected: []byte("multi:\n- <secret>\n- <secret>\n"), |
| 147 | + }, |
| 148 | + "empty": { |
| 149 | + input: Header{"empty": nil}, |
| 150 | + expected: []byte("empty: []\n"), |
| 151 | + }, |
| 152 | + } |
| 153 | + |
| 154 | + for name, tc := range testcases { |
| 155 | + t.Run(name, func(t *testing.T) { |
| 156 | + actual, err := yaml.Marshal(tc.input) |
| 157 | + if err != nil { |
| 158 | + t.Fatalf("error unmarshaling %#v: %s", tc.input, err) |
| 159 | + } |
| 160 | + if !bytes.Equal(actual, tc.expected) { |
| 161 | + t.Fatalf("expecting: %q, actual: %q", tc.expected, actual) |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
0 commit comments