github.com/hairyhenderson/templater@v3.5.0+incompatible/data/datasource_http_test.go (about) 1 package data 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "net/http/httptest" 8 "net/url" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func must(r interface{}, err error) interface{} { 15 if err != nil { 16 panic(err) 17 } 18 return r 19 } 20 21 func setupHTTP(code int, mimetype string, body string) (*httptest.Server, *http.Client) { 22 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 23 24 w.Header().Set("Content-Type", mimetype) 25 w.WriteHeader(code) 26 if body == "" { 27 // mirror back the headers 28 fmt.Fprintln(w, must(marshalObj(r.Header, json.Marshal))) 29 } else { 30 fmt.Fprintln(w, body) 31 } 32 })) 33 34 client := &http.Client{ 35 Transport: &http.Transport{ 36 Proxy: func(req *http.Request) (*url.URL, error) { 37 return url.Parse(server.URL) 38 }, 39 }, 40 } 41 42 return server, client 43 } 44 45 func TestHTTPFile(t *testing.T) { 46 server, client := setupHTTP(200, "application/json; charset=utf-8", `{"hello": "world"}`) 47 defer server.Close() 48 49 sources := make(map[string]*Source) 50 sources["foo"] = &Source{ 51 Alias: "foo", 52 URL: &url.URL{ 53 Scheme: "http", 54 Host: "example.com", 55 Path: "/foo", 56 }, 57 hc: client, 58 } 59 data := &Data{ 60 Sources: sources, 61 } 62 63 expected := map[string]interface{}{ 64 "hello": "world", 65 } 66 67 actual, err := data.Datasource("foo") 68 assert.NoError(t, err) 69 assert.Equal(t, must(marshalObj(expected, json.Marshal)), must(marshalObj(actual, json.Marshal))) 70 71 actual, err = data.Datasource(server.URL) 72 assert.NoError(t, err) 73 assert.Equal(t, must(marshalObj(expected, json.Marshal)), must(marshalObj(actual, json.Marshal))) 74 } 75 76 func TestHTTPFileWithHeaders(t *testing.T) { 77 server, client := setupHTTP(200, jsonMimetype, "") 78 defer server.Close() 79 80 sources := make(map[string]*Source) 81 sources["foo"] = &Source{ 82 Alias: "foo", 83 URL: &url.URL{ 84 Scheme: "http", 85 Host: "example.com", 86 Path: "/foo", 87 }, 88 hc: client, 89 header: http.Header{ 90 "Foo": {"bar"}, 91 "foo": {"baz"}, 92 "User-Agent": {}, 93 "Accept-Encoding": {"test"}, 94 }, 95 } 96 data := &Data{ 97 Sources: sources, 98 } 99 expected := http.Header{ 100 "Accept-Encoding": {"test"}, 101 "Foo": {"bar", "baz"}, 102 } 103 actual, err := data.Datasource("foo") 104 assert.NoError(t, err) 105 assert.Equal(t, must(marshalObj(expected, json.Marshal)), must(marshalObj(actual, json.Marshal))) 106 107 expected = http.Header{ 108 "Accept-Encoding": {"test"}, 109 "Foo": {"bar", "baz"}, 110 "User-Agent": {"Go-http-client/1.1"}, 111 } 112 data = &Data{ 113 Sources: sources, 114 extraHeaders: map[string]http.Header{server.URL: expected}, 115 } 116 actual, err = data.Datasource(server.URL) 117 assert.NoError(t, err) 118 assert.Equal(t, must(marshalObj(expected, json.Marshal)), must(marshalObj(actual, json.Marshal))) 119 } 120 121 func TestParseHeaderArgs(t *testing.T) { 122 args := []string{ 123 "foo=Accept: application/json", 124 "bar=Authorization: Bearer supersecret", 125 } 126 expected := map[string]http.Header{ 127 "foo": { 128 "Accept": {jsonMimetype}, 129 }, 130 "bar": { 131 "Authorization": {"Bearer supersecret"}, 132 }, 133 } 134 parsed, err := parseHeaderArgs(args) 135 assert.NoError(t, err) 136 assert.Equal(t, expected, parsed) 137 138 _, err = parseHeaderArgs([]string{"foo"}) 139 assert.Error(t, err) 140 141 _, err = parseHeaderArgs([]string{"foo=bar"}) 142 assert.Error(t, err) 143 144 args = []string{ 145 "foo=Accept: application/json", 146 "foo=Foo: bar", 147 "foo=foo: baz", 148 "foo=fOO: qux", 149 "bar=Authorization: Bearer supersecret", 150 } 151 expected = map[string]http.Header{ 152 "foo": { 153 "Accept": {jsonMimetype}, 154 "Foo": {"bar", "baz", "qux"}, 155 }, 156 "bar": { 157 "Authorization": {"Bearer supersecret"}, 158 }, 159 } 160 parsed, err = parseHeaderArgs(args) 161 assert.NoError(t, err) 162 assert.Equal(t, expected, parsed) 163 } 164 165 func TestBuildURL(t *testing.T) { 166 expected := "https://example.com/index.html" 167 base := mustParseURL(expected) 168 u, err := buildURL(base) 169 assert.NoError(t, err) 170 assert.Equal(t, expected, u.String()) 171 172 expected = "https://example.com/index.html" 173 base = mustParseURL("https://example.com") 174 u, err = buildURL(base, "index.html") 175 assert.NoError(t, err) 176 assert.Equal(t, expected, u.String()) 177 178 expected = "https://example.com/a/b/c/index.html" 179 base = mustParseURL("https://example.com/a/") 180 u, err = buildURL(base, "b/c/index.html") 181 assert.NoError(t, err) 182 assert.Equal(t, expected, u.String()) 183 184 expected = "https://example.com/bar/baz/index.html" 185 base = mustParseURL("https://example.com/foo") 186 u, err = buildURL(base, "bar/baz/index.html") 187 assert.NoError(t, err) 188 assert.Equal(t, expected, u.String()) 189 }