Skip to content

Commit

Permalink
Fix handling of http headers
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Oct 26, 2023
1 parent 0a4ec4c commit 593d023
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 31 deletions.
19 changes: 14 additions & 5 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 @@ -430,9 +430,12 @@ func (rnr *httpRunner) run(ctx context.Context, r *httpRequest, s *step) error {
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 +456,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
16 changes: 8 additions & 8 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
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

0 comments on commit 593d023

Please sign in to comment.