2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: 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===================================
30 "github.com/stretchr/testify/assert"
31 "github.com/stretchr/testify/require"
34 func TestNewRequest(t *testing.T) {
35 assertions := require.New(t)
37 bodyBytes, _ := json.Marshal("body")
38 succesfullReq, _ := http.NewRequest(http.MethodGet, "url", bytes.NewReader(bodyBytes))
53 name: "succesfull newRequest",
55 method: http.MethodGet,
58 userInfo: [2]string{"user", "pass"},
64 name: "request failed json marshal",
66 method: http.MethodGet,
68 payload: map[string]interface{}{
69 "foo": make(chan int),
73 wantErr: fmt.Errorf("failed to marshal request body: json: unsupported type: chan int"),
76 name: "request failed calling newRequest",
83 wantErr: fmt.Errorf("failed to create HTTP request: net/http: invalid method \"*?\""),
87 for _, tt := range tests {
88 t.Run(tt.name, func(t *testing.T) {
89 client := New(&http.Client{}, false)
92 if tt.args.userInfo[0] != "" {
93 req, err = client.newRequest(tt.args.method, tt.args.path, tt.args.payload, tt.args.userInfo[0], tt.args.userInfo[1])
95 req, err = client.newRequest(tt.args.method, tt.args.path, tt.args.payload)
98 if tt.wantErr != nil {
99 assertions.Equal(tt.want, req)
100 assertions.EqualError(tt.wantErr, err.Error())
102 assertions.Equal("url", req.URL.Path)
103 assertions.Equal("application/json", req.Header.Get("Content-Type"))
104 if tt.args.userInfo[0] != "" {
105 assertions.Contains(req.Header.Get("Authorization"), "Basic dXNlcjpwYXNz")
107 assertions.Empty(req.Header.Get("Authorization"))
117 func TestGet(t *testing.T) {
118 assertions := require.New(t)
131 name: "successful GET request",
133 header: "application/json",
134 respCode: http.StatusOK,
136 userInfo: [2]string{"user", "pass"},
141 name: "error GET request",
143 header: "application/json",
144 respCode: http.StatusBadRequest,
147 wantErr: "error response with status: 400 and body:",
151 for _, tt := range tests {
153 t.Run(tt.name, func(t *testing.T) {
154 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
155 assertions.Equal(http.MethodGet, r.Method)
156 response, _ := json.Marshal(tt.args.resp)
157 w.Header().Set("Content-Type", tt.args.header)
158 w.WriteHeader(tt.args.respCode)
163 client := New(&http.Client{}, false)
166 if tt.args.userInfo[0] != "" {
167 err = client.Get(srv.URL, &res, tt.args.userInfo[0], tt.args.userInfo[1])
169 err = client.Get(srv.URL, &res)
173 assertions.Contains(err.Error(), tt.wantErr)
175 assertions.Equal(tt.args.resp, res)
180 func TestPost(t *testing.T) {
181 assertions := require.New(t)
196 name: "successful POST request",
198 header: "application/json",
199 payload: `json:"example"`,
200 respCode: http.StatusOK,
202 userInfo: [2]string{"user", "pass"},
207 name: "error POST request",
209 header: "application/json",
210 payload: `json:"example"`,
211 respCode: http.StatusBadRequest,
214 wantErr: "error response with status: 400 and body:",
217 name: "error to create POST request",
219 header: "application/json",
220 payload: map[string]interface{}{
221 "foo": make(chan int),
223 respCode: http.StatusBadRequest,
226 wantErr: "failed to marshal request body: json: unsupported type: chan int",
230 for _, tt := range tests {
232 t.Run(tt.name, func(t *testing.T) {
233 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
235 assert.Equal(t, http.MethodPost, r.Method)
236 assert.Contains(t, r.Header.Get("Content-Type"), "application/json")
239 decoder := json.NewDecoder(r.Body)
240 decoder.Decode(&reqBody)
241 assert.Equal(t, reqBody, `json:"example"`)
243 response, _ := json.Marshal(tt.args.resp)
244 w.Header().Set("Content-Type", tt.args.header)
245 w.WriteHeader(tt.args.respCode)
250 client := New(&http.Client{}, false)
252 if tt.args.userInfo[0] != "" {
253 err = client.Post(srv.URL, tt.args.payload, nil, tt.args.userInfo[0], tt.args.userInfo[1])
255 err = client.Post(srv.URL, tt.args.payload, nil)
259 assertions.Contains(err.Error(), tt.wantErr)
261 assertions.Equal(tt.args.resp, "Success!")
268 func TestPut(t *testing.T) {
269 assertions := require.New(t)
284 name: "successful PUT request",
286 header: "application/json",
287 payload: `json:"example"`,
288 respCode: http.StatusOK,
290 userInfo: [2]string{"user", "pass"},
295 name: "error PUT request",
297 header: "application/json",
298 payload: `json:"example"`,
299 respCode: http.StatusBadRequest,
302 wantErr: "error response with status: 400 and body:",
305 name: "error to create PUT request",
307 header: "application/json",
308 payload: map[string]interface{}{
309 "foo": make(chan int),
311 respCode: http.StatusBadRequest,
314 wantErr: "failed to marshal request body: json: unsupported type: chan int",
318 for _, tt := range tests {
320 t.Run(tt.name, func(t *testing.T) {
321 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
323 assert.Equal(t, http.MethodPut, r.Method)
324 assert.Contains(t, r.Header.Get("Content-Type"), "application/json")
327 decoder := json.NewDecoder(r.Body)
328 decoder.Decode(&reqBody)
329 assert.Equal(t, reqBody, `json:"example"`)
331 response, _ := json.Marshal(tt.args.resp)
332 w.Header().Set("Content-Type", tt.args.header)
333 w.WriteHeader(tt.args.respCode)
338 client := New(&http.Client{}, false)
340 if tt.args.userInfo[0] != "" {
341 err = client.Put(srv.URL, tt.args.payload, nil, tt.args.userInfo[0], tt.args.userInfo[1])
343 err = client.Put(srv.URL, tt.args.payload, nil)
347 assertions.Contains(err.Error(), tt.wantErr)
349 assertions.Equal(tt.args.resp, "Success!")
356 func TestDelete(t *testing.T) {
357 header := "application/json"
358 respCode := http.StatusOK
361 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
363 assert.Equal(t, http.MethodDelete, r.Method)
364 assert.Contains(t, r.Header.Get("Content-Type"), "application/json")
367 decoder := json.NewDecoder(r.Body)
368 decoder.Decode(&reqBody)
369 assert.Equal(t, reqBody, `json:"example"`)
371 response, _ := json.Marshal(resp)
372 w.Header().Set("Content-Type", header)
373 w.WriteHeader(respCode)
378 client := New(&http.Client{}, false)
379 payload := `json:"example"`
381 err := client.Delete(srv.URL, payload, nil, "admin", "pass")
384 assert.Equal(t, "", err.Error())
388 err = client.Delete(srv.URL, payload, nil)
391 assert.Equal(t, "", err.Error())