From 168723a026cdad53e2d5d8434003fa3e57f04e9a Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Thu, 24 Dec 2015 09:00:10 +0100 Subject: [PATCH 1/2] Added escaped versions of uri, query and path. --- middleware/replacer.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/middleware/replacer.go b/middleware/replacer.go index 29c695b7..3fb389a1 100644 --- a/middleware/replacer.go +++ b/middleware/replacer.go @@ -3,6 +3,7 @@ package middleware import ( "net" "net/http" + "net/url" "path" "strconv" "strings" @@ -38,11 +39,13 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla } return "http" }(), - "{host}": r.Host, - "{path}": r.URL.Path, - "{query}": r.URL.RawQuery, - "{fragment}": r.URL.Fragment, - "{proto}": r.Proto, + "{host}": r.Host, + "{path}": r.URL.Path, + "{path_escaped}": url.QueryEscape(r.URL.Path), + "{query}": r.URL.RawQuery, + "{query_escaped}": url.QueryEscape(r.URL.RawQuery), + "{fragment}": r.URL.Fragment, + "{proto}": r.Proto, "{remote}": func() string { if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" { return fwdFor @@ -60,7 +63,8 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla } return port }(), - "{uri}": r.URL.RequestURI(), + "{uri}": r.URL.RequestURI(), + "{uri_escaped}": url.QueryEscape(r.URL.RequestURI()), "{when}": func() string { return time.Now().Format(timeFormat) }(), From 7dadcd58341b8ad63893a4669913099edbcf937c Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Wed, 30 Dec 2015 20:42:03 +0100 Subject: [PATCH 2/2] Add ability to set custom values. --- middleware/replacer.go | 6 ++++++ middleware/replacer_test.go | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/middleware/replacer.go b/middleware/replacer.go index 3fb389a1..193fd4fc 100644 --- a/middleware/replacer.go +++ b/middleware/replacer.go @@ -16,6 +16,7 @@ import ( // NewReplacer to get one of these. type Replacer interface { Replace(string) string + Set(key, value string) } type replacer struct { @@ -117,6 +118,11 @@ func (r replacer) Replace(s string) string { return s } +// Set sets key to value in the replacements map. +func (r replacer) Set(key, value string) { + r.replacements["{"+key+"}"] = value +} + const ( timeFormat = "02/Jan/2006:15:04:05 -0700" headerReplacer = "{>" diff --git a/middleware/replacer_test.go b/middleware/replacer_test.go index 39d91a22..8f1147de 100644 --- a/middleware/replacer_test.go +++ b/middleware/replacer_test.go @@ -69,3 +69,43 @@ func TestReplace(t *testing.T) { } } + +func TestSet(t *testing.T) { + w := httptest.NewRecorder() + recordRequest := NewResponseRecorder(w) + userJSON := `{"username": "dennis"}` + + reader := strings.NewReader(userJSON) //Convert string to reader + + request, err := http.NewRequest("POST", "http://caddyserver.com", reader) //Create request with JSON body + if err != nil { + t.Fatalf("Request Formation Failed \n") + } + replaceValues := NewReplacer(request, recordRequest, "") + + replaceValues.Set("host", "getcaddy.com") + replaceValues.Set("method", "GET") + replaceValues.Set("status", "201") + replaceValues.Set("variable", "value") + + switch v := replaceValues.(type) { + case replacer: + + if v.Replace("This host is {host}") != "This host is getcaddy.com" { + t.Errorf("Expected host replacement failed") + } + if v.Replace("This request method is {method}") != "This request method is GET" { + t.Errorf("Expected method replacement failed") + } + if v.Replace("The response status is {status}") != "The response status is 201" { + t.Errorf("Expected status replacement failed") + } + if v.Replace("The value of variable is {variable}") != "The value of variable is value" { + t.Errorf("Expected status replacement failed") + } + + default: + t.Fatalf("Return Value from New Replacer expected pass type assertion into a replacer type \n") + } + +}