X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Frestclient%2FHTTPClient_test.go;h=20c26ddabd93fada3ccfcd6910ef483fc44ed82c;hb=6f48adb69090799c74c29204dd2cd1737cc9d6ac;hp=7fe3cc7a81a44b851bd41d0e6b78be9dbb8a94b2;hpb=f1cee0f81c6bc482f73182c8f4c903e8376381e8;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/restclient/HTTPClient_test.go b/dmaap-mediator-producer/internal/restclient/HTTPClient_test.go index 7fe3cc7a..20c26dda 100644 --- a/dmaap-mediator-producer/internal/restclient/HTTPClient_test.go +++ b/dmaap-mediator-producer/internal/restclient/HTTPClient_test.go @@ -22,12 +22,17 @@ package restclient import ( "bytes" + "crypto/tls" "errors" "fmt" "io/ioutil" + "math" "net/http" + "reflect" "testing" + "time" + "github.com/hashicorp/go-retryablehttp" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "oransc.org/nonrtric/dmaapmediatorproducer/mocks/httpclient" @@ -122,7 +127,7 @@ func TestPutOk(t *testing.T) { assertions.Equal(http.MethodPut, actualRequest.Method) assertions.Equal("http", actualRequest.URL.Scheme) assertions.Equal("localhost:9990", actualRequest.URL.Host) - assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type")) + assertions.Equal("application/json", actualRequest.Header.Get("Content-Type")) body, _ := ioutil.ReadAll(actualRequest.Body) expectedBody := []byte("body") assertions.Equal(expectedBody, body) @@ -148,7 +153,7 @@ func TestPostOk(t *testing.T) { assertions.Equal(http.MethodPost, actualRequest.Method) assertions.Equal("http", actualRequest.URL.Scheme) assertions.Equal("localhost:9990", actualRequest.URL.Host) - assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type")) + assertions.Equal("application/json", actualRequest.Header.Get("Content-Type")) body, _ := ioutil.ReadAll(actualRequest.Body) expectedBody := []byte("body") assertions.Equal(expectedBody, body) @@ -202,3 +207,73 @@ func Test_doErrorCases(t *testing.T) { }) } } + +func Test_createClientCertificate(t *testing.T) { + assertions := require.New(t) + wantedCert, _ := tls.LoadX509KeyPair("../../security/producer.crt", "../../security/producer.key") + type args struct { + certPath string + keyPath string + } + tests := []struct { + name string + args args + wantCert tls.Certificate + wantErr error + }{ + { + name: "Paths to cert info ok should return cerftificate", + args: args{ + certPath: "../../security/producer.crt", + keyPath: "../../security/producer.key", + }, + wantCert: wantedCert, + }, + { + name: "Paths to cert info not ok should return error with info about error", + args: args{ + certPath: "wrong_cert", + keyPath: "wrong_key", + }, + wantErr: fmt.Errorf("cannot create x509 keypair from cert file wrong_cert and key file wrong_key due to: open wrong_cert: no such file or directory"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cert, err := CreateClientCertificate(tt.args.certPath, tt.args.keyPath) + assertions.Equal(tt.wantCert, cert, tt.name) + assertions.Equal(tt.wantErr, err, tt.name) + }) + } +} + +func Test_CreateRetryClient(t *testing.T) { + assertions := require.New(t) + + client := CreateRetryClient(tls.Certificate{}) + + transport := client.Transport + assertions.Equal("*retryablehttp.RoundTripper", reflect.TypeOf(transport).String()) + retryableTransport := transport.(*retryablehttp.RoundTripper) + retryableClient := retryableTransport.Client + assertions.Equal(time.Minute, retryableClient.RetryWaitMax) + assertions.Equal(math.MaxInt, retryableClient.RetryMax) +} + +func Test_CreateClientWithoutRetry(t *testing.T) { + assertions := require.New(t) + + client := CreateClientWithoutRetry(tls.Certificate{}, 5*time.Second) + + transport := client.Transport + assertions.Equal("*http.Transport", reflect.TypeOf(transport).String()) + assertions.Equal(5*time.Second, client.Timeout) +} + +func TestIsUrlSecured(t *testing.T) { + assertions := require.New(t) + + assertions.True(IsUrlSecure("https://url")) + + assertions.False(IsUrlSecure("http://url")) +}