From d031e3154ee9f76a20a9c3c909a52f5eae83615c Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 24 Jan 2017 20:54:24 +0000 Subject: [PATCH] Test makehandler --- .travis.yml | 2 +- build.sh | 2 - watchdog/main.go | 6 ++- watchdog/requesthandler_test.go | 65 +++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 watchdog/requesthandler_test.go diff --git a/.travis.yml b/.travis.yml index 0faacbc9..23e6904a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,5 +6,5 @@ before_install: - docker pull golang:1.7.3 script: - - ./build.sh + - sh build.sh diff --git a/build.sh b/build.sh index 2b3e6528..6f468ccf 100755 --- a/build.sh +++ b/build.sh @@ -2,5 +2,3 @@ (cd gateway && ./build.sh) (cd watchdog && ./build.sh) - - diff --git a/watchdog/main.go b/watchdog/main.go index d608a048..07010401 100644 --- a/watchdog/main.go +++ b/watchdog/main.go @@ -45,12 +45,14 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http. w.Write(response.Bytes()) return } - w.WriteHeader(200) - if config.writeDebug == true { os.Stdout.Write(out) } + + w.WriteHeader(200) w.Write(out) + } else { + w.WriteHeader(http.StatusMethodNotAllowed) } } } diff --git a/watchdog/requesthandler_test.go b/watchdog/requesthandler_test.go new file mode 100644 index 00000000..ad72a10c --- /dev/null +++ b/watchdog/requesthandler_test.go @@ -0,0 +1,65 @@ +package main + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/http/httptest" + "testing" +) + +func TestHandler_make(t *testing.T) { + config := WatchdogConfig{} + handler := makeRequestHandler(&config) + + if handler == nil { + t.Fail() + } +} + +func TestHandler_StatusOKAllowed_ForPOST(t *testing.T) { + rr := httptest.NewRecorder() + + body := "hello" + req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) + if err != nil { + t.Fatal(err) + } + + config := WatchdogConfig{ + faasProcess: "cat", + } + 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) + } + + buf, _ := ioutil.ReadAll(rr.Body) + val := string(buf) + if val != body { + t.Errorf("Exec of cat did not return input value, %s", val) + } +} + +func TestHandler_StatusMethodNotAllowed_ForGet(t *testing.T) { + rr := httptest.NewRecorder() + + req, err := http.NewRequest("GET", "/", nil) + if err != nil { + t.Fatal(err) + } + + config := WatchdogConfig{} + handler := makeRequestHandler(&config) + handler(rr, req) + + required := http.StatusMethodNotAllowed + if status := rr.Code; status != required { + t.Errorf("handler returned wrong status code: got %v, but wanted %v", + status, required) + } +}