github.com/sacloud/iaas-api-go@v1.12.0/naked/proxylb_test.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package naked 16 17 import ( 18 "encoding/json" 19 "reflect" 20 "testing" 21 ) 22 23 func TestProxyLBCertificates_UnmarshalJSON(t *testing.T) { 24 cases := []struct { 25 in *ProxyLBCertificates 26 expect string 27 }{ 28 { 29 in: &ProxyLBCertificates{ 30 PrimaryCert: &ProxyLBCertificate{ 31 ServerCertificate: "aaa", 32 IntermediateCertificate: "bbb", 33 PrivateKey: "ccc", 34 }, 35 }, 36 expect: `{"PrimaryCert":{"ServerCertificate":"aaa","IntermediateCertificate":"bbb","PrivateKey":"ccc"},"AdditionalCerts":[]}`, 37 }, 38 { 39 in: &ProxyLBCertificates{ 40 PrimaryCert: &ProxyLBCertificate{ 41 ServerCertificate: "aaa", 42 IntermediateCertificate: "bbb", 43 PrivateKey: "ccc", 44 }, 45 AdditionalCerts: ProxyLBAdditionalCerts{ 46 { 47 ServerCertificate: "aaa", 48 IntermediateCertificate: "bbb", 49 PrivateKey: "ccc", 50 }, 51 }, 52 }, 53 expect: `{"PrimaryCert":{"ServerCertificate":"aaa","IntermediateCertificate":"bbb","PrivateKey":"ccc"},"AdditionalCerts":[{"ServerCertificate":"aaa","IntermediateCertificate":"bbb","PrivateKey":"ccc"}]}`, 54 }, 55 } 56 57 for _, tc := range cases { 58 data, err := json.Marshal(tc.in) 59 if err != nil { 60 t.Fatal(err) 61 } 62 if string(data) != tc.expect { 63 t.Fatalf("got unexpected JSON: expected: %s got: %s", tc.expect, data) 64 } 65 } 66 } 67 68 func TestProxyLBACMESetting_UnmarshalJSON(t *testing.T) { 69 type fields struct { 70 Enabled bool 71 CommonName string 72 SubjectAltNames []string 73 } 74 type args struct { 75 data []byte 76 } 77 tests := []struct { 78 name string 79 fields fields 80 args args 81 wantErr bool 82 }{ 83 { 84 name: "unmarshal", 85 fields: fields{ 86 Enabled: true, 87 CommonName: "example.com", 88 SubjectAltNames: []string{"test1.example.com", "test2.example.com", "test3.example.com", "test4.example.com"}, 89 }, 90 args: args{ 91 data: []byte(`{"Enabled":true,"CommonName":"example.com","SubjectAltNames":"test1.example.com\ntest2.example.com,test3.example.com test4.example.com"}`), 92 }, 93 wantErr: false, 94 }, 95 } 96 for _, tt := range tests { 97 t.Run(tt.name, func(t *testing.T) { 98 p := &ProxyLBACMESetting{ 99 Enabled: tt.fields.Enabled, 100 CommonName: tt.fields.CommonName, 101 SubjectAltNames: tt.fields.SubjectAltNames, 102 } 103 if err := p.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { 104 t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) 105 } 106 expect := &ProxyLBACMESetting{ 107 Enabled: tt.fields.Enabled, 108 CommonName: tt.fields.CommonName, 109 SubjectAltNames: tt.fields.SubjectAltNames, 110 } 111 if !reflect.DeepEqual(p, expect) { 112 t.Errorf("MarshalJSON() got = %v, want %v", p, expect) 113 } 114 }) 115 } 116 } 117 118 func TestProxyLBACMESetting_MarshalJSON(t *testing.T) { 119 type fields struct { 120 Enabled bool 121 CommonName string 122 SubjectAltNames []string 123 } 124 tests := []struct { 125 name string 126 fields fields 127 want []byte 128 wantErr bool 129 }{ 130 { 131 name: "marshal", 132 fields: fields{ 133 Enabled: true, 134 CommonName: "example.com", 135 SubjectAltNames: []string{"test1.example.com", "test2.example.com", "test3.example.com", "test4.example.com"}, 136 }, 137 want: []byte(`{"Enabled":true,"CommonName":"example.com","SubjectAltNames":"test1.example.com,test2.example.com,test3.example.com,test4.example.com"}`), 138 wantErr: false, 139 }, 140 } 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 p := ProxyLBACMESetting{ 144 Enabled: tt.fields.Enabled, 145 CommonName: tt.fields.CommonName, 146 SubjectAltNames: tt.fields.SubjectAltNames, 147 } 148 got, err := p.MarshalJSON() 149 if (err != nil) != tt.wantErr { 150 t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr) 151 return 152 } 153 if !reflect.DeepEqual(got, tt.want) { 154 t.Errorf("MarshalJSON() got = %v, want %v", string(got), string(tt.want)) 155 } 156 }) 157 } 158 }