github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/datasources_http_test.go (about) 1 package integration 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 ) 8 9 func setupDatasourcesHTTPTest(t *testing.T) *httptest.Server { 10 mux := http.NewServeMux() 11 12 mux.HandleFunc("/mirror", mirrorHandler) 13 mux.HandleFunc("/not.json", typeHandler("application/yaml", "value: notjson\n")) 14 mux.HandleFunc("/foo", typeHandler("application/json", `{"value": "json"}`)) 15 mux.HandleFunc("/actually.json", typeHandler("", `{"value": "json"}`)) 16 mux.HandleFunc("/bogus.csv", typeHandler("text/plain", `{"value": "json"}`)) 17 mux.HandleFunc("/list", typeHandler("application/array+json", `[1, 2, 3, 4, 5]`)) 18 19 srv := httptest.NewServer(mux) 20 t.Cleanup(srv.Close) 21 22 return srv 23 } 24 25 func TestDatasources_HTTP(t *testing.T) { 26 srv := setupDatasourcesHTTPTest(t) 27 28 o, e, err := cmd(t, 29 "-d", "foo="+srv.URL+"/mirror", 30 "-H", "foo=Foo:bar", 31 "-i", "{{ index (ds `foo`).headers.Foo 0 }}").run() 32 assertSuccess(t, o, e, err, "bar") 33 34 o, e, err = cmd(t, 35 "-H", "foo=Foo:bar", 36 "-i", "{{defineDatasource `foo` `"+srv.URL+"/mirror`}}{{ index (ds `foo`).headers.Foo 0 }}").run() 37 assertSuccess(t, o, e, err, "bar") 38 39 o, e, err = cmd(t, 40 "-i", "{{ $d := ds `"+srv.URL+"/mirror`}}{{ index (index $d.headers `Accept-Encoding`) 0 }}").run() 41 assertSuccess(t, o, e, err, "gzip") 42 } 43 44 func TestDatasources_HTTP_TypeOverridePrecedence(t *testing.T) { 45 srv := setupDatasourcesHTTPTest(t) 46 47 o, e, err := cmd(t, 48 "-d", "foo="+srv.URL+"/foo", 49 "-i", "{{ (ds `foo`).value }}").run() 50 assertSuccess(t, o, e, err, "json") 51 52 o, e, err = cmd(t, 53 "-d", "foo="+srv.URL+"/not.json", 54 "-i", "{{ (ds `foo`).value }}").run() 55 assertSuccess(t, o, e, err, "notjson") 56 57 o, e, err = cmd(t, 58 "-d", "foo="+srv.URL+"/actually.json", 59 "-i", "{{ (ds `foo`).value }}").run() 60 assertSuccess(t, o, e, err, "json") 61 62 o, e, err = cmd(t, 63 "-d", "foo="+srv.URL+"/bogus.csv?type=application/json", 64 "-i", "{{ (ds `foo`).value }}").run() 65 assertSuccess(t, o, e, err, "json") 66 67 o, e, err = cmd(t, 68 "-c", ".="+srv.URL+"/list?type=application/array+json", 69 "-i", "{{ range . }}{{ . }}{{ end }}").run() 70 assertSuccess(t, o, e, err, "12345") 71 } 72 73 func TestDatasources_HTTP_AppendQueryAfterSubPaths(t *testing.T) { 74 srv := setupDatasourcesHTTPTest(t) 75 76 o, e, err := cmd(t, 77 "-d", "foo="+srv.URL+"/?type=application/json", 78 "-i", "{{ (ds `foo` `bogus.csv`).value }}").run() 79 assertSuccess(t, o, e, err, "json") 80 }