diff options
Diffstat (limited to 'vendor/github.com/utahta/go-openuri')
-rw-r--r-- | vendor/github.com/utahta/go-openuri/openuri.go | 54 | ||||
-rw-r--r-- | vendor/github.com/utahta/go-openuri/openuri_test.go | 74 |
2 files changed, 128 insertions, 0 deletions
diff --git a/vendor/github.com/utahta/go-openuri/openuri.go b/vendor/github.com/utahta/go-openuri/openuri.go new file mode 100644 index 0000000..a1ce966 --- /dev/null +++ b/vendor/github.com/utahta/go-openuri/openuri.go @@ -0,0 +1,54 @@ +package openuri + +import ( + "io" + "net/http" + "os" + "strings" +) + +// Client type +type Client struct { + httpClient *http.Client +} + +// ClientOption type +type ClientOption func(*Client) error + +// New returns a Client struct +func New(options ...ClientOption) (*Client, error) { + c := &Client{httpClient: http.DefaultClient} + for _, option := range options { + if err := option(c); err != nil { + return nil, err + } + } + return c, nil +} + +// Open an io.ReadCloser from a local file or URL +func Open(name string, options ...ClientOption) (io.ReadCloser, error) { + c, err := New(options...) + if err != nil { + return nil, err + } + return c.Open(name) +} + +func WithHTTPClient(v *http.Client) ClientOption { + return func(c *Client) error { + c.httpClient = v + return nil + } +} + +func (c *Client) Open(name string) (io.ReadCloser, error) { + if strings.HasPrefix(name, "http://") || strings.HasPrefix(name, "https://") { + resp, err := c.httpClient.Get(name) + if err != nil { + return nil, err + } + return resp.Body, nil + } + return os.Open(name) +} diff --git a/vendor/github.com/utahta/go-openuri/openuri_test.go b/vendor/github.com/utahta/go-openuri/openuri_test.go new file mode 100644 index 0000000..7f653e6 --- /dev/null +++ b/vendor/github.com/utahta/go-openuri/openuri_test.go @@ -0,0 +1,74 @@ +package openuri + +import ( + "io/ioutil" + "net/http" + "strings" + "testing" +) + +type dummyRoundTripper struct{} + +func (d *dummyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + return &http.Response{Status: "dummy", Body: ioutil.NopCloser(strings.NewReader("body dummy"))}, nil +} + +func TestNew(t *testing.T) { + _, err := New() + if err != nil { + t.Error(err) + } +} + +func TestWithHTTPClient(t *testing.T) { + c, err := New(WithHTTPClient(&http.Client{Transport: &dummyRoundTripper{}})) + if err != nil { + t.Error(err) + } + + resp, _ := c.httpClient.Get("test") + if resp.Status != "dummy" { + t.Errorf("Expected status dummy, got %s", resp.Status) + } +} + +func TestOpen_File(t *testing.T) { + o, err := Open("./openuri.go") + if err != nil { + t.Error(err) + } + + b, err := ioutil.ReadAll(o) + if err != nil { + t.Error(err) + } + + if !strings.HasPrefix(string(b), "package openuri") { + t.Errorf("Expected open file, go %s", string(b)) + } +} + +func TestOpen_URL(t *testing.T) { + tests := []struct { + url string + }{ + {"http://example.com"}, + {"https://example.com"}, + } + + for _, test := range tests { + o, err := Open(test.url, WithHTTPClient(&http.Client{Transport: &dummyRoundTripper{}})) + if err != nil { + t.Error(err) + } + + b, err := ioutil.ReadAll(o) + if err != nil { + t.Error(err) + } + + if !strings.HasPrefix(string(b), "body dummy") { + t.Errorf("Expected open file, go %s", string(b)) + } + } +} |