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/require"
33 func TestNewRequest(t *testing.T) {
34 assertions := require.New(t)
36 bodyBytes, _ := json.Marshal("body")
37 succesfullReq, _ := http.NewRequest(http.MethodGet, "url", bytes.NewReader(bodyBytes))
51 name: "succesfull newRequest",
53 method: http.MethodGet,
61 name: "request failed json marshal",
63 method: http.MethodGet,
65 payload: map[string]interface{}{
66 "foo": make(chan int),
70 wantErr: fmt.Errorf("failed to marshal request body: json: unsupported type: chan int"),
73 name: "request failed calling newRequest",
80 wantErr: fmt.Errorf("failed to create HTTP request: net/http: invalid method \"*?\""),
84 for _, tt := range tests {
85 t.Run(tt.name, func(t *testing.T) {
86 client := New(&http.Client{})
88 req, err := client.newRequest(tt.args.method, tt.args.path, tt.args.payload)
89 if tt.wantErr != nil {
90 assertions.Equal(tt.want, req)
91 assertions.EqualError(tt.wantErr, err.Error())
93 assertions.Equal("url", req.URL.Path)
94 assertions.Equal("application/json; charset=utf-8", req.Header.Get("Content-Type"))
95 assertions.Empty(req.Header.Get("Authorization"))
103 func TestGet(t *testing.T) {
104 assertions := require.New(t)
116 name: "successful GET request",
118 header: "application/json",
119 respCode: http.StatusOK,
125 name: "error GET request",
127 header: "application/json",
128 respCode: http.StatusBadRequest,
131 wantErr: "failed to do request, 400 status code received",
135 for _, tt := range tests {
137 t.Run(tt.name, func(t *testing.T) {
138 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
139 response, _ := json.Marshal(tt.args.resp)
140 w.Header().Set("Content-Type", tt.args.header)
141 w.WriteHeader(tt.args.respCode)
146 client := New(&http.Client{})
148 err := client.Get(srv.URL, &res)
151 assertions.Equal(tt.wantErr, err.Error())
153 assertions.Equal(tt.args.resp, res)
158 func TestPost(t *testing.T) {
159 assertions := require.New(t)
171 name: "successful Post request",
173 header: "application/json",
174 respCode: http.StatusOK,
181 for _, tt := range tests {
183 t.Run(tt.name, func(t *testing.T) {
184 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
186 assertions.Equal(http.MethodPost, r.Method)
187 assertions.Contains(r.Header.Get("Content-Type"), "application/json")
189 var reqBody interface{}
190 decoder := json.NewDecoder(r.Body)
191 decoder.Decode(&reqBody)
192 assertions.Equal(reqBody, `json:"example"`)
194 response, _ := json.Marshal(tt.args.resp)
195 w.Header().Set("Content-Type", tt.args.header)
196 w.WriteHeader(tt.args.respCode)
201 client := New(&http.Client{})
202 payload := `json:"example"`
203 err := client.Post(srv.URL, payload, nil)
206 assertions.Equal(tt.wantErr, err.Error())