Skip to content

Commit ff99f28

Browse files
authored
Fix(cuex): send vela/http request headers as the template declares them (#157)
* Fix(cuex): send vela/http request headers as the template declares them http.cue declares request headers and trailers as one string per name, but RequestVars decoded them as http.Header, a list per name. CUE refuses a list and the decoder refuses a string, so a template could set no request header at all: the call failed with "cannot unmarshal string into Go struct field DoParams.$params.request.header.X-Token of type []string". RequestVars now takes one string per name and the http.Header is built with Add, so names that differ only in case each keep their value. This matches the vela/http workflow provider, which has always taken headers this way. NewRequest builds the request a vela/http call describes, so a provider that sends it through its own client (kubevela's guarded vela/http) builds it the same way. Fixes kubevela/kubevela#7595 Signed-off-by: Brian Kane <briankane1@gmail.com> * Fix(cuex): keep NewRequest's header map usable when none are set NewRequest replaced the request's header map with nil when a template sets no headers, so a caller adding a header to the result panicked. Headers are now added to the map the constructor made; trailers stay nil unless set, as net/http leaves them. Signed-off-by: Brian Kane <briankane1@gmail.com> --------- Signed-off-by: Brian Kane <briankane1@gmail.com>
1 parent 000437b commit ff99f28

2 files changed

Lines changed: 104 additions & 8 deletions

File tree

‎cue/cuex/providers/http/http.go‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ import (
2828
"github.com/kubevela/pkg/util/runtime"
2929
)
3030

31-
// RequestVars is the vars for http request
31+
// RequestVars is the vars for http request. Headers and trailers take one
32+
// string per name, as http.cue declares them.
3233
// TODO: support timeout & tls
3334
type RequestVars struct {
3435
Method string `json:"method"`
3536
URL string `json:"url"`
3637
Request struct {
37-
Body string `json:"body"`
38-
Header http.Header `json:"header"`
39-
Trailer http.Header `json:"trailer"`
38+
Body string `json:"body"`
39+
Header map[string]string `json:"header"`
40+
Trailer map[string]string `json:"trailer"`
4041
} `json:"request"`
4142
}
4243

44+
func addAll(header http.Header, values map[string]string) {
45+
for name, value := range values {
46+
header.Add(name, value)
47+
}
48+
}
49+
4350
// ResponseVars is the vars for http response
4451
type ResponseVars struct {
4552
Body string `json:"body"`
@@ -54,15 +61,27 @@ type DoParams providers.Params[RequestVars]
5461
// DoReturns returned struct for http response
5562
type DoReturns providers.Returns[ResponseVars]
5663

64+
// NewRequest builds the HTTP request a vela/http call describes, so a provider
65+
// that sends it through its own client builds it the same way.
66+
func NewRequest(ctx context.Context, vars RequestVars) (*http.Request, error) {
67+
req, err := http.NewRequestWithContext(ctx, vars.Method, vars.URL, strings.NewReader(vars.Request.Body))
68+
if err != nil {
69+
return nil, err
70+
}
71+
addAll(req.Header, vars.Request.Header)
72+
if vars.Request.Trailer != nil {
73+
req.Trailer = make(http.Header, len(vars.Request.Trailer))
74+
addAll(req.Trailer, vars.Request.Trailer)
75+
}
76+
return req, nil
77+
}
78+
5779
// Do execute http request and process returned result
5880
func Do(ctx context.Context, doParams *DoParams) (*DoReturns, error) {
59-
params := doParams.Params
60-
req, err := http.NewRequestWithContext(ctx, params.Method, params.URL, strings.NewReader(params.Request.Body))
81+
req, err := NewRequest(ctx, doParams.Params)
6182
if err != nil {
6283
return nil, err
6384
}
64-
req.Header = params.Request.Header
65-
req.Trailer = params.Request.Trailer
6685

6786
resp, err := http.DefaultClient.Do(req)
6887
if err != nil {

‎cue/cuex/providers/http/http_test.go‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ package http_test
1818

1919
import (
2020
"context"
21+
"fmt"
22+
"io"
2123
nethttp "net/http"
2224
"net/http/httptest"
25+
"sort"
26+
"strings"
2327
"testing"
2428

29+
"cuelang.org/go/cue"
2530
"github.com/stretchr/testify/require"
2631

32+
"github.com/kubevela/pkg/cue/cuex"
2733
"github.com/kubevela/pkg/cue/cuex/providers/http"
2834
)
2935

@@ -47,3 +53,74 @@ func TestDo(t *testing.T) {
4753
})
4854
require.Error(t, err)
4955
}
56+
57+
func TestDoSendsRequestHeaders(t *testing.T) {
58+
svr := httptest.NewServer(nethttp.HandlerFunc(func(writer nethttp.ResponseWriter, request *nethttp.Request) {
59+
_, _ = writer.Write([]byte(request.Header.Get("X-Token") + "," + request.Header.Get("Content-Type")))
60+
}))
61+
defer svr.Close()
62+
src := fmt.Sprintf(`
63+
import "vela/http"
64+
65+
r: http.#Do & {$params: {
66+
method: "POST"
67+
url: %q
68+
request: header: {"X-Token": "abc", "Content-Type": "application/json"}
69+
}}
70+
`, svr.URL)
71+
v, err := cuex.NewCompilerWithInternalPackages(http.Package).CompileString(context.Background(), src)
72+
require.NoError(t, err)
73+
body, err := v.LookupPath(cue.ParsePath("r.$returns.body")).String()
74+
require.NoError(t, err)
75+
require.Equal(t, "abc,application/json", body)
76+
}
77+
78+
func TestDoSendsCaseVariantHeaders(t *testing.T) {
79+
svr := httptest.NewServer(nethttp.HandlerFunc(func(writer nethttp.ResponseWriter, request *nethttp.Request) {
80+
values := request.Header.Values("X-Tag")
81+
sort.Strings(values)
82+
_, _ = writer.Write([]byte(strings.Join(values, ",")))
83+
}))
84+
defer svr.Close()
85+
src := fmt.Sprintf(`
86+
import "vela/http"
87+
88+
r: http.#Do & {$params: {
89+
method: "GET"
90+
url: %q
91+
request: header: {"X-Tag": "a", "x-tag": "b"}
92+
}}
93+
`, svr.URL)
94+
v, err := cuex.NewCompilerWithInternalPackages(http.Package).CompileString(context.Background(), src)
95+
require.NoError(t, err)
96+
body, err := v.LookupPath(cue.ParsePath("r.$returns.body")).String()
97+
require.NoError(t, err)
98+
require.Equal(t, "a,b", body)
99+
}
100+
101+
func TestNewRequest(t *testing.T) {
102+
vars := http.RequestVars{Method: "PUT", URL: "http://example.com/x"}
103+
vars.Request.Body = "payload"
104+
vars.Request.Header = map[string]string{"X-Token": "abc"}
105+
vars.Request.Trailer = map[string]string{"X-Sum": "1"}
106+
req, err := http.NewRequest(context.Background(), vars)
107+
require.NoError(t, err)
108+
require.Equal(t, "PUT", req.Method)
109+
require.Equal(t, "http://example.com/x", req.URL.String())
110+
require.Equal(t, "abc", req.Header.Get("X-Token"))
111+
require.Equal(t, "1", req.Trailer.Get("X-Sum"))
112+
b, err := io.ReadAll(req.Body)
113+
require.NoError(t, err)
114+
require.Equal(t, "payload", string(b))
115+
116+
_, err = http.NewRequest(context.Background(), http.RequestVars{Method: "GET", URL: "://bad"})
117+
require.Error(t, err)
118+
}
119+
120+
func TestNewRequestWithoutHeadersKeepsHeaderUsable(t *testing.T) {
121+
req, err := http.NewRequest(context.Background(), http.RequestVars{Method: "GET", URL: "http://example.com/"})
122+
require.NoError(t, err)
123+
require.NotNil(t, req.Header)
124+
req.Header.Set("X-Added", "later")
125+
require.Equal(t, "later", req.Header.Get("X-Added"))
126+
}

0 commit comments

Comments
 (0)