dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/common_test.go (about)

     1  //
     2  // Copyright (C) 2020-2021 IOTech Ltd
     3  // Copyright (C) 2023 Intel Corporation
     4  //
     5  // SPDX-License-Identifier: Apache-2.0
     6  
     7  package http
     8  
     9  import (
    10  	"context"
    11  	"encoding/json"
    12  	"net/http"
    13  	"net/http/httptest"
    14  	"testing"
    15  
    16  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    17  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  const (
    22  	TestUnexpectedMsgFormatStr = "unexpected result, active: '%s' but expected: '%s'"
    23  )
    24  
    25  func TestGetConfig(t *testing.T) {
    26  	expected := dtoCommon.ConfigResponse{}
    27  	ts := newTestServer(http.MethodGet, common.ApiConfigRoute, dtoCommon.ConfigResponse{})
    28  	defer ts.Close()
    29  
    30  	gc := NewCommonClient(ts.URL, NewNullAuthenticationInjector())
    31  	response, err := gc.Configuration(context.Background())
    32  	require.NoError(t, err)
    33  	require.Equal(t, expected, response)
    34  }
    35  
    36  func TestPing(t *testing.T) {
    37  	expected := dtoCommon.PingResponse{}
    38  	ts := newTestServer(http.MethodGet, common.ApiPingRoute, dtoCommon.PingResponse{})
    39  	defer ts.Close()
    40  
    41  	gc := NewCommonClient(ts.URL, NewNullAuthenticationInjector())
    42  	response, err := gc.Ping(context.Background())
    43  	require.NoError(t, err)
    44  	require.Equal(t, expected, response)
    45  }
    46  
    47  func TestVersion(t *testing.T) {
    48  	expected := dtoCommon.VersionResponse{}
    49  	ts := newTestServer(http.MethodGet, common.ApiVersionRoute, dtoCommon.VersionResponse{})
    50  	defer ts.Close()
    51  
    52  	gc := NewCommonClient(ts.URL, NewNullAuthenticationInjector())
    53  	response, err := gc.Version(context.Background())
    54  	require.NoError(t, err)
    55  	require.Equal(t, expected, response)
    56  }
    57  
    58  func TestAddSecret(t *testing.T) {
    59  	expected := dtoCommon.BaseResponse{}
    60  	req := dtoCommon.NewSecretRequest(
    61  		"testSecretName",
    62  		[]dtoCommon.SecretDataKeyValue{
    63  			{Key: "username", Value: "tester"},
    64  			{Key: "password", Value: "123"},
    65  		},
    66  	)
    67  	ts := newTestServer(http.MethodPost, common.ApiSecretRoute, expected)
    68  	defer ts.Close()
    69  
    70  	client := NewCommonClient(ts.URL, NewNullAuthenticationInjector())
    71  	res, err := client.AddSecret(context.Background(), req)
    72  	require.NoError(t, err)
    73  	require.IsType(t, expected, res)
    74  }
    75  
    76  func newTestServer(httpMethod string, apiRoute string, expectedResponse interface{}) *httptest.Server {
    77  	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    78  		if r.Method != httpMethod {
    79  			w.WriteHeader(http.StatusMethodNotAllowed)
    80  			return
    81  		}
    82  		if r.URL.EscapedPath() != apiRoute {
    83  			w.WriteHeader(http.StatusBadRequest)
    84  			return
    85  		}
    86  
    87  		w.WriteHeader(http.StatusOK)
    88  		b, _ := json.Marshal(expectedResponse)
    89  		_, _ = w.Write(b)
    90  	}))
    91  }