Add integration test and defer

This commit is contained in:
Alex 2017-01-27 21:07:09 +00:00
parent 1297048799
commit 42c0c02950
2 changed files with 102 additions and 0 deletions

View File

@ -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()

View File

@ -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()
}
}