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===================================
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))
52 name: "succesfull newRequest",
54 method: http.MethodGet,
62 name: "request failed json marshal",
64 method: http.MethodGet,
66 payload: map[string]interface{}{
67 "foo": make(chan int),
71 wantErr: fmt.Errorf("failed to marshal request body: json: unsupported type: chan int"),
74 name: "request failed calling newRequest",
81 wantErr: fmt.Errorf("failed to create HTTP request: net/http: invalid method \"*?\""),
85 for _, tt := range tests {
86 t.Run(tt.name, func(t *testing.T) {
87 client := New(&http.Client{}, false)
89 req, err := client.newRequest(tt.args.method, tt.args.path, tt.args.payload)
90 if tt.wantErr != nil {
91 assertions.Equal(tt.want, req)
92 assertions.EqualError(tt.wantErr, err.Error())
94 assertions.Equal("url", req.URL.Path)
95 assertions.Equal("application/json", req.Header.Get("Content-Type"))
96 assertions.Empty(req.Header.Get("Authorization"))
104 func TestGet(t *testing.T) {
105 assertions := require.New(t)
117 name: "successful GET request",
119 header: "application/json",
120 respCode: http.StatusOK,
126 name: "error GET request",
128 header: "application/json",
129 respCode: http.StatusBadRequest,
132 wantErr: "error response with status: 400 and body:",
136 for _, tt := range tests {
138 t.Run(tt.name, func(t *testing.T) {
139 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
140 assertions.Equal(http.MethodGet, r.Method)
141 response, _ := json.Marshal(tt.args.resp)
142 w.Header().Set("Content-Type", tt.args.header)
143 w.WriteHeader(tt.args.respCode)
148 client := New(&http.Client{}, false)
150 err := client.Get(srv.URL, &res)
153 assertions.Contains(err.Error(), tt.wantErr)
155 assertions.Equal(tt.args.resp, res)
160 func TestPost(t *testing.T) {
161 header := "application/json"
162 respCode := http.StatusOK
165 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
167 assert.Equal(t, http.MethodPost, r.Method)
168 assert.Contains(t, r.Header.Get("Content-Type"), "application/json")
171 decoder := json.NewDecoder(r.Body)
172 decoder.Decode(&reqBody)
173 assert.Equal(t, reqBody, `json:"example"`)
175 response, _ := json.Marshal(resp)
176 w.Header().Set("Content-Type", header)
177 w.WriteHeader(respCode)
182 client := New(&http.Client{}, false)
183 payload := `json:"example"`
184 err := client.Post(srv.URL, payload, nil, "admin", "pass")
187 assert.Equal(t, "", err.Error())
191 func TestPut(t *testing.T) {
192 header := "application/json"
193 respCode := http.StatusOK
196 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
198 assert.Equal(t, http.MethodPut, r.Method)
199 assert.Contains(t, r.Header.Get("Content-Type"), "application/json")
202 decoder := json.NewDecoder(r.Body)
203 decoder.Decode(&reqBody)
204 assert.Equal(t, reqBody, `json:"example"`)
206 response, _ := json.Marshal(resp)
207 w.Header().Set("Content-Type", header)
208 w.WriteHeader(respCode)
213 client := New(&http.Client{}, false)
214 payload := `json:"example"`
215 err := client.Put(srv.URL, payload, nil, "admin", "pass")
218 assert.Equal(t, "", err.Error())