Skip to content

Commit

Permalink
test: mock http client
Browse files Browse the repository at this point in the history
  • Loading branch information
sundowndev committed Apr 6, 2021
1 parent 22aa2ff commit 509c76e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
19 changes: 19 additions & 0 deletions mocks/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mocks

import (
"net/http"

mock "github.com/stretchr/testify/mock"
)

// HttpClient is an autogenerated mock type for the Client type
type HttpClient struct {
mock.Mock
}

// Do provides a mock function
func (_m *HttpClient) Do(req *http.Request) (*http.Response, error) {
ret := _m.Called(req)

return ret.Get(0).(*http.Response), ret.Error(1)
}
3 changes: 2 additions & 1 deletion pkg/iac/terraform/state/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package backend
import (
"fmt"
"io"
"net/http"

"github.com/cloudskiff/driftctl/pkg/iac/config"
"github.com/pkg/errors"
Expand Down Expand Up @@ -46,7 +47,7 @@ func GetBackend(config config.SupplierConfig, opts *Options) (Backend, error) {
case BackendKeyHTTP:
fallthrough
case BackendKeyHTTPS:
return NewHTTPReader(fmt.Sprintf("%s://%s", config.Backend, config.Path), opts)
return NewHTTPReader(&http.Client{}, fmt.Sprintf("%s://%s", config.Backend, config.Path), opts)
default:
return nil, errors.Errorf("Unsupported backend '%s'", backend)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/iac/terraform/state/backend/http_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ type HTTPBackend struct {
reader io.ReadCloser
}

func NewHTTPReader(rawURL string, opts *Options) (*HTTPBackend, error) {
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}

func NewHTTPReader(client HttpClient, rawURL string, opts *Options) (*HTTPBackend, error) {
req, err := http.NewRequest(http.MethodGet, rawURL, nil)
if err != nil {
return nil, err
Expand All @@ -26,7 +30,6 @@ func NewHTTPReader(rawURL string, opts *Options) (*HTTPBackend, error) {
req.Header.Add(key, value)
}

client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
Expand Down
60 changes: 48 additions & 12 deletions pkg/iac/terraform/state/backend/http_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package backend
import (
"errors"
"io"
"net/http"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/cloudskiff/driftctl/mocks"
)

func TestNewHTTPReader(t *testing.T) {
Expand All @@ -15,10 +19,10 @@ func TestNewHTTPReader(t *testing.T) {
options *Options
}
tests := []struct {
name string
args args
wantURL string
wantErr error
name string
args args
wantErr error
httpClient func() HttpClient
}{
{
name: "Should fail with wrong URL",
Expand All @@ -28,45 +32,77 @@ func TestNewHTTPReader(t *testing.T) {
Headers: map[string]string{},
},
},
wantURL: "",
wantErr: errors.New("Get \"wrong_url\": unsupported protocol scheme \"\""),
httpClient: func() HttpClient {
return &http.Client{}
},
},
{
name: "Should fetch URL with auth header",
args: args{
url: "https://raw.githubusercontent.com/cloudskiff/driftctl/main/.dockerignore",
url: "https://wrong.url/cloudskiff/driftctl/main/terraform.tfstate",
options: &Options{
Headers: map[string]string{
"Authorization": "Basic Test",
},
},
},
wantURL: "https://raw.githubusercontent.com/cloudskiff/driftctl/main/.dockerignore",
wantErr: nil,
httpClient: func() HttpClient {
m := &mocks.HttpClient{}

req, _ := http.NewRequest(http.MethodGet, "https://wrong.url/cloudskiff/driftctl/main/terraform.tfstate", nil)

req.Header.Add("Authorization", "Basic Test")

bodyReader := strings.NewReader("test")
bodyReadCloser := io.NopCloser(bodyReader)

m.On("Do", req).Return(&http.Response{
StatusCode: 200,
Body: bodyReadCloser,
}, nil)

return m
},
},
{
name: "Should fail with bad status code",
args: args{
url: "https://raw.githubusercontent.com/cloudskiff/driftctl-test/main/.dockerignore",
url: "https://wrong.url/cloudskiff/driftctl/main/terraform.tfstate",
options: &Options{
Headers: map[string]string{},
},
},
wantURL: "https://raw.githubusercontent.com/cloudskiff/driftctl-badprojecturl",
wantErr: errors.New("error in backend HTTP(s): non-200 OK status code: 404 Not Found body: \"404: Not Found\""),
wantErr: errors.New("error in backend HTTP(s): non-200 OK status code: 404 Not Found body: \"test\""),
httpClient: func() HttpClient {
m := &mocks.HttpClient{}

req, _ := http.NewRequest(http.MethodGet, "https://wrong.url/cloudskiff/driftctl/main/terraform.tfstate", nil)

bodyReader := strings.NewReader("test")
bodyReadCloser := io.NopCloser(bodyReader)

m.On("Do", req).Return(&http.Response{
StatusCode: 404,
Status: "404 Not Found",
Body: bodyReadCloser,
}, nil)

return m
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewHTTPReader(tt.args.url, tt.args.options)
got, err := NewHTTPReader(tt.httpClient(), tt.args.url, tt.args.options)
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
return
} else {
assert.NoError(t, err)
}
assert.NotNil(t, got)
assert.Equal(t, tt.wantURL, got.url)
})
}
}
Expand Down

0 comments on commit 509c76e

Please sign in to comment.