diff options
author | Martin Polden <mpolden@mpolden.no> | 2023-11-23 14:26:22 +0100 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2023-11-23 14:26:22 +0100 |
commit | f59873e5e8b4742068e5c9b4a8e6863686fc6700 (patch) | |
tree | 4aee35a19fa1bca23cf2aaffe7070ff11daac0c2 /client/go/internal | |
parent | b9b89862d0203e59594ba273b1ee060c7e198813 (diff) |
Support headers in basic HTTP tests
Diffstat (limited to 'client/go/internal')
-rw-r--r-- | client/go/internal/cli/cmd/test.go | 22 | ||||
-rw-r--r-- | client/go/internal/cli/cmd/test_test.go | 48 | ||||
-rw-r--r-- | client/go/internal/cli/cmd/testdata/tests/system-test/test.json | 7 |
3 files changed, 50 insertions, 27 deletions
diff --git a/client/go/internal/cli/cmd/test.go b/client/go/internal/cli/cmd/test.go index f0432dd4f70..778e640760e 100644 --- a/client/go/internal/cli/cmd/test.go +++ b/client/go/internal/cli/cmd/test.go @@ -198,6 +198,14 @@ func verify(step step, defaultCluster string, defaultParameters map[string]strin method = "GET" } + header := http.Header{} + for k, v := range step.Request.Headers { + header.Set(k, v) + } + if header.Get("Content-Type") == "" { // Set default if not specified by test + header.Set("Content-Type", "application/json") // TODO: Not guaranteed to be true ... + } + var service *vespa.Service requestUri := step.Request.URI if requestUri == "" { @@ -238,9 +246,6 @@ func verify(step step, defaultCluster string, defaultParameters map[string]strin } requestUrl.RawQuery = query.Encode() - header := http.Header{} - header.Add("Content-Type", "application/json") // TODO: Not guaranteed to be true ... - request := &http.Request{ URL: requestUrl, Method: method, @@ -464,11 +469,12 @@ type step struct { } type request struct { - Cluster string `json:"cluster"` - Method string `json:"method"` - URI string `json:"uri"` - ParametersRaw json.RawMessage `json:"parameters"` - BodyRaw json.RawMessage `json:"body"` + Cluster string `json:"cluster"` + Method string `json:"method"` + URI string `json:"uri"` + Headers map[string]string `json:"headers"` + ParametersRaw json.RawMessage `json:"parameters"` + BodyRaw json.RawMessage `json:"body"` } type response struct { diff --git a/client/go/internal/cli/cmd/test_test.go b/client/go/internal/cli/cmd/test_test.go index 9237221f003..1888db017d4 100644 --- a/client/go/internal/cli/cmd/test_test.go +++ b/client/go/internal/cli/cmd/test_test.go @@ -40,8 +40,8 @@ func TestSuite(t *testing.T) { assert.Equal(t, "", stderr.String()) baseUrl := "http://127.0.0.1:8080" urlWithQuery := baseUrl + "/search/?presentation.timing=true&query=artist%3A+foo&timeout=3.4s" - discoveryRequest := createSearchRequest("http://127.0.0.1:19071/application/v2/tenant/default/application/default/environment/prod/region/default/instance/default/serviceconverge") - requests := []*http.Request{discoveryRequest, createFeedRequest(baseUrl), createFeedRequest(baseUrl), createSearchRequest(urlWithQuery), createSearchRequest(urlWithQuery)} + discoveryRequest := createDiscoveryRequest() + requests := []*http.Request{discoveryRequest, createFeedRequest(baseUrl), createFeedRequest(baseUrl), createSearchRequest(urlWithQuery), createRequestWithCustomHeader(urlWithQuery)} requests = append(requests, discoveryRequest) requests = append(requests, createSearchRequest(baseUrl+"/search/")) requests = append(requests, createSearchRequest(baseUrl+"/search/?foo=%2F")) @@ -116,8 +116,8 @@ func TestSingleTest(t *testing.T) { baseUrl := "http://127.0.0.1:8080" rawUrl := baseUrl + "/search/?presentation.timing=true&query=artist%3A+foo&timeout=3.4s" - discoveryRequest := createSearchRequest("http://127.0.0.1:19071/application/v2/tenant/default/application/default/environment/prod/region/default/instance/default/serviceconverge") - assertRequests([]*http.Request{discoveryRequest, createFeedRequest(baseUrl), createFeedRequest(baseUrl), createSearchRequest(rawUrl), createSearchRequest(rawUrl)}, client, t) + discoveryRequest := createDiscoveryRequest() + assertRequests([]*http.Request{discoveryRequest, createFeedRequest(baseUrl), createFeedRequest(baseUrl), createSearchRequest(rawUrl), createRequestWithCustomHeader(rawUrl)}, client, t) } func TestSingleTestWithCloudAndEndpoints(t *testing.T) { @@ -156,7 +156,7 @@ func TestSingleTestWithCloudAndEndpoints(t *testing.T) { baseUrl := "https://url" rawUrl := baseUrl + "/search/?presentation.timing=true&query=artist%3A+foo&timeout=3.4s" - assertRequests([]*http.Request{createFeedRequest(baseUrl), createFeedRequest(baseUrl), createSearchRequest(rawUrl), createSearchRequest(rawUrl)}, client, t) + assertRequests([]*http.Request{createFeedRequest(baseUrl), createFeedRequest(baseUrl), createSearchRequest(rawUrl), createRequestWithCustomHeader(rawUrl)}, client, t) } func createFeedRequest(urlPrefix string) *http.Request { @@ -171,26 +171,40 @@ func createSearchRequest(rawUrl string) *http.Request { func createRequest(method string, uri string, body string) *http.Request { requestUrl, _ := url.ParseRequestURI(uri) - return &http.Request{ + r := &http.Request{ URL: requestUrl, Method: method, - Header: nil, + Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body)), } + r.Header.Set("Content-Type", "application/json") + return r +} + +func createRequestWithCustomHeader(url string) *http.Request { + r := createSearchRequest(url) + r.Header.Set("X-Foo", "bar") + return r +} + +func createDiscoveryRequest() *http.Request { + r := createSearchRequest("http://127.0.0.1:19071/application/v2/tenant/default/application/default/environment/prod/region/default/instance/default/serviceconverge") + r.Header = http.Header{} + return r } func assertRequests(requests []*http.Request, client *mock.HTTPClient, t *testing.T) { t.Helper() - if assert.Equal(t, len(requests), len(client.Requests)) { - for i, e := range requests { - a := client.Requests[i] - assert.Equal(t, e.URL.String(), a.URL.String()) - assert.Equal(t, e.Method, a.Method) - actualBody := a.Body - if actualBody == nil { - actualBody = io.NopCloser(strings.NewReader("")) - } - assert.Equal(t, util.ReaderToJSON(e.Body), util.ReaderToJSON(actualBody)) + require.Equal(t, len(requests), len(client.Requests)) + for i, want := range requests { + got := client.Requests[i] + assert.Equal(t, want.URL.String(), got.URL.String()) + assert.Equal(t, want.Method, got.Method) + assert.Equal(t, want.Header, got.Header) + actualBody := got.Body + if actualBody == nil { + actualBody = io.NopCloser(strings.NewReader("")) } + assert.Equal(t, util.ReaderToJSON(want.Body), util.ReaderToJSON(actualBody)) } } diff --git a/client/go/internal/cli/cmd/testdata/tests/system-test/test.json b/client/go/internal/cli/cmd/testdata/tests/system-test/test.json index 2e327b5e5df..b338c111d2c 100644 --- a/client/go/internal/cli/cmd/testdata/tests/system-test/test.json +++ b/client/go/internal/cli/cmd/testdata/tests/system-test/test.json @@ -19,7 +19,7 @@ "name": "re-feed music", "request": { "method": "POST", - "body": { + "body": { "fields": { "artist": "Foo Fighters" } @@ -44,7 +44,10 @@ "name": "query for foo again", "request": { "uri": "/search/?presentation.timing=true", - "parameters": "foo/query.json" + "parameters": "foo/query.json", + "headers": { + "X-Foo": "bar" + } }, "response": { "body": { |