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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
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"
41 func TestRequestError_Error(t *testing.T) {
42 assertions := require.New(t)
43 actualError := RequestError{
44 StatusCode: http.StatusBadRequest,
45 Body: []byte("error"),
47 assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
49 func TestGet(t *testing.T) {
50 assertions := require.New(t)
64 name: "Test Get with OK response",
67 mockReturnStatus: http.StatusOK,
68 mockReturnBody: "Response",
70 want: []byte("Response"),
73 name: "Test Get with Not OK response",
75 url: "http://testNotOk",
76 mockReturnStatus: http.StatusBadRequest,
77 mockReturnBody: "Bad Response",
80 wantedError: RequestError{
81 StatusCode: http.StatusBadRequest,
82 Body: []byte("Bad Response"),
86 name: "Test Get with error",
88 url: "http://testError",
89 mockReturnError: errors.New("Failed Request"),
92 wantedError: errors.New("Failed Request"),
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)
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)
111 func TestPutOk(t *testing.T) {
112 assertions := require.New(t)
113 clientMock := httpclient.HTTPClient{}
115 clientMock.On("Do", mock.Anything).Return(&http.Response{
116 StatusCode: http.StatusOK,
119 if err := Put("http://localhost:9990", []byte("body"), &clientMock); err != nil {
120 t.Errorf("Put() error = %v, did not want error", err)
122 var actualRequest *http.Request
123 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
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)
137 func TestPostOk(t *testing.T) {
138 assertions := require.New(t)
139 clientMock := httpclient.HTTPClient{}
141 clientMock.On("Do", mock.Anything).Return(&http.Response{
142 StatusCode: http.StatusOK,
145 if err := Post("http://localhost:9990", []byte("body"), &clientMock); err != nil {
146 t.Errorf("Put() error = %v, did not want error", err)
148 var actualRequest *http.Request
149 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
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)
163 func Test_doErrorCases(t *testing.T) {
164 assertions := require.New(t)
168 mockReturnBody []byte
169 mockReturnError error
177 name: "Bad request should get RequestError",
180 mockReturnStatus: http.StatusBadRequest,
181 mockReturnBody: []byte("bad request"),
182 mockReturnError: nil,
184 wantErr: RequestError{
185 StatusCode: http.StatusBadRequest,
186 Body: []byte("bad request"),
190 name: "Server unavailable should get error",
192 url: "serverUnavailable",
193 mockReturnError: fmt.Errorf("Server unavailable"),
195 wantErr: fmt.Errorf("Server unavailable"),
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)
211 func Test_createClientCertificate(t *testing.T) {
212 assertions := require.New(t)
213 wantedCert, _ := tls.LoadX509KeyPair("../../security/producer.crt", "../../security/producer.key")
221 wantCert tls.Certificate
225 name: "Paths to cert info ok should return cerftificate",
227 certPath: "../../security/producer.crt",
228 keyPath: "../../security/producer.key",
230 wantCert: wantedCert,
233 name: "Paths to cert info not ok should return error with info about error",
235 certPath: "wrong_cert",
236 keyPath: "wrong_key",
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"),
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)
250 func Test_CreateRetryClient(t *testing.T) {
251 assertions := require.New(t)
253 client := CreateRetryClient(tls.Certificate{})
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)
263 func Test_CreateClientWithoutRetry(t *testing.T) {
264 assertions := require.New(t)
266 client := CreateClientWithoutRetry(tls.Certificate{}, 5*time.Second)
268 transport := client.Transport
269 assertions.Equal("*http.Transport", reflect.TypeOf(transport).String())
270 assertions.Equal(5*time.Second, client.Timeout)
273 func TestIsUrlSecured(t *testing.T) {
274 assertions := require.New(t)
276 assertions.True(IsUrlSecure("https://url"))
278 assertions.False(IsUrlSecure("http://url"))