From 33e794abdefbf4dcf85ea5768ab9bab3b51b9c2a Mon Sep 17 00:00:00 2001 From: Alex Ellis Date: Fri, 27 Oct 2017 15:21:20 +0100 Subject: [PATCH] Add Http_ContentLength for deterministic applications Signed-off-by: Alex Ellis --- watchdog/main.go | 1 + watchdog/requesthandler_test.go | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/watchdog/main.go b/watchdog/main.go index c26cef30..53f19216 100644 --- a/watchdog/main.go +++ b/watchdog/main.go @@ -202,6 +202,7 @@ func getAdditionalEnvs(config *WatchdogConfig, r *http.Request, method string) [ } envs = append(envs, fmt.Sprintf("Http_Method=%s", method)) + envs = append(envs, fmt.Sprintf("Http_ContentLength=%d", r.ContentLength)) if config.writeDebug { log.Println("Query ", r.URL.RawQuery) diff --git a/watchdog/requesthandler_test.go b/watchdog/requesthandler_test.go index 03875a8e..afe4be04 100644 --- a/watchdog/requesthandler_test.go +++ b/watchdog/requesthandler_test.go @@ -5,6 +5,7 @@ package main import ( "bytes" + "fmt" "io/ioutil" "net/http" "net/http/httptest" @@ -28,6 +29,7 @@ func TestHandler_HasCustomHeaderInFunction_WithCgi_Mode(t *testing.T) { body := "" req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) req.Header.Add("custom-header", "value") + if err != nil { t.Fatal(err) } @@ -47,9 +49,50 @@ func TestHandler_HasCustomHeaderInFunction_WithCgi_Mode(t *testing.T) { read, _ := ioutil.ReadAll(rr.Body) val := string(read) + if !strings.Contains(val, "Http_ContentLength=0") { + t.Errorf("'env' should printed: Http_ContentLength=0, got: %s\n", val) + } if !strings.Contains(val, "Http_Custom_Header") { t.Errorf("'env' should printed: Http_Custom_Header, got: %s\n", val) + } + seconds := rr.Header().Get("X-Duration-Seconds") + if len(seconds) == 0 { + t.Errorf("Exec of cat should have given a duration as an X-Duration-Seconds header\n") + } +} + +func TestHandler_HasCustomHeaderInFunction_WithCgiMode_AndBody(t *testing.T) { + rr := httptest.NewRecorder() + + body := "test" + req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) + req.Header.Add("custom-header", "value") + + if err != nil { + t.Fatal(err) + } + + config := WatchdogConfig{ + faasProcess: "env", + cgiHeaders: true, + } + handler := makeRequestHandler(&config) + handler(rr, req) + + required := http.StatusOK + if status := rr.Code; status != required { + t.Errorf("handler returned wrong status code: got %v, but wanted %v", + status, required) + } + + read, _ := ioutil.ReadAll(rr.Body) + val := string(read) + if !strings.Contains(val, fmt.Sprintf("Http_ContentLength=%d", len(body))) { + t.Errorf("'env' should printed: Http_ContentLength=0, got: %s\n", val) + } + if !strings.Contains(val, "Http_Custom_Header") { + t.Errorf("'env' should printed: Http_Custom_Header, got: %s\n", val) } seconds := rr.Header().Get("X-Duration-Seconds")