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:
Felix Niederwanger 2024-04-01 17:10:25 +02:00
parent 288e23169e
commit 4bc8085103
Signed by: phoenix
GPG key ID: 6E77A590E3F6D71C
3 changed files with 23 additions and 6 deletions

View file

@ -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 {

View file

@ -7,6 +7,7 @@ import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/http"
@ -225,6 +226,10 @@ func RegisterHandlers(cf Config, mux *http.ServeMux) error {
if hook.Concurrency < 1 {
hook.Concurrency = 1
}
// allow hooks to have individual maxBodySize arguments.
if cf.Settings.MaxBodySize > 0 && hook.maxBodySize == 0 {
hook.maxBodySize = cf.Settings.MaxBodySize
}
mux.HandleFunc(hook.Route, createHandler(hook))
}
return nil
@ -306,11 +311,11 @@ func createHandler(hook Hook) Handler {
buffer.WriteString(fmt.Sprintf("%s:%s\n", k, strings.Join(v, ",")))
}
buffer.WriteString("\n")
// Receive body, if desired
if cf.Settings.MaxBodySize > 0 {
body := make([]byte, cf.Settings.MaxBodySize)
// Receive body only if configured. By default the body is ignored.
if hook.maxBodySize > 0 {
body := make([]byte, hook.maxBodySize)
n, err := r.Body.Read(body)
if err != nil {
if err != nil && err != io.EOF {
log.Printf("receive body failed: %s", err)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(500)

View file

@ -281,8 +281,12 @@ 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"})
@ -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 {