mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-27 23:03:37 -05:00
add custom replacer
This commit is contained in:
parent
aba0ae358e
commit
3bfebfd79e
2 changed files with 55 additions and 0 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -86,6 +87,11 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
|
||||||
rep.replacements[headerReplacer+header+"}"] = strings.Join(val, ",")
|
rep.replacements[headerReplacer+header+"}"] = strings.Join(val, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
customReplacementsMutex.RLock()
|
||||||
|
for k, v := range customReplacements {
|
||||||
|
rep.replacements["{@"+k+"}"] = v
|
||||||
|
}
|
||||||
|
customReplacementsMutex.RUnlock()
|
||||||
return rep
|
return rep
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,3 +123,14 @@ const (
|
||||||
timeFormat = "02/Jan/2006:15:04:05 -0700"
|
timeFormat = "02/Jan/2006:15:04:05 -0700"
|
||||||
headerReplacer = "{>"
|
headerReplacer = "{>"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
customReplacements = map[string]string{}
|
||||||
|
customReplacementsMutex sync.RWMutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterReplacement(name, value string) {
|
||||||
|
customReplacementsMutex.Lock()
|
||||||
|
defer customReplacementsMutex.Unlock()
|
||||||
|
customReplacements[name] = value
|
||||||
|
}
|
||||||
|
|
|
@ -69,3 +69,41 @@ func TestReplace(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCustomReplace(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")
|
||||||
|
}
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
go func() {
|
||||||
|
RegisterReplacement("customId", "foobar")
|
||||||
|
replaceValues := NewReplacer(request, recordRequest, "")
|
||||||
|
switch v := replaceValues.(type) {
|
||||||
|
case replacer:
|
||||||
|
|
||||||
|
if v.Replace("This host is {host}") != "This host is caddyserver.com" {
|
||||||
|
t.Errorf("Expected host replacement failed")
|
||||||
|
}
|
||||||
|
if v.Replace("This request method is {method}") != "This request method is POST" {
|
||||||
|
t.Errorf("Expected method replacement failed")
|
||||||
|
}
|
||||||
|
if v.Replace("The response status is {status}") != "The response status is 200" {
|
||||||
|
t.Errorf("Expected status replacement failed")
|
||||||
|
}
|
||||||
|
if v.Replace("The custom replace is customId with value of {@customId}") != "The custom replace is customId with value of foobar" {
|
||||||
|
t.Errorf("Expected customId replacement failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
t.Fatalf("Return Value from New Replacer expected pass type assertion into a replacer type \n")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue