Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of http headers #657

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type httpRunner struct {
type httpRequest struct {
path string
method string
headers map[string]string
headers http.Header
mediaType string
body any
useCookie *bool
Expand Down Expand Up @@ -302,7 +302,7 @@ func (r *httpRequest) setCookieHeader(req *http.Request, cookies map[string]map[
}
}

func (r *httpRequest) setTraceHeader(req *http.Request, s *step) error {
func (r *httpRequest) setTraceHeader(s *step) error {
if r.trace == nil || !*r.trace {
return nil
}
Expand All @@ -314,7 +314,7 @@ func (r *httpRequest) setTraceHeader(req *http.Request, s *step) error {
return err
}
// Set Trace in the header
req.Header.Set("X-Runn-Trace", string(tj))
r.headers.Set("X-Runn-Trace", string(tj))
return nil
}

Expand Down Expand Up @@ -361,6 +361,19 @@ func (rnr *httpRunner) run(ctx context.Context, r *httpRequest, s *step) error {
return err
}

// Override useCookie
if r.useCookie == nil && rnr.useCookie != nil && *rnr.useCookie {
r.useCookie = rnr.useCookie
}

// Override trace
if r.trace == nil && rnr.trace != nil && *rnr.trace {
r.trace = rnr.trace
}
if err := r.setTraceHeader(s); err != nil {
return err
}

var (
req *http.Request
res *http.Response
Expand Down Expand Up @@ -416,23 +429,14 @@ func (rnr *httpRunner) run(ctx context.Context, r *httpRequest, s *step) error {
return err
}
r.setContentTypeHeader(req)

// Override useCookie
if r.useCookie == nil && rnr.useCookie != nil && *rnr.useCookie {
r.useCookie = rnr.useCookie
}
r.setCookieHeader(req, o.store.cookies)
// Override trace
if r.trace == nil && rnr.trace != nil && *rnr.trace {
r.trace = rnr.trace
}
if err := r.setTraceHeader(req, s); err != nil {
return err
}
for k, v := range r.headers {
req.Header.Set(k, v)
if k == "Host" {
req.Host = v
req.Header.Del(k)
for _, vv := range v {
req.Header.Add(k, vv)
if k == "Host" {
req.Host = vv
}
}
}

Expand All @@ -453,7 +457,13 @@ func (rnr *httpRunner) run(ctx context.Context, r *httpRequest, s *step) error {
req.Header.Set("Content-Type", r.mediaType)
}
for k, v := range r.headers {
req.Header.Set(k, v)
req.Header.Del(k)
for _, vv := range v {
req.Header.Add(k, vv)
if k == "Host" {
req.Host = vv
}
}
}

o.capturers.captureHTTPRequest(rnr.name, req)
Expand Down
29 changes: 13 additions & 16 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestHTTPRunnerRunUsingGitHubAPI(t *testing.T) {
path: "/users/k1LoW",
method: http.MethodGet,
mediaType: MediaTypeApplicationJSON,
headers: map[string]string{
"Authorization": fmt.Sprintf("token %s", os.Getenv("GITHUB_TOKEN")),
headers: http.Header{
"Authorization": []string{fmt.Sprintf("token %s", os.Getenv("GITHUB_TOKEN"))},
},
},
true,
Expand All @@ -45,8 +45,8 @@ func TestHTTPRunnerRunUsingGitHubAPI(t *testing.T) {
path: "/invalid/endpoint",
method: http.MethodGet,
mediaType: MediaTypeApplicationJSON,
headers: map[string]string{
"Authorization": fmt.Sprintf("token %s", os.Getenv("GITHUB_TOKEN")),
headers: http.Header{
"Authorization": []string{fmt.Sprintf("token %s", os.Getenv("GITHUB_TOKEN"))},
},
},
false,
Expand Down Expand Up @@ -507,7 +507,7 @@ func TestNotFollowRedirect(t *testing.T) {
&httpRequest{
path: "/redirect",
method: http.MethodGet,
headers: map[string]string{},
headers: http.Header{},
},
false,
http.StatusNotFound,
Expand All @@ -516,7 +516,7 @@ func TestNotFollowRedirect(t *testing.T) {
&httpRequest{
path: "/redirect",
method: http.MethodGet,
headers: map[string]string{},
headers: http.Header{},
},
true,
http.StatusFound,
Expand Down Expand Up @@ -576,7 +576,7 @@ func TestHTTPCerts(t *testing.T) {
req := &httpRequest{
path: "/users/1",
method: http.MethodGet,
headers: map[string]string{},
headers: http.Header{},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
Expand Down Expand Up @@ -624,7 +624,7 @@ func TestHTTPRunnerInitializeWithCerts(t *testing.T) {
req := &httpRequest{
path: "/users/1",
method: http.MethodGet,
headers: map[string]string{},
headers: http.Header{},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
Expand Down Expand Up @@ -817,16 +817,13 @@ func TestSetTraceHeader(t *testing.T) {
for _, tt := range tests {
t.Run(fmt.Sprintf("trace:%v", *tt.trace), func(t *testing.T) {
r := &httpRequest{
trace: tt.trace,
headers: http.Header{},
trace: tt.trace,
}
req := &http.Request{
Method: http.MethodPost,
Header: http.Header{"Content-Type": []string{"application/json"}},
if err := r.setTraceHeader(tt.step); err != nil {
t.Error(err)
}

r.setTraceHeader(req, tt.step)
got := req.Header.Get("X-Runn-Trace")

got := r.headers.Get("X-Runn-Trace")
if got != tt.want {
t.Errorf("got %v\nwant %v", got, tt.want)
}
Expand Down
15 changes: 12 additions & 3 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runn

import (
"fmt"
"net/http"
"regexp"
"strings"
"time"
Expand All @@ -14,7 +15,7 @@ import (
func parseHTTPRequest(v map[string]any) (*httpRequest, error) {
v = trimDelimiter(v)
req := &httpRequest{
headers: map[string]string{},
headers: http.Header{},
}
part, err := yaml.Marshal(v)
if err != nil {
Expand Down Expand Up @@ -45,8 +46,14 @@ func parseHTTPRequest(v map[string]any) (*httpRequest, error) {
return nil, fmt.Errorf("invalid request: %s", string(part))
}
for k, v := range hm {
req.headers[k], ok = v.(string)
if !ok {
switch v := v.(type) {
case string:
req.headers.Add(k, v)
case []any:
for _, vv := range v {
req.headers.Add(k, vv.(string))
}
default:
return nil, fmt.Errorf("invalid request: %s", string(part))
}
}
Expand Down Expand Up @@ -178,6 +185,8 @@ func parseGrpcRequest(v map[string]any, expand func(any) (any, error)) (*grpcReq
for _, vv := range v {
req.headers.Append(k, vv.(string))
}
default:
return nil, fmt.Errorf("invalid request: %s", string(part))
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestParseHTTPRequest(t *testing.T) {
path: "/login",
method: http.MethodPost,
mediaType: MediaTypeApplicationJSON,
headers: map[string]string{},
headers: http.Header{},
body: map[string]any{
"key": "value",
},
Expand All @@ -47,7 +47,7 @@ func TestParseHTTPRequest(t *testing.T) {
path: "/users/k1LoW",
method: http.MethodGet,
mediaType: "",
headers: map[string]string{},
headers: http.Header{},
body: nil,
useCookie: nil,
trace: nil,
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestParseHTTPRequest(t *testing.T) {
path: "/users/k1LoW",
method: http.MethodGet,
mediaType: "",
headers: map[string]string{},
headers: http.Header{},
body: nil,
useCookie: &use,
trace: nil,
Expand All @@ -100,7 +100,7 @@ func TestParseHTTPRequest(t *testing.T) {
path: "/users/k1LoW",
method: http.MethodGet,
mediaType: "",
headers: map[string]string{},
headers: http.Header{},
body: nil,
useCookie: nil,
trace: &use,
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestParseHTTPRequest(t *testing.T) {
path: "/users/k1LoW?page=2",
method: http.MethodGet,
mediaType: "",
headers: map[string]string{},
headers: http.Header{},
body: nil,
useCookie: &notUse,
trace: &notUse,
Expand Down
3 changes: 3 additions & 0 deletions testdata/book/http.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ steps:
get:
headers:
Authorization: 'Bearer xxxxx'
Multivalues:
- a
- b
body:
application/json:
null
Expand Down
Binary file modified testdata/http.yml.debugger.golden
Binary file not shown.
1 change: 1 addition & 0 deletions testdata/http.yml.runbook.golden
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ steps:
get:
headers:
Authorization: Bearer xxxxx
Multivalues: a
body:
application/json: null
test: |
Expand Down
20 changes: 10 additions & 10 deletions testdata/pick_step.http.yml.8.golden
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
88 fileupload:
89 desc: Post /upload with single file
90 req:
91 /upload:
92 post:
93 body:
94 multipart/form-data:
95 upload0: ../dummy.png
96 test: |
97 current.res.status == 201
91 fileupload:
92 desc: Post /upload with single file
93 req:
94 /upload:
95 post:
96 body:
97 multipart/form-data:
98 upload0: ../dummy.png
99 test: |
100 current.res.status == 201
Loading