Request placeholders: extract query argument (#1478)

* feat(request placeholders): adds {?arg}

* test(request placeholders): test query argument extractor {?arg}
This commit is contained in:
Samuel BERTHE 2017-03-03 07:25:28 +01:00 committed by Matt Holt
parent 0a0d2cc1cf
commit 75ccc05d84
2 changed files with 11 additions and 2 deletions

View File

@ -207,6 +207,12 @@ func (r *replacer) getSubstitution(key string) string {
return cookie.Value return cookie.Value
} }
} }
// next check for query argument
if key[1] == '?' {
query := r.request.URL.Query()
name := key[2 : len(key)-1]
return query.Get(name)
}
// search default replacements in the end // search default replacements in the end
switch key { switch key {

View File

@ -38,7 +38,7 @@ func TestReplace(t *testing.T) {
recordRequest := NewResponseRecorder(w) recordRequest := NewResponseRecorder(w)
reader := strings.NewReader(`{"username": "dennis"}`) reader := strings.NewReader(`{"username": "dennis"}`)
request, err := http.NewRequest("POST", "http://localhost", reader) request, err := http.NewRequest("POST", "http://localhost/?foo=bar", reader)
if err != nil { if err != nil {
t.Fatal("Request Formation Failed\n") t.Fatal("Request Formation Failed\n")
} }
@ -73,7 +73,7 @@ func TestReplace(t *testing.T) {
{"{when_iso}", "2006-01-02T15:04:12Z"}, {"{when_iso}", "2006-01-02T15:04:12Z"},
{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."}, {"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."}, {"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."},
{"The request is {request}.", "The request is POST / HTTP/1.1\\r\\nHost: localhost\\r\\n" + {"The request is {request}.", "The request is POST /?foo=bar HTTP/1.1\\r\\nHost: localhost\\r\\n" +
"Cookie: foo=bar; taste=delicious\\r\\nCustom: foobarbaz\\r\\nCustomadd: caddy\\r\\n" + "Cookie: foo=bar; taste=delicious\\r\\nCustom: foobarbaz\\r\\nCustomadd: caddy\\r\\n" +
"Shorterval: 1\\r\\n\\r\\n."}, "Shorterval: 1\\r\\n\\r\\n."},
{"The cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."}, {"The cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."},
@ -84,6 +84,9 @@ func TestReplace(t *testing.T) {
{"Bad {}", "Bad -"}, {"Bad {}", "Bad -"},
{"Cookies are {~taste}", "Cookies are delicious"}, {"Cookies are {~taste}", "Cookies are delicious"},
{"Missing cookie is {~missing}", "Missing cookie is -"}, {"Missing cookie is {~missing}", "Missing cookie is -"},
{"Query string is {query}", "Query string is foo=bar"},
{"Query string value for foo is {?foo}", "Query string value for foo is bar"},
{"Missing query string argument is {?missing}", "Missing query string argument is "},
} }
for _, c := range testCases { for _, c := range testCases {