X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Frestclient%2FHTTPClient_test.go;fp=capifcore%2Finternal%2Frestclient%2FHTTPClient_test.go;h=47cf8b7c277e18c56b89fd434bacdc011ddbdcd0;hb=6f91b6ac28e733561200c5faf12029cafed39d3f;hp=e390686c117d3be849085121822338490ee66ad1;hpb=e71305f32cddb7933da76dc5ce60193a866c48e7;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/restclient/HTTPClient_test.go b/capifcore/internal/restclient/HTTPClient_test.go index e390686..47cf8b7 100644 --- a/capifcore/internal/restclient/HTTPClient_test.go +++ b/capifcore/internal/restclient/HTTPClient_test.go @@ -47,6 +47,7 @@ func TestPutOk(t *testing.T) { clientMock.On("Do", mock.Anything).Return(&http.Response{ StatusCode: http.StatusOK, + Body: io.NopCloser(bytes.NewReader([]byte("body"))), }, nil) if err := Put("http://localhost:9990", []byte("body"), &clientMock); err != nil { @@ -67,6 +68,36 @@ func TestPutOk(t *testing.T) { clientMock.AssertNumberOfCalls(t, "Do", 1) } +func TestPostOk(t *testing.T) { + assertions := require.New(t) + clientMock := mocks.HTTPClient{} + + clientMock.On("Do", mock.Anything).Return(&http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(bytes.NewReader([]byte("body"))), + }, nil) + + headers := map[string]string{ + "Content-Type": "application/json", + } + if err := Post("http://localhost:9990", []byte("body"), headers, &clientMock); err != nil { + t.Errorf("Post() error = %v, did not want error", err) + } + var actualRequest *http.Request + clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool { + actualRequest = req + return true + })) + assertions.Equal(http.MethodPost, actualRequest.Method) + assertions.Equal("http", actualRequest.URL.Scheme) + assertions.Equal("localhost:9990", actualRequest.URL.Host) + assertions.Equal("application/json", actualRequest.Header.Get("Content-Type")) + body, _ := io.ReadAll(actualRequest.Body) + expectedBody := []byte("body") + assertions.Equal(expectedBody, body) + clientMock.AssertNumberOfCalls(t, "Do", 1) +} + func Test_doErrorCases(t *testing.T) { assertions := require.New(t) type args struct { @@ -109,7 +140,7 @@ func Test_doErrorCases(t *testing.T) { StatusCode: tt.args.mockReturnStatus, Body: io.NopCloser(bytes.NewReader(tt.args.mockReturnBody)), }, tt.args.mockReturnError) - err := do("PUT", tt.args.url, nil, map[string]string{}, &clientMock) + _, err := do("PUT", tt.args.url, nil, map[string]string{}, &clientMock) assertions.Equal(tt.wantErr, err, tt.name) }) }