90db6ae41813b81c1ad0d7d29cc32b25699870e9
[nonrtric.git] / dmaap-mediator-producer / internal / restclient / HTTPClient_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
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 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package restclient
22
23 import (
24         "bytes"
25         "crypto/tls"
26         "errors"
27         "fmt"
28         "io/ioutil"
29         "math"
30         "net/http"
31         "reflect"
32         "testing"
33         "time"
34
35         "github.com/hashicorp/go-retryablehttp"
36         "github.com/stretchr/testify/mock"
37         "github.com/stretchr/testify/require"
38         "oransc.org/nonrtric/dmaapmediatorproducer/mocks/httpclient"
39 )
40
41 func TestRequestError_Error(t *testing.T) {
42         assertions := require.New(t)
43         actualError := RequestError{
44                 StatusCode: http.StatusBadRequest,
45                 Body:       []byte("error"),
46         }
47         assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
48 }
49 func TestGet(t *testing.T) {
50         assertions := require.New(t)
51         type args struct {
52                 url              string
53                 mockReturnStatus int
54                 mockReturnBody   string
55                 mockReturnError  error
56         }
57         tests := []struct {
58                 name        string
59                 args        args
60                 want        []byte
61                 wantedError error
62         }{
63                 {
64                         name: "Test Get with OK response",
65                         args: args{
66                                 url:              "http://testOk",
67                                 mockReturnStatus: http.StatusOK,
68                                 mockReturnBody:   "Response",
69                         },
70                         want: []byte("Response"),
71                 },
72                 {
73                         name: "Test Get with Not OK response",
74                         args: args{
75                                 url:              "http://testNotOk",
76                                 mockReturnStatus: http.StatusBadRequest,
77                                 mockReturnBody:   "Bad Response",
78                         },
79                         want: nil,
80                         wantedError: RequestError{
81                                 StatusCode: http.StatusBadRequest,
82                                 Body:       []byte("Bad Response"),
83                         },
84                 },
85                 {
86                         name: "Test Get with error",
87                         args: args{
88                                 url:             "http://testError",
89                                 mockReturnError: errors.New("Failed Request"),
90                         },
91                         want:        nil,
92                         wantedError: errors.New("Failed Request"),
93                 },
94         }
95         for _, tt := range tests {
96                 t.Run(tt.name, func(t *testing.T) {
97                         clientMock := httpclient.HTTPClient{}
98                         clientMock.On("Get", tt.args.url).Return(&http.Response{
99                                 StatusCode: tt.args.mockReturnStatus,
100                                 Body:       ioutil.NopCloser(bytes.NewReader([]byte(tt.args.mockReturnBody))),
101                         }, tt.args.mockReturnError)
102
103                         got, err := Get(tt.args.url, &clientMock)
104                         assertions.Equal(tt.wantedError, err, tt.name)
105                         assertions.Equal(tt.want, got, tt.name)
106                         clientMock.AssertCalled(t, "Get", tt.args.url)
107                 })
108         }
109 }
110
111 func TestPutOk(t *testing.T) {
112         assertions := require.New(t)
113         clientMock := httpclient.HTTPClient{}
114
115         clientMock.On("Do", mock.Anything).Return(&http.Response{
116                 StatusCode: http.StatusOK,
117         }, nil)
118
119         if err := Put("http://localhost:9990", []byte("body"), &clientMock); err != nil {
120                 t.Errorf("Put() error = %v, did not want error", err)
121         }
122         var actualRequest *http.Request
123         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
124                 actualRequest = req
125                 return true
126         }))
127         assertions.Equal(http.MethodPut, actualRequest.Method)
128         assertions.Equal("http", actualRequest.URL.Scheme)
129         assertions.Equal("localhost:9990", actualRequest.URL.Host)
130         assertions.Equal("application/json", actualRequest.Header.Get("Content-Type"))
131         body, _ := ioutil.ReadAll(actualRequest.Body)
132         expectedBody := []byte("body")
133         assertions.Equal(expectedBody, body)
134         clientMock.AssertNumberOfCalls(t, "Do", 1)
135 }
136
137 func TestPostOk(t *testing.T) {
138         assertions := require.New(t)
139         clientMock := httpclient.HTTPClient{}
140
141         clientMock.On("Do", mock.Anything).Return(&http.Response{
142                 StatusCode: http.StatusOK,
143         }, nil)
144
145         if err := Post("http://localhost:9990", []byte("body"), "application/json", &clientMock); err != nil {
146                 t.Errorf("Put() error = %v, did not want error", err)
147         }
148         var actualRequest *http.Request
149         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
150                 actualRequest = req
151                 return true
152         }))
153         assertions.Equal(http.MethodPost, actualRequest.Method)
154         assertions.Equal("http", actualRequest.URL.Scheme)
155         assertions.Equal("localhost:9990", actualRequest.URL.Host)
156         assertions.Equal("application/json", actualRequest.Header.Get("Content-Type"))
157         body, _ := ioutil.ReadAll(actualRequest.Body)
158         expectedBody := []byte("body")
159         assertions.Equal(expectedBody, body)
160         clientMock.AssertNumberOfCalls(t, "Do", 1)
161 }
162
163 func Test_doErrorCases(t *testing.T) {
164         assertions := require.New(t)
165         type args struct {
166                 url              string
167                 mockReturnStatus int
168                 mockReturnBody   []byte
169                 mockReturnError  error
170         }
171         tests := []struct {
172                 name    string
173                 args    args
174                 wantErr error
175         }{
176                 {
177                         name: "Bad request should get RequestError",
178                         args: args{
179                                 url:              "badRequest",
180                                 mockReturnStatus: http.StatusBadRequest,
181                                 mockReturnBody:   []byte("bad request"),
182                                 mockReturnError:  nil,
183                         },
184                         wantErr: RequestError{
185                                 StatusCode: http.StatusBadRequest,
186                                 Body:       []byte("bad request"),
187                         },
188                 },
189                 {
190                         name: "Server unavailable should get error",
191                         args: args{
192                                 url:             "serverUnavailable",
193                                 mockReturnError: fmt.Errorf("Server unavailable"),
194                         },
195                         wantErr: fmt.Errorf("Server unavailable"),
196                 },
197         }
198         for _, tt := range tests {
199                 t.Run(tt.name, func(t *testing.T) {
200                         clientMock := httpclient.HTTPClient{}
201                         clientMock.On("Do", mock.Anything).Return(&http.Response{
202                                 StatusCode: tt.args.mockReturnStatus,
203                                 Body:       ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
204                         }, tt.args.mockReturnError)
205                         err := do("PUT", tt.args.url, nil, "", &clientMock)
206                         assertions.Equal(tt.wantErr, err, tt.name)
207                 })
208         }
209 }
210
211 func Test_createClientCertificate(t *testing.T) {
212         assertions := require.New(t)
213         wantedCert, _ := tls.LoadX509KeyPair("../../security/producer.crt", "../../security/producer.key")
214         type args struct {
215                 certPath string
216                 keyPath  string
217         }
218         tests := []struct {
219                 name     string
220                 args     args
221                 wantCert tls.Certificate
222                 wantErr  error
223         }{
224                 {
225                         name: "Paths to cert info ok should return cerftificate",
226                         args: args{
227                                 certPath: "../../security/producer.crt",
228                                 keyPath:  "../../security/producer.key",
229                         },
230                         wantCert: wantedCert,
231                 },
232                 {
233                         name: "Paths to cert info not ok should return error with info about error",
234                         args: args{
235                                 certPath: "wrong_cert",
236                                 keyPath:  "wrong_key",
237                         },
238                         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"),
239                 },
240         }
241         for _, tt := range tests {
242                 t.Run(tt.name, func(t *testing.T) {
243                         cert, err := CreateClientCertificate(tt.args.certPath, tt.args.keyPath)
244                         assertions.Equal(tt.wantCert, cert, tt.name)
245                         assertions.Equal(tt.wantErr, err, tt.name)
246                 })
247         }
248 }
249
250 func Test_CreateRetryClient(t *testing.T) {
251         assertions := require.New(t)
252
253         client := CreateRetryClient(tls.Certificate{})
254
255         transport := client.Transport
256         assertions.Equal("*retryablehttp.RoundTripper", reflect.TypeOf(transport).String())
257         retryableTransport := transport.(*retryablehttp.RoundTripper)
258         retryableClient := retryableTransport.Client
259         assertions.Equal(time.Minute, retryableClient.RetryWaitMax)
260         assertions.Equal(math.MaxInt, retryableClient.RetryMax)
261 }
262
263 func Test_CreateClientWithoutRetry(t *testing.T) {
264         assertions := require.New(t)
265
266         client := CreateClientWithoutRetry(tls.Certificate{}, 5*time.Second)
267
268         transport := client.Transport
269         assertions.Equal("*http.Transport", reflect.TypeOf(transport).String())
270         assertions.Equal(5*time.Second, client.Timeout)
271 }
272
273 func TestIsUrlSecured(t *testing.T) {
274         assertions := require.New(t)
275
276         assertions.True(IsUrlSecure("https://url"))
277
278         assertions.False(IsUrlSecure("http://url"))
279 }