2 * Copyright 2018 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 smsbackend "sms/backend"
31 // Here we are using the anonymous variable feature of golang to
32 // override methods form an interface
33 type TestBackend struct {
34 smsbackend.SecretBackend
37 func (b *TestBackend) Init() error {
41 func (b *TestBackend) GetStatus() (bool, error) {
45 func (b *TestBackend) GetSecretDomain(name string) (smsbackend.SecretDomain, error) {
46 return smsbackend.SecretDomain{}, nil
49 func (b *TestBackend) GetSecret(dom string, sec string) (smsbackend.Secret, error) {
50 return smsbackend.Secret{}, nil
53 func (b *TestBackend) CreateSecretDomain(name string) (smsbackend.SecretDomain, error) {
54 return smsbackend.SecretDomain{}, nil
57 func (b *TestBackend) CreateSecret(dom string, sec smsbackend.Secret) error {
61 func (b *TestBackend) DeleteSecretDomain(name string) error {
65 func (b *TestBackend) DeleteSecret(dom string, name string) error {
70 testBackend := &TestBackend{}
71 h = handler{secretBackend: testBackend}
74 func TestCreateRouter(t *testing.T) {
75 router := CreateRouter(h.secretBackend)
77 t.Fatal("CreateRouter: Got error when none expected")
81 func TestStatusHandler(t *testing.T) {
82 req, err := http.NewRequest("GET", "/v1/sms/status", nil)
87 rr := httptest.NewRecorder()
88 hr := http.HandlerFunc(h.statusHandler)
93 if ret != http.StatusOK {
94 t.Errorf("statusHandler returned wrong status code: %v vs %v",
98 expected := backendStatus{}
99 got := backendStatus{}
100 expectedStr := strings.NewReader(`{"sealstatus":true}`)
101 json.NewDecoder(expectedStr).Decode(&expected)
102 json.NewDecoder(rr.Body).Decode(&got)
104 if reflect.DeepEqual(expected, got) == false {
105 t.Errorf("statusHandler returned unexpected body: got %v vs %v",
106 rr.Body.String(), expectedStr)