2021-03-16 10:57:48 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
2021-04-28 15:06:17 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2021-03-16 10:57:48 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-12-06 13:29:39 +00:00
|
|
|
pkghttp "github.com/snyk/driftctl/pkg/http"
|
2021-03-16 10:57:48 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-04-29 10:22:34 +00:00
|
|
|
func TestHTTPBackend_Read(t *testing.T) {
|
2021-03-16 10:57:48 +00:00
|
|
|
type args struct {
|
2021-03-16 15:21:28 +00:00
|
|
|
url string
|
|
|
|
options *Options
|
2021-03-16 10:57:48 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2021-04-28 15:06:17 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr error
|
2021-04-30 12:47:22 +00:00
|
|
|
httpClient pkghttp.HTTPClient
|
2021-04-28 15:35:33 +00:00
|
|
|
expected string
|
2021-03-16 10:57:48 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-03-16 15:21:28 +00:00
|
|
|
name: "Should fail with wrong URL",
|
2021-03-16 10:57:48 +00:00
|
|
|
args: args{
|
|
|
|
url: "wrong_url",
|
2021-03-16 15:21:28 +00:00
|
|
|
options: &Options{
|
|
|
|
Headers: map[string]string{},
|
|
|
|
},
|
2021-03-16 10:57:48 +00:00
|
|
|
},
|
2021-04-06 10:58:48 +00:00
|
|
|
wantErr: errors.New("Get \"wrong_url\": unsupported protocol scheme \"\""),
|
2021-04-30 12:47:22 +00:00
|
|
|
httpClient: func() pkghttp.HTTPClient {
|
2021-04-28 15:06:17 +00:00
|
|
|
return &http.Client{}
|
2021-04-29 10:22:34 +00:00
|
|
|
}(),
|
2021-04-28 15:35:33 +00:00
|
|
|
expected: "",
|
2021-03-16 10:57:48 +00:00
|
|
|
},
|
|
|
|
{
|
2021-03-16 15:21:28 +00:00
|
|
|
name: "Should fetch URL with auth header",
|
2021-03-16 10:57:48 +00:00
|
|
|
args: args{
|
2021-04-30 12:47:22 +00:00
|
|
|
url: "https://example.com/cloudskiff/driftctl/main/terraform.tfstate",
|
2021-03-16 15:21:28 +00:00
|
|
|
options: &Options{
|
|
|
|
Headers: map[string]string{
|
|
|
|
"Authorization": "Basic Test",
|
|
|
|
},
|
|
|
|
},
|
2021-03-16 10:57:48 +00:00
|
|
|
},
|
2021-04-06 10:58:48 +00:00
|
|
|
wantErr: nil,
|
2021-04-30 12:47:22 +00:00
|
|
|
httpClient: func() pkghttp.HTTPClient {
|
|
|
|
m := &pkghttp.MockHTTPClient{}
|
2021-04-28 15:06:17 +00:00
|
|
|
|
2021-04-30 12:47:22 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "https://example.com/cloudskiff/driftctl/main/terraform.tfstate", nil)
|
2021-04-28 15:06:17 +00:00
|
|
|
|
|
|
|
req.Header.Add("Authorization", "Basic Test")
|
|
|
|
|
2021-04-28 15:35:33 +00:00
|
|
|
bodyReader := strings.NewReader("{}")
|
2021-04-28 15:06:17 +00:00
|
|
|
bodyReadCloser := io.NopCloser(bodyReader)
|
|
|
|
|
|
|
|
m.On("Do", req).Return(&http.Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: bodyReadCloser,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
return m
|
2021-04-29 10:22:34 +00:00
|
|
|
}(),
|
2021-04-28 15:35:33 +00:00
|
|
|
expected: "{}",
|
2021-04-06 10:58:48 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Should fail with bad status code",
|
|
|
|
args: args{
|
2021-04-30 12:47:22 +00:00
|
|
|
url: "https://example.com/cloudskiff/driftctl/main/terraform.tfstate",
|
2021-04-06 10:58:48 +00:00
|
|
|
options: &Options{
|
|
|
|
Headers: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
2021-04-07 13:12:30 +00:00
|
|
|
wantErr: errors.New("error requesting HTTP(s) backend state: status code: 404"),
|
2021-04-30 12:47:22 +00:00
|
|
|
httpClient: func() pkghttp.HTTPClient {
|
|
|
|
m := &pkghttp.MockHTTPClient{}
|
2021-04-28 15:06:17 +00:00
|
|
|
|
2021-04-30 12:47:22 +00:00
|
|
|
req, _ := http.NewRequest(http.MethodGet, "https://example.com/cloudskiff/driftctl/main/terraform.tfstate", nil)
|
2021-04-28 15:06:17 +00:00
|
|
|
|
|
|
|
bodyReader := strings.NewReader("test")
|
|
|
|
bodyReadCloser := io.NopCloser(bodyReader)
|
|
|
|
|
|
|
|
m.On("Do", req).Return(&http.Response{
|
|
|
|
StatusCode: 404,
|
|
|
|
Body: bodyReadCloser,
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
return m
|
2021-04-29 10:22:34 +00:00
|
|
|
}(),
|
2021-04-28 15:35:33 +00:00
|
|
|
expected: "test",
|
2021-03-16 10:57:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-04-29 10:22:34 +00:00
|
|
|
reader, err := NewHTTPReader(tt.httpClient, tt.args.url, tt.args.options)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
got := make([]byte, len(tt.expected))
|
|
|
|
_, err = reader.Read(got)
|
2021-04-06 10:58:48 +00:00
|
|
|
if tt.wantErr != nil {
|
|
|
|
assert.EqualError(t, err, tt.wantErr.Error())
|
2021-03-16 10:57:48 +00:00
|
|
|
return
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
assert.NotNil(t, got)
|
2021-04-29 10:22:34 +00:00
|
|
|
assert.Equal(t, tt.expected, string(got))
|
2021-03-16 10:57:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPBackend_Close(t *testing.T) {
|
|
|
|
type fields struct {
|
2021-04-29 10:22:34 +00:00
|
|
|
req *http.Request
|
2021-04-30 12:47:22 +00:00
|
|
|
reader io.ReadCloser
|
2021-03-16 10:57:48 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "should fail to close reader",
|
|
|
|
fields: fields{
|
2021-04-29 10:22:34 +00:00
|
|
|
req: &http.Request{},
|
2021-03-16 10:57:48 +00:00
|
|
|
reader: func() io.ReadCloser {
|
|
|
|
return nil
|
2021-04-30 12:47:22 +00:00
|
|
|
}(),
|
2021-03-16 10:57:48 +00:00
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "should close reader",
|
|
|
|
fields: fields{
|
2021-04-29 10:22:34 +00:00
|
|
|
req: &http.Request{},
|
2021-03-16 10:57:48 +00:00
|
|
|
reader: func() io.ReadCloser {
|
|
|
|
m := &MockReaderMock{}
|
|
|
|
m.On("Close").Return(nil)
|
|
|
|
return m
|
2021-04-30 12:47:22 +00:00
|
|
|
}(),
|
2021-03-16 10:57:48 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
h := &HTTPBackend{
|
2021-04-29 10:22:34 +00:00
|
|
|
request: tt.fields.req,
|
2021-04-30 12:47:22 +00:00
|
|
|
reader: tt.fields.reader,
|
2021-03-16 10:57:48 +00:00
|
|
|
}
|
|
|
|
if err := h.Close(); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("Close() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|