summaryrefslogtreecommitdiff
path: root/internal/tests/integration/datasources_http_test.go
blob: 2c4390de6f8106616791298766f9c5d42fc7c6bd (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
75
76
77
78
79
80
81
82
83
84
85
86
87
package integration

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

func setupDatasourcesHTTPTest(t *testing.T) *httptest.Server {
	mux := http.NewServeMux()

	mux.HandleFunc("/mirror", mirrorHandler)
	mux.HandleFunc("/not.json", typeHandler("application/yaml", "value: notjson\n"))
	mux.HandleFunc("/foo", typeHandler("application/json", `{"value": "json"}`))
	mux.HandleFunc("/actually.json", typeHandler("", `{"value": "json"}`))
	mux.HandleFunc("/bogus.csv", typeHandler("text/plain", `{"value": "json"}`))
	mux.HandleFunc("/list", typeHandler("application/array+json", `[1, 2, 3, 4, 5]`))
	mux.HandleFunc("/params", paramHandler(t))

	srv := httptest.NewServer(mux)
	t.Cleanup(srv.Close)

	return srv
}

func TestDatasources_HTTP(t *testing.T) {
	srv := setupDatasourcesHTTPTest(t)

	o, e, err := cmd(t,
		"-d", "foo="+srv.URL+"/mirror",
		"-H", "foo=Foo:bar",
		"-i", "{{ index (ds `foo`).headers.Foo 0 }}").run()
	assertSuccess(t, o, e, err, "bar")

	o, e, err = cmd(t,
		"-H", "foo=Foo:bar",
		"-i", "{{defineDatasource `foo` `"+srv.URL+"/mirror`}}{{ index (ds `foo`).headers.Foo 0 }}").run()
	assertSuccess(t, o, e, err, "bar")

	o, e, err = cmd(t,
		"-i", "{{ $d := ds `"+srv.URL+"/mirror`}}{{ index (index $d.headers `Accept-Encoding`) 0 }}").run()
	assertSuccess(t, o, e, err, "gzip")
}

func TestDatasources_HTTP_TypeOverridePrecedence(t *testing.T) {
	srv := setupDatasourcesHTTPTest(t)

	o, e, err := cmd(t,
		"-d", "foo="+srv.URL+"/foo",
		"-i", "{{ (ds `foo`).value }}").run()
	assertSuccess(t, o, e, err, "json")

	o, e, err = cmd(t,
		"-d", "foo="+srv.URL+"/not.json",
		"-i", "{{ (ds `foo`).value }}").run()
	assertSuccess(t, o, e, err, "notjson")

	o, e, err = cmd(t,
		"-d", "foo="+srv.URL+"/actually.json",
		"-i", "{{ (ds `foo`).value }}").run()
	assertSuccess(t, o, e, err, "json")

	o, e, err = cmd(t,
		"-d", "foo="+srv.URL+"/bogus.csv?type=application/json",
		"-i", "{{ (ds `foo`).value }}").run()
	assertSuccess(t, o, e, err, "json")

	o, e, err = cmd(t,
		"-c", ".="+srv.URL+"/list?type=application/array+json",
		"-i", "{{ range . }}{{ . }}{{ end }}").run()
	assertSuccess(t, o, e, err, "12345")

	o, e, err = cmd(t,
		"-c", ".="+srv.URL+"/params?foo=bar&type=http&_type=application/json",
		"-i", "{{ . | toJSON }}").
		withEnv("GOMPLATE_TYPE_PARAM", "_type").run()
	assertSuccess(t, o, e, err, `{"foo":["bar"],"type":["http"]}`)
}

func TestDatasources_HTTP_AppendQueryAfterSubPaths(t *testing.T) {
	srv := setupDatasourcesHTTPTest(t)

	o, e, err := cmd(t,
		"-d", "foo="+srv.URL+"/?type=application/json",
		"-i", "{{ (ds `foo` `bogus.csv`).value }}").run()
	assertSuccess(t, o, e, err, "json")
}