From 42c0c02950805448d769e41b2c0e8e0365c3ec2d Mon Sep 17 00:00:00 2001 From: Alex <alexellis2@gmail.com> Date: Fri, 27 Jan 2017 21:07:09 +0000 Subject: [PATCH] Add integration test and defer --- gateway/handlers/proxy.go | 10 +++ gateway/tests/integration/routes_test.go | 92 ++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 gateway/tests/integration/routes_test.go diff --git a/gateway/handlers/proxy.go b/gateway/handlers/proxy.go index fecb7c2c..eb2d5de5 100644 --- a/gateway/handlers/proxy.go +++ b/gateway/handlers/proxy.go @@ -35,12 +35,18 @@ func MakeProxy(metrics metrics.MetricOptions, wildcard bool, c *client.Client, l name := vars["name"] fmt.Println("invoke by name") lookupInvoke(w, r, metrics, name, c, logger) + defer r.Body.Close() + } else if len(header) > 0 { lookupInvoke(w, r, metrics, header[0], c, logger) + defer r.Body.Close() + } else { requestBody, _ := ioutil.ReadAll(r.Body) + defer r.Body.Close() alexaService := IsAlexa(requestBody) fmt.Println(alexaService) + defer r.Body.Close() if len(alexaService.Session.SessionId) > 0 && len(alexaService.Session.Application.ApplicationId) > 0 && @@ -50,9 +56,11 @@ func MakeProxy(metrics metrics.MetricOptions, wildcard bool, c *client.Client, l fmt.Printf("SessionId=%s, Intent=%s, AppId=%s\n", alexaService.Session.SessionId, alexaService.Request.Intent.Name, alexaService.Session.Application.ApplicationId) invokeService(w, r, metrics, alexaService.Request.Intent.Name, requestBody, logger) + } else { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("Provide an x-function header or a valid Alexa SDK request.")) + defer r.Body.Close() } } } @@ -79,6 +87,7 @@ func lookupInvoke(w http.ResponseWriter, r *http.Request, metrics metrics.Metric } w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("Error resolving service.")) + defer r.Body.Close() } if exists == true { requestBody, _ := ioutil.ReadAll(r.Body) @@ -125,6 +134,7 @@ func invokeService(w http.ResponseWriter, r *http.Request, metrics metrics.Metri w.WriteHeader(http.StatusOK) w.Write(responseBody) + seconds := time.Since(start).Seconds() fmt.Printf("[%s] took %f seconds\n", stamp, seconds) metrics.GatewayServerlessServedTotal.Inc() diff --git a/gateway/tests/integration/routes_test.go b/gateway/tests/integration/routes_test.go new file mode 100644 index 00000000..a3d68f00 --- /dev/null +++ b/gateway/tests/integration/routes_test.go @@ -0,0 +1,92 @@ +package inttests + +import ( + "bytes" + "io/ioutil" + "log" + "net/http" + "testing" + "time" +) + +// Before running these tests do a Docker stack deploy. + +func fireRequest(url string, method string, reqBody string) (string, int, error) { + return fireRequestWithHeader(url, method, reqBody, "") +} + +func fireRequestWithHeader(url string, method string, reqBody string, xheader string) (string, int, error) { + httpClient := http.Client{ + Timeout: time.Second * 2, // Maximum of 2 secs + } + + req, err := http.NewRequest(method, url, bytes.NewBufferString(reqBody)) + if err != nil { + log.Fatal(err) + } + + req.Header.Set("User-Agent", "spacecount-tutorial") + if len(xheader) != 0 { + req.Header.Set("X-Function", xheader) + } + res, getErr := httpClient.Do(req) + if getErr != nil { + log.Fatal(getErr) + } + + body, readErr := ioutil.ReadAll(res.Body) + defer req.Body.Close() + if readErr != nil { + log.Fatal(readErr) + } + return string(body), res.StatusCode, readErr +} + +func Test_Get_Rejected(t *testing.T) { + var reqBody string + _, code, err := fireRequest("http://localhost:8080/function/func_echoit", http.MethodGet, reqBody) + if code != http.StatusInternalServerError { + t.Log("Failed") + } + + if err != nil { + t.Log(err) + t.Fail() + } + +} + +func Test_EchoIt_Post_Route_Handler(t *testing.T) { + reqBody := "test message" + body, code, err := fireRequest("http://localhost:8080/function/func_echoit", http.MethodPost, reqBody) + + if err != nil { + t.Log(err) + t.Fail() + } + if code != http.StatusOK { + t.Log("Failed") + } + if body != reqBody { + t.Log("Expected body returned") + t.Fail() + } +} + +func Test_EchoIt_Post_Header_Handler(t *testing.T) { + reqBody := "test message" + body, code, err := fireRequestWithHeader("http://localhost:8080/", http.MethodPost, reqBody, "func_echoit") + + if err != nil { + t.Log(err) + t.Fail() + } + if code != http.StatusOK { + t.Log("Failed") + } + if body != reqBody { + t.Log("Expected body returned") + t.Fail() + } + +} -- GitLab