Add Http_ContentLength for deterministic applications

Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis 2017-10-27 15:21:20 +01:00
parent 4889c779b7
commit 33e794abde
2 changed files with 44 additions and 0 deletions

View File

@ -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_Method=%s", method))
envs = append(envs, fmt.Sprintf("Http_ContentLength=%d", r.ContentLength))
if config.writeDebug { if config.writeDebug {
log.Println("Query ", r.URL.RawQuery) log.Println("Query ", r.URL.RawQuery)

View File

@ -5,6 +5,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -28,6 +29,7 @@ func TestHandler_HasCustomHeaderInFunction_WithCgi_Mode(t *testing.T) {
body := "" body := ""
req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body))
req.Header.Add("custom-header", "value") req.Header.Add("custom-header", "value")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -47,9 +49,50 @@ func TestHandler_HasCustomHeaderInFunction_WithCgi_Mode(t *testing.T) {
read, _ := ioutil.ReadAll(rr.Body) read, _ := ioutil.ReadAll(rr.Body)
val := string(read) 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") { if !strings.Contains(val, "Http_Custom_Header") {
t.Errorf("'env' should printed: Http_Custom_Header, got: %s\n", val) 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") seconds := rr.Header().Get("X-Duration-Seconds")