mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-13 22:51:08 -05:00
commit
55aa492dc1
2 changed files with 56 additions and 6 deletions
|
@ -3,6 +3,7 @@ package middleware
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -15,6 +16,7 @@ import (
|
||||||
// NewReplacer to get one of these.
|
// NewReplacer to get one of these.
|
||||||
type Replacer interface {
|
type Replacer interface {
|
||||||
Replace(string) string
|
Replace(string) string
|
||||||
|
Set(key, value string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type replacer struct {
|
type replacer struct {
|
||||||
|
@ -38,11 +40,13 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
|
||||||
}
|
}
|
||||||
return "http"
|
return "http"
|
||||||
}(),
|
}(),
|
||||||
"{host}": r.Host,
|
"{host}": r.Host,
|
||||||
"{path}": r.URL.Path,
|
"{path}": r.URL.Path,
|
||||||
"{query}": r.URL.RawQuery,
|
"{path_escaped}": url.QueryEscape(r.URL.Path),
|
||||||
"{fragment}": r.URL.Fragment,
|
"{query}": r.URL.RawQuery,
|
||||||
"{proto}": r.Proto,
|
"{query_escaped}": url.QueryEscape(r.URL.RawQuery),
|
||||||
|
"{fragment}": r.URL.Fragment,
|
||||||
|
"{proto}": r.Proto,
|
||||||
"{remote}": func() string {
|
"{remote}": func() string {
|
||||||
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
|
if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" {
|
||||||
return fwdFor
|
return fwdFor
|
||||||
|
@ -60,7 +64,8 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
|
||||||
}
|
}
|
||||||
return port
|
return port
|
||||||
}(),
|
}(),
|
||||||
"{uri}": r.URL.RequestURI(),
|
"{uri}": r.URL.RequestURI(),
|
||||||
|
"{uri_escaped}": url.QueryEscape(r.URL.RequestURI()),
|
||||||
"{when}": func() string {
|
"{when}": func() string {
|
||||||
return time.Now().Format(timeFormat)
|
return time.Now().Format(timeFormat)
|
||||||
}(),
|
}(),
|
||||||
|
@ -113,6 +118,11 @@ func (r replacer) Replace(s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set sets key to value in the replacements map.
|
||||||
|
func (r replacer) Set(key, value string) {
|
||||||
|
r.replacements["{"+key+"}"] = value
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
timeFormat = "02/Jan/2006:15:04:05 -0700"
|
timeFormat = "02/Jan/2006:15:04:05 -0700"
|
||||||
headerReplacer = "{>"
|
headerReplacer = "{>"
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue