Skip to content

Instantly share code, notes, and snippets.

@jstr
Created August 4, 2016 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstr/87a32f93f3d5b6d6bb1bae31a48bb3db to your computer and use it in GitHub Desktop.
Save jstr/87a32f93f3d5b6d6bb1bae31a48bb3db to your computer and use it in GitHub Desktop.
[PATCH] Accept HTTP request body content in targets file via @@ prefix
From d6fecfa5e32e692664a651ab60e0975302da7e6e Mon Sep 17 00:00:00 2001
From: James Stradling <james@thisdata.com>
Date: Wed, 27 Jul 2016 18:18:07 +1300
Subject: [PATCH] Accept HTTP request body content in targets file via @@
prefix
diff --git a/lib/targets.go b/lib/targets.go
index 394e9eb..e01105b 100644
--- a/lib/targets.go
+++ b/lib/targets.go
@@ -144,6 +144,10 @@ func NewLazyTargeter(src io.Reader, body []byte, hdr http.Header) Targeter {
for sc.Scan() {
if line = strings.TrimSpace(sc.Text()); line == "" {
break
+ } else if strings.HasPrefix(line, "@@") {
+ bodyString := line[2:]
+ tgt.Body = []byte(bodyString)
+ break
} else if strings.HasPrefix(line, "@") {
if tgt.Body, err = ioutil.ReadFile(line[1:]); err != nil {
return fmt.Errorf("bad body: %s", err)
diff --git a/lib/targets_test.go b/lib/targets_test.go
index 3a3c0b0..4b188cc 100644
--- a/lib/targets_test.go
+++ b/lib/targets_test.go
@@ -142,6 +142,12 @@ func TestNewLazyTargeter(t *testing.T) {
POST http://foobar.org/fnord/2
Authorization: x67890
@`, bodyf.Name(),
+ `
+
+ POST http://foobar.org/fnord/3
+ Content-Type: application/json
+ @@{"some":"json"}
+ `
)
src := bytes.NewBufferString(strings.TrimSpace(targets))
@@ -185,6 +191,13 @@ func TestNewLazyTargeter(t *testing.T) {
"Authorization": []string{"x67890"},
"Content-Type": []string{"text/plain"},
},
+ {
+ Method: "POST",
+ URL: "http://foobar.org/fnord/3",
+ Body: []byte("{\"some\":\"json\"}"),
+ Header: http.Header{
+ "Content-Type": []string{"application/json"},
+ },
},
} {
var got Target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment