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===================================
29 "github.com/stretchr/testify/mock"
30 "github.com/stretchr/testify/require"
31 "oransc.org/usecase/oruclosedloop/mocks"
34 func TestRequestError_Error(t *testing.T) {
35 assertions := require.New(t)
37 actualError := RequestError{
38 StatusCode: http.StatusBadRequest,
39 Body: []byte("error"),
41 assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
44 func TestPutWithoutAuth(t *testing.T) {
45 assertions := require.New(t)
47 clientMock := mocks.HTTPClient{}
48 clientMock.On("Do", mock.Anything).Return(&http.Response{
49 StatusCode: http.StatusOK,
52 error := PutWithoutAuth("url", []byte("body"), &clientMock)
55 var actualRequest *http.Request
56 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
60 assertions.Equal(http.MethodPut, actualRequest.Method)
61 assertions.Equal("url", actualRequest.URL.Path)
62 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
63 assertions.Empty(actualRequest.Header.Get("Authorization"))
64 body, _ := ioutil.ReadAll(actualRequest.Body)
65 expectedBody := []byte("body")
66 assertions.Equal(expectedBody, body)
67 clientMock.AssertNumberOfCalls(t, "Do", 1)
70 func TestPut(t *testing.T) {
71 assertions := require.New(t)
73 clientMock := mocks.HTTPClient{}
74 clientMock.On("Do", mock.Anything).Return(&http.Response{
75 StatusCode: http.StatusOK,
78 error := Put("url", "body", &clientMock, "admin", "pwd")
81 var actualRequest *http.Request
82 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
86 assertions.Equal(http.MethodPut, actualRequest.Method)
87 assertions.Equal("url", actualRequest.URL.Path)
88 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
89 tempRequest, _ := http.NewRequest("", "", nil)
90 tempRequest.SetBasicAuth("admin", "pwd")
91 assertions.Equal(tempRequest.Header.Get("Authorization"), actualRequest.Header.Get("Authorization"))
92 body, _ := ioutil.ReadAll(actualRequest.Body)
93 expectedBody := []byte("body")
94 assertions.Equal(expectedBody, body)
95 clientMock.AssertNumberOfCalls(t, "Do", 1)
98 func TestDelete(t *testing.T) {
99 assertions := require.New(t)
101 clientMock := mocks.HTTPClient{}
102 clientMock.On("Do", mock.Anything).Return(&http.Response{
103 StatusCode: http.StatusOK,
106 error := Delete("url", &clientMock)
108 assertions.Nil(error)
109 var actualRequest *http.Request
110 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
114 assertions.Equal(http.MethodDelete, actualRequest.Method)
115 assertions.Equal("url", actualRequest.URL.Path)
116 assertions.Empty(actualRequest.Header.Get("Content-Type"))
117 assertions.Empty(actualRequest.Header.Get("Authorization"))
118 assertions.Equal(http.NoBody, actualRequest.Body)
119 clientMock.AssertNumberOfCalls(t, "Do", 1)
122 func Test_doErrorCases(t *testing.T) {
123 assertions := require.New(t)
128 mockReturnBody []byte
129 mockReturnError error
137 name: "Bad request should get RequestError",
140 mockReturnStatus: http.StatusBadRequest,
141 mockReturnBody: []byte("bad request"),
143 wantErr: RequestError{
144 StatusCode: http.StatusBadRequest,
145 Body: []byte("bad request"),
149 name: "Server unavailable should get error",
151 url: "serverUnavailable",
152 mockReturnError: fmt.Errorf("Server unavailable"),
154 wantErr: fmt.Errorf("Server unavailable"),
157 for _, tt := range tests {
158 t.Run(tt.name, func(t *testing.T) {
159 clientMock := mocks.HTTPClient{}
160 clientMock.On("Do", mock.Anything).Return(&http.Response{
161 StatusCode: tt.args.mockReturnStatus,
162 Body: ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
163 }, tt.args.mockReturnError)
165 err := do("PUT", tt.args.url, nil, &clientMock)
166 assertions.Equal(tt.wantErr, err, tt.name)