Skip to content

Commit

Permalink
Set body to nil when empty
Browse files Browse the repository at this point in the history
Update test scenarios

Signed-off-by: Vicente Zepeda Mas <vzepedam@redhat.com>
Signed-off-by: Tomás Senart <tsenart@gmail.com>
  • Loading branch information
chentex authored and tsenart committed Jul 14, 2023
1 parent 500ec86 commit cd9cfd2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
27 changes: 17 additions & 10 deletions lib/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,25 @@ type Target struct {
// Request creates an *http.Request out of Target and returns it along with an
// error in case of failure.
func (t *Target) Request() (*http.Request, error) {
req, err := http.NewRequest(t.Method, t.URL, bytes.NewReader(t.Body))
var body io.Reader
if len(t.Body) != 0 {
body = bytes.NewReader(t.Body)
}

req, err := http.NewRequest(t.Method, t.URL, body)
if err != nil {
return nil, err
}

for k, vs := range t.Header {
req.Header[k] = make([]string, len(vs))
copy(req.Header[k], vs)
}

if host := req.Header.Get("Host"); host != "" {
req.Host = host
}

return req, nil
}

Expand Down Expand Up @@ -116,12 +124,11 @@ func (tr Targeter) Decode(t *Target) error {
// The method and url fields are required. If present, the body field must be base64 encoded.
// The generated [JSON Schema](lib/target.schema.json) defines the format in detail.
//
// {"method":"POST", "url":"https://goku/1", "header":{"Content-Type":["text/plain"], "body": "Rk9P"}
// {"method":"GET", "url":"https://goku/2"}
// {"method":"POST", "url":"https://goku/1", "header":{"Content-Type":["text/plain"], "body": "Rk9P"}
// {"method":"GET", "url":"https://goku/2"}
//
// body will be set as the Target's body if no body is provided in each target definiton.
// hdr will be merged with the each Target's headers.
//
func NewJSONTargeter(src io.Reader, body []byte, header http.Header) Targeter {
type reader struct {
*bufio.Reader
Expand Down Expand Up @@ -242,13 +249,13 @@ func ReadAllTargets(t Targeter) (tgts []Target, err error) {
// NewHTTPTargeter returns a new Targeter that decodes one Target from the
// given io.Reader on every invocation. The format is as follows:
//
// GET https://foo.bar/a/b/c
// Header-X: 123
// Header-Y: 321
// @/path/to/body/file
// GET https://foo.bar/a/b/c
// Header-X: 123
// Header-Y: 321
// @/path/to/body/file
//
// POST https://foo.bar/b/c/a
// Header-X: 123
// POST https://foo.bar/b/c/a
// Header-X: 123
//
// body will be set as the Target's body if no body is provided.
// hdr will be merged with the each Target's headers.
Expand Down
33 changes: 33 additions & 0 deletions lib/targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ func TestTargetRequest(t *testing.T) {
}
}

func TestTargetRequest_EmptyBody(t *testing.T) {
t.Parallel()

tgt := Target{
Method: "GET",
URL: "http://:9999/",
Body: []byte{},
Header: http.Header{
"X-Some-Header": []string{"1"},
"X-Some-Other-Header": []string{"2"},
"X-Some-New-Header": []string{"3"},
"Host": []string{"lolcathost"},
},
}
req, _ := tgt.Request()
if req.Body != nil {
t.Fatal("Body should be nil")
}

tgt.Header.Set("X-Stuff", "0")
if req.Header.Get("X-Stuff") == "0" {
t.Error("Each Target must have its own Header")
}

want, got := tgt.Header.Get("Host"), req.Header.Get("Host")
if want != got {
t.Fatalf("Target Header wasn't copied correctly. Want: %s, Got: %s", want, got)
}
if req.Host != want {
t.Fatalf("Target Host wasn't copied correctly. Want: %s, Got: %s", want, req.Host)
}
}

func TestJSONTargeter(t *testing.T) {
target := func(s string) io.Reader {
return strings.NewReader(s + "\n")
Expand Down

0 comments on commit cd9cfd2

Please sign in to comment.