From 03015e48bd84c6cd9d2e229451beb0cefbe9a3d3 Mon Sep 17 00:00:00 2001
From: "Alex Ellis (VMware)" <alexellis2@gmail.com>
Date: Mon, 13 Aug 2018 17:57:44 +0100
Subject: [PATCH] Add test for Http_path for watchdog.

This test takes inspiration from the PR from @telackey with changes
to make it more maintainable. Since the test does not require
changes to the code, I wanted to add it before merging changes.

Ref: https://github.com/openfaas/faas/pull/789

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
---
 watchdog/requesthandler_test.go | 37 +++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/watchdog/requesthandler_test.go b/watchdog/requesthandler_test.go
index 065df130..8fefddb9 100644
--- a/watchdog/requesthandler_test.go
+++ b/watchdog/requesthandler_test.go
@@ -490,6 +490,43 @@ func TestHealthHandler_SatusMethoNotAllowed_ForWriteableVerbs(t *testing.T) {
 	}
 }
 
+func TestHandler_HasFullPathAndQueryInFunction_WithCgi_Mode(t *testing.T) {
+	rr := httptest.NewRecorder()
+
+	body := ""
+	wantPath := "/my/full/path"
+	wantQuery := "q=x"
+	requestURI := wantPath + "?" + wantQuery
+	req, err := http.NewRequest(http.MethodPost, requestURI, bytes.NewBufferString(body))
+
+	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, want: %v",
+			status, required)
+	}
+
+	read, _ := ioutil.ReadAll(rr.Body)
+	val := string(read)
+	if !strings.Contains(val, "Http_Path="+wantPath) {
+		t.Errorf(config.faasProcess+" should print: Http_Path="+wantPath+", got: %s\n", val)
+	}
+
+	if !strings.Contains(val, "Http_Query="+wantQuery) {
+		t.Errorf(config.faasProcess+" should print: Http_Query="+wantQuery+", got: %s\n", val)
+	}
+}
+
 func removeLockFile() error {
 	path := filepath.Join(os.TempDir(), ".lock")
 	log.Printf("Removing lock-file : %s\n", path)
-- 
GitLab