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
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
31 "github.com/stretchr/testify/mock"
32 "github.com/stretchr/testify/require"
33 "oransc.org/nonrtric/dmaapmediatorproducer/mocks/httpclient"
36 func TestRequestError_Error(t *testing.T) {
37 assertions := require.New(t)
38 actualError := RequestError{
39 StatusCode: http.StatusBadRequest,
40 Body: []byte("error"),
42 assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
44 func TestGet(t *testing.T) {
45 assertions := require.New(t)
59 name: "Test Get with OK response",
62 mockReturnStatus: http.StatusOK,
63 mockReturnBody: "Response",
65 want: []byte("Response"),
68 name: "Test Get with Not OK response",
70 url: "http://testNotOk",
71 mockReturnStatus: http.StatusBadRequest,
72 mockReturnBody: "Bad Response",
75 wantedError: RequestError{
76 StatusCode: http.StatusBadRequest,
77 Body: []byte("Bad Response"),
81 name: "Test Get with error",
83 url: "http://testError",
84 mockReturnError: errors.New("Failed Request"),
87 wantedError: errors.New("Failed Request"),
90 for _, tt := range tests {
91 t.Run(tt.name, func(t *testing.T) {
92 clientMock := httpclient.HTTPClient{}
93 clientMock.On("Get", tt.args.url).Return(&http.Response{
94 StatusCode: tt.args.mockReturnStatus,
95 Body: ioutil.NopCloser(bytes.NewReader([]byte(tt.args.mockReturnBody))),
96 }, tt.args.mockReturnError)
98 got, err := Get(tt.args.url, &clientMock)
99 assertions.Equal(tt.wantedError, err, tt.name)
100 assertions.Equal(tt.want, got, tt.name)
101 clientMock.AssertCalled(t, "Get", tt.args.url)
106 func TestPutOk(t *testing.T) {
107 assertions := require.New(t)
108 clientMock := httpclient.HTTPClient{}
110 clientMock.On("Do", mock.Anything).Return(&http.Response{
111 StatusCode: http.StatusOK,
114 if err := Put("http://localhost:9990", []byte("body"), &clientMock); err != nil {
115 t.Errorf("Put() error = %v, did not want error", err)
117 var actualRequest *http.Request
118 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
122 assertions.Equal(http.MethodPut, actualRequest.Method)
123 assertions.Equal("http", actualRequest.URL.Scheme)
124 assertions.Equal("localhost:9990", actualRequest.URL.Host)
125 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
126 body, _ := ioutil.ReadAll(actualRequest.Body)
127 expectedBody := []byte("body")
128 assertions.Equal(expectedBody, body)
129 clientMock.AssertNumberOfCalls(t, "Do", 1)
132 func TestPostOk(t *testing.T) {
133 assertions := require.New(t)
134 clientMock := httpclient.HTTPClient{}
136 clientMock.On("Do", mock.Anything).Return(&http.Response{
137 StatusCode: http.StatusOK,
140 if err := Post("http://localhost:9990", []byte("body"), &clientMock); err != nil {
141 t.Errorf("Put() error = %v, did not want error", err)
143 var actualRequest *http.Request
144 clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
148 assertions.Equal(http.MethodPost, actualRequest.Method)
149 assertions.Equal("http", actualRequest.URL.Scheme)
150 assertions.Equal("localhost:9990", actualRequest.URL.Host)
151 assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
152 body, _ := ioutil.ReadAll(actualRequest.Body)
153 expectedBody := []byte("body")
154 assertions.Equal(expectedBody, body)
155 clientMock.AssertNumberOfCalls(t, "Do", 1)
158 func Test_doErrorCases(t *testing.T) {
159 assertions := require.New(t)
163 mockReturnBody []byte
164 mockReturnError error
172 name: "Bad request should get RequestError",
175 mockReturnStatus: http.StatusBadRequest,
176 mockReturnBody: []byte("bad request"),
177 mockReturnError: nil,
179 wantErr: RequestError{
180 StatusCode: http.StatusBadRequest,
181 Body: []byte("bad request"),
185 name: "Server unavailable should get error",
187 url: "serverUnavailable",
188 mockReturnError: fmt.Errorf("Server unavailable"),
190 wantErr: fmt.Errorf("Server unavailable"),
193 for _, tt := range tests {
194 t.Run(tt.name, func(t *testing.T) {
195 clientMock := httpclient.HTTPClient{}
196 clientMock.On("Do", mock.Anything).Return(&http.Response{
197 StatusCode: tt.args.mockReturnStatus,
198 Body: ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
199 }, tt.args.mockReturnError)
200 err := do("PUT", tt.args.url, nil, &clientMock)
201 assertions.Equal(tt.wantErr, err, tt.name)