mirror of
https://github.com/openfaas/faas.git
synced 2025-06-19 04:26:35 +00:00
Test makehandler
This commit is contained in:
@ -6,5 +6,5 @@ before_install:
|
|||||||
- docker pull golang:1.7.3
|
- docker pull golang:1.7.3
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- ./build.sh
|
- sh build.sh
|
||||||
|
|
||||||
|
2
build.sh
2
build.sh
@ -2,5 +2,3 @@
|
|||||||
|
|
||||||
(cd gateway && ./build.sh)
|
(cd gateway && ./build.sh)
|
||||||
(cd watchdog && ./build.sh)
|
(cd watchdog && ./build.sh)
|
||||||
|
|
||||||
|
|
||||||
|
@ -45,12 +45,14 @@ func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.
|
|||||||
w.Write(response.Bytes())
|
w.Write(response.Bytes())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(200)
|
|
||||||
|
|
||||||
if config.writeDebug == true {
|
if config.writeDebug == true {
|
||||||
os.Stdout.Write(out)
|
os.Stdout.Write(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(200)
|
||||||
w.Write(out)
|
w.Write(out)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
65
watchdog/requesthandler_test.go
Normal file
65
watchdog/requesthandler_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user