Add unit test for the message body and headers
Adds unit tests for passing of the message body and the message headers.
This commit is contained in:
parent
288e23169e
commit
3ad2e79deb
|
@ -25,6 +25,8 @@ type Hook struct {
|
|||
BlockedAddresses []string `yaml:"blocked"` // Addresses that are explicitly blocked
|
||||
HttpBasicAuth BasicAuth `yaml:"basic_auth"` // Optional requires http basic auth
|
||||
Env map[string]string `yaml:"env"` // Optional environment variables
|
||||
|
||||
maxBodySize int64 // Maximum allowed body size for this hook
|
||||
}
|
||||
|
||||
type BasicAuth struct {
|
||||
|
|
|
@ -225,6 +225,9 @@ func RegisterHandlers(cf Config, mux *http.ServeMux) error {
|
|||
if hook.Concurrency < 1 {
|
||||
hook.Concurrency = 1
|
||||
}
|
||||
if cf.Settings.MaxBodySize > 0 && hook.maxBodySize == 0 {
|
||||
hook.maxBodySize = cf.Settings.MaxBodySize
|
||||
}
|
||||
mux.HandleFunc(hook.Route, createHandler(hook))
|
||||
}
|
||||
return nil
|
||||
|
@ -307,8 +310,8 @@ func createHandler(hook Hook) Handler {
|
|||
}
|
||||
buffer.WriteString("\n")
|
||||
// Receive body, if desired
|
||||
if cf.Settings.MaxBodySize > 0 {
|
||||
body := make([]byte, cf.Settings.MaxBodySize)
|
||||
if hook.maxBodySize > 0 {
|
||||
body := make([]byte, hook.maxBodySize)
|
||||
n, err := r.Body.Read(body)
|
||||
if err != nil {
|
||||
log.Printf("receive body failed: %s", err)
|
||||
|
|
|
@ -281,10 +281,14 @@ func TestHeaderAndBody(t *testing.T) {
|
|||
|
||||
var cf Config
|
||||
|
||||
bodyIncluded := "this is the request body\nit is awesome"
|
||||
bodyIgnored := "this part of the body should be ignored\nIt is hopefully not present"
|
||||
bodyText := fmt.Sprintf("%s\n%s", bodyIncluded, bodyIgnored)
|
||||
|
||||
cf.Settings.BindAddress = "127.0.0.1:2088"
|
||||
cf.Settings.MaxBodySize = 4096
|
||||
cf.Settings.MaxBodySize = int64(len(bodyIncluded))
|
||||
cf.Hooks = make([]Hook, 0)
|
||||
cf.Hooks = append(cf.Hooks, Hook{Name: "hook", Command: fmt.Sprintf("tee %s", tempFile.Name()), Route: "/header_and_body"})
|
||||
cf.Hooks = append(cf.Hooks, Hook{Name: "hook", Command: fmt.Sprintf("tee %s", tempFile.Name()), Route: "/header_and_body", maxBodySize: cf.Settings.MaxBodySize})
|
||||
|
||||
listener, err := CreateListener(cf)
|
||||
if err != nil {
|
||||
|
@ -320,7 +324,7 @@ func TestHeaderAndBody(t *testing.T) {
|
|||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
req.Body = io.NopCloser(bytes.NewReader([]byte("this is the request body\nit is awesome")))
|
||||
req.Body = io.NopCloser(bytes.NewReader([]byte(bodyText)))
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("http request error: %s", err)
|
||||
|
@ -348,8 +352,14 @@ func TestHeaderAndBody(t *testing.T) {
|
|||
}
|
||||
|
||||
// Assert the message body got passed as well
|
||||
if !strings.Contains(contents, bodyIncluded) {
|
||||
t.Fatal("Message body was not passed")
|
||||
}
|
||||
|
||||
// Assert the messaeg body got cropped
|
||||
if strings.Contains(contents, bodyIgnored) {
|
||||
t.Fatal("Cut-off after max-body size didn't happen")
|
||||
}
|
||||
}
|
||||
|
||||
func generateKeypair(keyfile string, certfile string, hostnames []string) error {
|
||||
|
|
Loading…
Reference in a new issue