2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // ========================LICENSE_END===================================
33 "github.com/hashicorp/go-retryablehttp"
34 "github.com/stretchr/testify/mock"
35 "github.com/stretchr/testify/require"
36 "oransc.org/usecase/oruclosedloop/mocks"
39 func TestRequestError_Error(t *testing.T) {
40 assertions := require.New(t)
42 actualError := RequestError{
43 StatusCode: http.StatusBadRequest,
44 Body: []byte("error"),
46 assertions.Equal("error response with status: 400 and body: error", actualError.Error())
49 func TestPutWithoutAuth(t *testing.T) {
50 assertions := require.New(t)
52 clientMock := mocks.HTTPClient{}
53 clientMock.On("Do", mock.Anything).Return(&http.Response{
54 StatusCode: http.StatusOK,
57 error := PutWithoutAuth("url", []byte("body"), &clientMock)
60 var actualRequest *http.Request
61 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
65 assertions.Equal(http.MethodPut, actualRequest.Method)
66 assertions.Equal("url", actualRequest.URL.Path)
67 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
68 assertions.Empty(actualRequest.Header.Get("Authorization"))
69 body, _ := ioutil.ReadAll(actualRequest.Body)
70 expectedBody := []byte("body")
71 assertions.Equal(expectedBody, body)
72 clientMock.AssertNumberOfCalls(t, "Do", 1)
75 func TestPut(t *testing.T) {
76 assertions := require.New(t)
78 clientMock := mocks.HTTPClient{}
79 clientMock.On("Do", mock.Anything).Return(&http.Response{
80 StatusCode: http.StatusOK,
83 error := Put("url", "body", &clientMock, "admin", "pwd")
86 var actualRequest *http.Request
87 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
91 assertions.Equal(http.MethodPut, actualRequest.Method)
92 assertions.Equal("url", actualRequest.URL.Path)
93 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
94 tempRequest, _ := http.NewRequest("", "", nil)
95 tempRequest.SetBasicAuth("admin", "pwd")
96 assertions.Equal(tempRequest.Header.Get("Authorization"), actualRequest.Header.Get("Authorization"))
97 body, _ := ioutil.ReadAll(actualRequest.Body)
98 expectedBody := []byte("body")
99 assertions.Equal(expectedBody, body)
100 clientMock.AssertNumberOfCalls(t, "Do", 1)
103 func TestDelete(t *testing.T) {
104 assertions := require.New(t)
106 clientMock := mocks.HTTPClient{}
107 clientMock.On("Do", mock.Anything).Return(&http.Response{
108 StatusCode: http.StatusOK,
111 error := Delete("url", &clientMock)
113 assertions.Nil(error)
114 var actualRequest *http.Request
115 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
119 assertions.Equal(http.MethodDelete, actualRequest.Method)
120 assertions.Equal("url", actualRequest.URL.Path)
121 assertions.Empty(actualRequest.Header.Get("Content-Type"))
122 assertions.Empty(actualRequest.Header.Get("Authorization"))
123 assertions.Equal(http.NoBody, actualRequest.Body)
124 clientMock.AssertNumberOfCalls(t, "Do", 1)
127 func Test_doErrorCases(t *testing.T) {
128 assertions := require.New(t)
133 mockReturnBody []byte
134 mockReturnError error
142 name: "Bad request should get RequestError",
145 mockReturnStatus: http.StatusBadRequest,
146 mockReturnBody: []byte("bad request"),
148 wantErr: RequestError{
149 StatusCode: http.StatusBadRequest,
150 Body: []byte("bad request"),
154 name: "Server unavailable should get error",
156 url: "serverUnavailable",
157 mockReturnError: fmt.Errorf("Server unavailable"),
159 wantErr: fmt.Errorf("Server unavailable"),
162 for _, tt := range tests {
163 t.Run(tt.name, func(t *testing.T) {
164 clientMock := mocks.HTTPClient{}
165 clientMock.On("Do", mock.Anything).Return(&http.Response{
166 StatusCode: tt.args.mockReturnStatus,
167 Body: ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
168 }, tt.args.mockReturnError)
170 err := do("PUT", tt.args.url, nil, &clientMock)
171 assertions.Equal(tt.wantErr, err, tt.name)
176 func Test_createClientCertificate(t *testing.T) {
177 assertions := require.New(t)
178 wantedCert, _ := tls.LoadX509KeyPair("../../security/consumer.crt", "../../security/consumer.key")
186 wantCert tls.Certificate
190 name: "Paths to cert info ok should return cerftificate",
192 certPath: "../../security/consumer.crt",
193 keyPath: "../../security/consumer.key",
195 wantCert: wantedCert,
198 name: "Paths to cert info not ok should return error with info about error",
200 certPath: "wrong_cert",
201 keyPath: "wrong_key",
203 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"),
206 for _, tt := range tests {
207 t.Run(tt.name, func(t *testing.T) {
208 cert, err := CreateClientCertificate(tt.args.certPath, tt.args.keyPath)
209 assertions.Equal(tt.wantCert, cert, tt.name)
210 assertions.Equal(tt.wantErr, err, tt.name)
215 func Test_CreateRetryClient(t *testing.T) {
216 assertions := require.New(t)
218 client := CreateRetryClient(tls.Certificate{})
220 transport := client.Transport
221 assertions.Equal("*retryablehttp.RoundTripper", reflect.TypeOf(transport).String())
222 retryableTransport := transport.(*retryablehttp.RoundTripper)
223 retryableClient := retryableTransport.Client
224 assertions.Equal(time.Minute, retryableClient.RetryWaitMax)
225 assertions.Equal(math.MaxInt, retryableClient.RetryMax)
228 func TestIsUrlSecured(t *testing.T) {
229 assertions := require.New(t)
231 assertions.True(IsUrlSecure("https://url"))
233 assertions.False(IsUrlSecure("http://url"))