mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
More tests and simplify hashFilter
This commit is contained in:
+2
-1
@@ -68,7 +68,8 @@ func (m *HTTPModifier) Rewrite(payload []byte) (response []byte) {
|
||||
|
||||
hasher := fnv.New32a()
|
||||
hasher.Write(value)
|
||||
if hasher.Sum32() > f.maxHash {
|
||||
|
||||
if (hasher.Sum32() % 100) >= f.percent {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"github.com/buger/gor/proto"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func TestHTTPModifierWithoutConfig(t *testing.T) {
|
||||
@@ -36,3 +38,58 @@ func TestHTTPModifierHeaderFilters(t *testing.T) {
|
||||
t.Error("Request should not pass filters")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestHTTPModifierURLRegexp(t *testing.T) {
|
||||
var url, new_url []byte
|
||||
|
||||
rewrites := UrlRewriteMap{}
|
||||
|
||||
payload := func(url []byte) []byte {
|
||||
return []byte("POST " + string(url) + " HTTP/1.1\r\nContent-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")
|
||||
}
|
||||
|
||||
err := rewrites.Set("/v1/user/([^\\/]+)/ping:/v2/user/$1/ping")
|
||||
if err != nil {
|
||||
t.Error("Should not error on /v1/user/([^\\/]+)/ping:/v2/user/$1/ping")
|
||||
}
|
||||
|
||||
modifier := NewHTTPModifier(&HTTPModifierConfig{
|
||||
urlRewrite: rewrites,
|
||||
})
|
||||
|
||||
url = []byte("/v1/user/joe/ping")
|
||||
if new_url = proto.Path(modifier.Rewrite(payload(url))); bytes.Equal(new_url, url) {
|
||||
t.Error("Request url should have been rewritten, wasn't", string(new_url))
|
||||
}
|
||||
|
||||
url = []byte("/v1/user/ping")
|
||||
if new_url = proto.Path(modifier.Rewrite(payload(url))); !bytes.Equal(new_url, url) {
|
||||
t.Error("Request url should have been rewritten, wasn't", string(new_url))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHTTPModifierHeaderHashFilters(t *testing.T) {
|
||||
filters := HTTPHeaderHashFilters{}
|
||||
filters.Set("Header2:1/2")
|
||||
|
||||
modifier := NewHTTPModifier(&HTTPModifierConfig{
|
||||
headerHashFilters: filters,
|
||||
})
|
||||
|
||||
payload := func(header []byte) []byte {
|
||||
return []byte("POST / HTTP/1.1\r\n" + string(header) + "Content-Length: 7\r\nHost: www.w3.org\r\n\r\na=1&b=2")
|
||||
}
|
||||
|
||||
if p := modifier.Rewrite(payload([]byte(""))); len(p) > 0 {
|
||||
t.Error("Request should not pass filters, Header2 does not exist")
|
||||
}
|
||||
|
||||
if p := modifier.Rewrite(payload([]byte("Header2: 3\r\n"))); len(p) > 0 {
|
||||
t.Error("Request should not pass filters, Header2 hash too high")
|
||||
}
|
||||
|
||||
if p := modifier.Rewrite(payload([]byte("Header2: 1\r\n"))); len(p) == 0 {
|
||||
t.Error("Request should pass filters")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
type headerHashFilter struct {
|
||||
name []byte
|
||||
maxHash uint32
|
||||
percent uint32
|
||||
}
|
||||
|
||||
type HTTPHeaderHashFilters []headerHashFilter
|
||||
@@ -33,17 +33,9 @@ func (h *HTTPHeaderHashFilters) Set(value string) error {
|
||||
num, _ = strconv.ParseUint(fracArr[0], 10, 64)
|
||||
den, _ = strconv.ParseUint(fracArr[1], 10, 64)
|
||||
|
||||
if num < 1 || den < 1 || num > den {
|
||||
panic("need positive numerators and denominators, with the former less than the latter.")
|
||||
}
|
||||
|
||||
if den&(den-1) != 0 {
|
||||
return errors.New("must have a denominator which is a power of two.")
|
||||
}
|
||||
|
||||
var f headerHashFilter
|
||||
f.name = []byte(valArr[0])
|
||||
f.maxHash = (uint32)(num * (((uint64)(2 << 31)) / den))
|
||||
f.percent = uint32((float64(num) / float64(den)) * 100)
|
||||
*h = append(*h, f)
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1 +1,18 @@
|
||||
package main
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUrlRewriteMap(t *testing.T) {
|
||||
var err error
|
||||
rewrites := UrlRewriteMap{}
|
||||
|
||||
if err = rewrites.Set("/v1/user/([^\\/]+)/ping:/v2/user/$1/ping"); err != nil {
|
||||
t.Error("Should set mapping", err)
|
||||
}
|
||||
|
||||
if err = rewrites.Set("/v1/user/([^\\/]+)/ping"); err == nil {
|
||||
t.Error("Should not set mapping without :")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user