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 TestGet(t *testing.T) {
45 assertions := require.New(t)
63 mockReturnStatus: http.StatusOK,
64 mockReturnBody: []byte("body"),
71 name: "Bad request should get RequestError",
74 mockReturnStatus: http.StatusBadRequest,
75 mockReturnBody: []byte("bad request"),
79 wantErr: RequestError{
80 StatusCode: http.StatusBadRequest,
81 Body: []byte("bad request"),
85 name: "Server unavailable should get error",
87 url: "serverUnavailable",
88 mockReturnError: fmt.Errorf("Server unavailable"),
91 wantErr: fmt.Errorf("Server unavailable"),
94 for _, tt := range tests {
95 t.Run(tt.name, func(t *testing.T) {
96 clientMock := mocks.HTTPClient{}
97 clientMock.On("Get", tt.args.url).Return(&http.Response{
98 StatusCode: tt.args.mockReturnStatus,
99 Body: ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
100 }, tt.args.mockReturnError)
103 got, err := Get(tt.args.url)
104 assertions.Equal(tt.wantErr, err, tt.name)
105 assertions.Equal(tt.want, got, tt.name)
106 clientMock.AssertCalled(t, "Get", tt.args.url)
111 func TestPutWithoutAuth(t *testing.T) {
112 assertions := require.New(t)
114 clientMock := mocks.HTTPClient{}
115 clientMock.On("Do", mock.Anything).Return(&http.Response{
116 StatusCode: http.StatusOK,
120 error := PutWithoutAuth("url", []byte("body"))
122 assertions.Nil(error)
123 var actualRequest *http.Request
124 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
128 assertions.Equal(http.MethodPut, actualRequest.Method)
129 assertions.Equal("url", actualRequest.URL.Path)
130 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
131 assertions.Empty(actualRequest.Header.Get("Authorization"))
132 body, _ := ioutil.ReadAll(actualRequest.Body)
133 expectedBody := []byte("body")
134 assertions.Equal(expectedBody, body)
135 clientMock.AssertNumberOfCalls(t, "Do", 1)
138 func TestPut(t *testing.T) {
139 assertions := require.New(t)
141 clientMock := mocks.HTTPClient{}
142 clientMock.On("Do", mock.Anything).Return(&http.Response{
143 StatusCode: http.StatusOK,
147 error := Put("url", "body", "admin", "pwd")
149 assertions.Nil(error)
150 var actualRequest *http.Request
151 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
155 assertions.Equal(http.MethodPut, actualRequest.Method)
156 assertions.Equal("url", actualRequest.URL.Path)
157 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
158 tempRequest, _ := http.NewRequest("", "", nil)
159 tempRequest.SetBasicAuth("admin", "pwd")
160 assertions.Equal(tempRequest.Header.Get("Authorization"), actualRequest.Header.Get("Authorization"))
161 body, _ := ioutil.ReadAll(actualRequest.Body)
162 expectedBody := []byte("body")
163 assertions.Equal(expectedBody, body)
164 clientMock.AssertNumberOfCalls(t, "Do", 1)
167 func TestDelete(t *testing.T) {
168 assertions := require.New(t)
170 clientMock := mocks.HTTPClient{}
171 clientMock.On("Do", mock.Anything).Return(&http.Response{
172 StatusCode: http.StatusOK,
176 error := Delete("url")
178 assertions.Nil(error)
179 var actualRequest *http.Request
180 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
184 assertions.Equal(http.MethodDelete, actualRequest.Method)
185 assertions.Equal("url", actualRequest.URL.Path)
186 assertions.Empty(actualRequest.Header.Get("Content-Type"))
187 assertions.Empty(actualRequest.Header.Get("Authorization"))
188 assertions.Equal(http.NoBody, actualRequest.Body)
189 clientMock.AssertNumberOfCalls(t, "Do", 1)
192 func Test_doErrorCases(t *testing.T) {
193 assertions := require.New(t)
198 mockReturnBody []byte
199 mockReturnError error
207 name: "Bad request should get RequestError",
210 mockReturnStatus: http.StatusBadRequest,
211 mockReturnBody: []byte("bad request"),
212 mockReturnError: nil,
214 wantErr: RequestError{
215 StatusCode: http.StatusBadRequest,
216 Body: []byte("bad request"),
220 name: "Server unavailable should get error",
222 url: "serverUnavailable",
223 mockReturnError: fmt.Errorf("Server unavailable"),
225 wantErr: fmt.Errorf("Server unavailable"),
228 for _, tt := range tests {
229 t.Run(tt.name, func(t *testing.T) {
230 clientMock := mocks.HTTPClient{}
231 clientMock.On("Do", mock.Anything).Return(&http.Response{
232 StatusCode: tt.args.mockReturnStatus,
233 Body: ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
234 }, tt.args.mockReturnError)
237 err := do("PUT", tt.args.url, nil)
238 assertions.Equal(tt.wantErr, err, tt.name)