diff --git a/docs/reference/filters.md b/docs/reference/filters.md index b47b9b1104..fddb581e5a 100644 --- a/docs/reference/filters.md +++ b/docs/reference/filters.md @@ -746,9 +746,9 @@ Parameters: Example: ``` -* -> logHeader("request") -> "https://www.example.org"; -* -> logHeader("response") -> "https://www.example.org"; -* -> logHeader("request", "response") -> "https://www.example.org"; +* -> logBody("request") -> "https://www.example.org"; +* -> logBody("response") -> "https://www.example.org"; +* -> logBody("request", "response") -> "https://www.example.org"; ``` ## Timeout diff --git a/filters/diag/logbody.go b/filters/diag/logbody.go index 313c66ef46..d5c320d425 100644 --- a/filters/diag/logbody.go +++ b/filters/diag/logbody.go @@ -29,7 +29,7 @@ func (logBody) CreateFilter(args []interface{}) (filters.Filter, error) { response = false ) - if len(args) == 0 { + if len(args) == 0 || len(args) > 2 { return nil, filters.ErrInvalidFilterParameters } diff --git a/filters/diag/logbody_test.go b/filters/diag/logbody_test.go index f615f45242..66c67c5cb4 100644 --- a/filters/diag/logbody_test.go +++ b/filters/diag/logbody_test.go @@ -107,6 +107,39 @@ func TestLogBody(t *testing.T) { io.Copy(io.Discard, rsp.Body) log.SetOutput(os.Stderr) + got := logbuf.String() + if !strings.Contains(got, requestContent) { + t.Fatalf("Failed to find req %q log, got: %q", requestContent, got) + } + // repeatContent("a", 10) + if !strings.Contains(got, "aaaaaaaaaa") { + t.Fatalf("Failed to find %q log, got: %q", "aaaaaaaaaa", got) + } + }) + t.Run("Request-response chaining", func(t *testing.T) { + beRoutes := eskip.MustParse(`r: * -> repeatContent("a", 10) -> `) + fr := make(filters.Registry) + fr.Register(NewLogBody()) + fr.Register(NewRepeat()) + be := proxytest.New(fr, beRoutes...) + defer be.Close() + + routes := eskip.MustParse(fmt.Sprintf(`r: * -> logBody("request") -> logBody("response") -> "%s"`, be.URL)) + p := proxytest.New(fr, routes...) + defer p.Close() + + requestContent := "testrequestresponsechain" + logbuf := bytes.NewBuffer(nil) + log.SetOutput(logbuf) + buf := bytes.NewBufferString(requestContent) + rsp, err := http.DefaultClient.Post(p.URL, "text/plain", buf) + if err != nil { + t.Fatalf("Failed to get respone: %v", err) + } + defer rsp.Body.Close() + io.Copy(io.Discard, rsp.Body) + log.SetOutput(os.Stderr) + got := logbuf.String() if !strings.Contains(got, requestContent) { t.Fatalf("Failed to find req %q log, got: %q", requestContent, got)