Skip to content

Commit 172748e

Browse files
committed
Write test cases
1 parent c80ee4e commit 172748e

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

cns/service/main.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,17 @@ func sendRegisterNodeRequest(httpClient acn.HTTPClient, httpRestService cns.HTTP
403403
return errors.Wrap(retry.Unrecoverable(err), "failed to sendRegisterNodeRequest")
404404
}
405405

406-
response, err := httpClient.Post(registerURL, "application/json", body.Bytes())
406+
request, err := http.NewRequest(http.MethodPost, registerURL, &body)
407407
if err != nil {
408-
logger.Errorf("[Azure CNS] Failed to register node with retryable err: %+v", err)
409-
return errors.Wrap(err, "failed to sendRegisterNodeRequest")
408+
return errors.Wrap(err, "failed to build request")
410409
}
410+
411+
request.Header.Set("Content-Type", "application/json")
412+
response, err := httpClient.Do(request)
413+
if err != nil {
414+
return errors.Wrap(err, "http request failed")
415+
}
416+
411417
defer response.Body.Close()
412418

413419
if response.StatusCode != http.StatusOK {

cns/service/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type MockHTTPClient struct {
2121
}
2222

2323
// Post is the implementation of the Post method for MockHTTPClient
24-
func (m *MockHTTPClient) Post(_, _ string, _ []byte) (*http.Response, error) {
24+
func (m *MockHTTPClient) Do(_ *http.Request) (*http.Response, error) {
2525
return m.Response, m.Err
2626
}
2727

common/utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package common
55

66
import (
7-
"bytes"
87
"context"
98
"encoding/binary"
109
"encoding/json"
@@ -81,15 +80,16 @@ type metadataWrapper struct {
8180

8281
// HTTPClient interface to abstract http.Client methods
8382
type HTTPClient interface {
84-
Post(url, contentType string, body []byte) (*http.Response, error)
83+
Do(req *http.Request) (*http.Response, error)
8584
}
8685

8786
// StandardHTTPClient is a standard implementation of the HTTPClient interface
8887
type StandardHTTPClient struct{}
8988

90-
// Post is the implementation of the Post method for StandardHTTPClient
91-
func (c *StandardHTTPClient) Post(url, contentType string, body []byte) (*http.Response, error) {
92-
return http.Post(url, contentType, bytes.NewBuffer(body))
89+
// Do is the implementation of the Post method for StandardHTTPClient
90+
func (c *StandardHTTPClient) Do(req *http.Request) (*http.Response, error) {
91+
client := http.Client{}
92+
return client.Do(req)
9393
}
9494

9595
// Creating http client object to be reused instead of creating one every time.

0 commit comments

Comments
 (0)