summaryrefslogtreecommitdiff
path: root/vendor/github.com/utahta/go-openuri/openuri_test.go
blob: 7f653e6e5ba694f72b8835f2df178289ffc2e708 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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))
		}
	}
}