-func TestGet(t *testing.T) {
- assertions := require.New(t)
-
- type args struct {
- url string
- mockReturnStatus int
- mockReturnBody []byte
- mockReturnError error
- }
- tests := []struct {
- name string
- args args
- want []byte
- wantErr error
- }{
- {
- name: "Ok response",
- args: args{
- url: "ok",
- mockReturnStatus: http.StatusOK,
- mockReturnBody: []byte("body"),
- mockReturnError: nil,
- },
- want: []byte("body"),
- wantErr: nil,
- },
- {
- name: "Bad request should get RequestError",
- args: args{
- url: "badRequest",
- mockReturnStatus: http.StatusBadRequest,
- mockReturnBody: []byte("bad request"),
- mockReturnError: nil,
- },
- want: nil,
- wantErr: RequestError{
- StatusCode: http.StatusBadRequest,
- Body: []byte("bad request"),
- },
- },
- {
- name: "Server unavailable should get error",
- args: args{
- url: "serverUnavailable",
- mockReturnError: fmt.Errorf("Server unavailable"),
- },
- want: nil,
- wantErr: fmt.Errorf("Server unavailable"),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- clientMock := mocks.HTTPClient{}
- clientMock.On("Get", tt.args.url).Return(&http.Response{
- StatusCode: tt.args.mockReturnStatus,
- Body: ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
- }, tt.args.mockReturnError)
- Client = &clientMock
-
- got, err := Get(tt.args.url)
- assertions.Equal(tt.wantErr, err, tt.name)
- assertions.Equal(tt.want, got, tt.name)
- clientMock.AssertCalled(t, "Get", tt.args.url)
- })
- }
-}
-