mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 16:56:47 +00:00
Add integration test and defer
This commit is contained in:
parent
1297048799
commit
42c0c02950
@ -35,12 +35,18 @@ func MakeProxy(metrics metrics.MetricOptions, wildcard bool, c *client.Client, l
|
|||||||
name := vars["name"]
|
name := vars["name"]
|
||||||
fmt.Println("invoke by name")
|
fmt.Println("invoke by name")
|
||||||
lookupInvoke(w, r, metrics, name, c, logger)
|
lookupInvoke(w, r, metrics, name, c, logger)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
} else if len(header) > 0 {
|
} else if len(header) > 0 {
|
||||||
lookupInvoke(w, r, metrics, header[0], c, logger)
|
lookupInvoke(w, r, metrics, header[0], c, logger)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
requestBody, _ := ioutil.ReadAll(r.Body)
|
requestBody, _ := ioutil.ReadAll(r.Body)
|
||||||
|
defer r.Body.Close()
|
||||||
alexaService := IsAlexa(requestBody)
|
alexaService := IsAlexa(requestBody)
|
||||||
fmt.Println(alexaService)
|
fmt.Println(alexaService)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
if len(alexaService.Session.SessionId) > 0 &&
|
if len(alexaService.Session.SessionId) > 0 &&
|
||||||
len(alexaService.Session.Application.ApplicationId) > 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)
|
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)
|
invokeService(w, r, metrics, alexaService.Request.Intent.Name, requestBody, logger)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("Provide an x-function header or a valid Alexa SDK request."))
|
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.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte("Error resolving service."))
|
w.Write([]byte("Error resolving service."))
|
||||||
|
defer r.Body.Close()
|
||||||
}
|
}
|
||||||
if exists == true {
|
if exists == true {
|
||||||
requestBody, _ := ioutil.ReadAll(r.Body)
|
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.WriteHeader(http.StatusOK)
|
||||||
w.Write(responseBody)
|
w.Write(responseBody)
|
||||||
|
|
||||||
seconds := time.Since(start).Seconds()
|
seconds := time.Since(start).Seconds()
|
||||||
fmt.Printf("[%s] took %f seconds\n", stamp, seconds)
|
fmt.Printf("[%s] took %f seconds\n", stamp, seconds)
|
||||||
metrics.GatewayServerlessServedTotal.Inc()
|
metrics.GatewayServerlessServedTotal.Inc()
|
||||||
|
92
gateway/tests/integration/routes_test.go
Normal file
92
gateway/tests/integration/routes_test.go
Normal 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()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user