mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* Adjust API tests internal API * Refactor API tests to take less time - sqlite: tests 15s, overall time: 1m - mysql: tests 15s, overall time: 59s * Use retry func to survive in faulty places * Use retry func while trying to ping SQL datastore - implements retry func specifically for SQL datastore ping - fmt fixes - using sqlx.Db.PingContext instead of sqlx.Db.Ping - propogate context to SQL datastore * Simplify TestCanCauseTimeout retry loop * Call retry with sane timeout * Fix TestOversizedLog, use retry func * Increase number of attempts 2 test cases are really faulty in CI, so they need a lot more time to finish. * Increase TestCanCauseTimeout timeout * Use retry at TestMultiLog to speed it up * Use retry at TestCanWriteLogs to speed it up * Use retry at TestGetCallsSuccess to speed it up * Use retry at TestCanGetAsyncState to speed it up * Use retry at TestListCallsSuccess to speed it up * Remove sleep calls * Remove dup test case * Cleaup Calls API test * Build API tests binary once This patch lets CI to build API tests binary once and reuse that whenever it needs it * Swap API tests checks * Build API test binary by default dirty fix for CircleCI * Use retry func to determine if datastore is alive in tests * go install should also reduce build time * Fix rebase issues
68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package tests
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/url"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
type JSONResponse struct {
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func TestFnJSONFormats(t *testing.T) {
|
|
t.Parallel()
|
|
s := SetupDefaultSuite()
|
|
|
|
// TODO(treeder): put image in fnproject @ dockerhub
|
|
image := "denismakogon/test-hot-json-go:0.0.1"
|
|
format := "json"
|
|
route := "/test-hot-json-go"
|
|
|
|
CreateApp(t, s.Context, s.Client, s.AppName, map[string]string{})
|
|
CreateRoute(t, s.Context, s.Client, s.AppName, route, image, "sync",
|
|
format, s.Timeout, s.IdleTimeout, s.RouteConfig, s.RouteHeaders)
|
|
|
|
u := url.URL{
|
|
Scheme: "http",
|
|
Host: Host(),
|
|
}
|
|
u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)
|
|
|
|
b, _ := json.Marshal(&struct {
|
|
Name string `json:"name"`
|
|
}{
|
|
Name: "Jimmy",
|
|
})
|
|
content := bytes.NewBuffer(b)
|
|
output := &bytes.Buffer{}
|
|
headers, err := CallFN(u.String(), content, output, "POST", []string{})
|
|
if err != nil {
|
|
t.Errorf("Got unexpected error: %v", err)
|
|
}
|
|
|
|
msg := &JSONResponse{}
|
|
json.Unmarshal(output.Bytes(), msg)
|
|
expectedOutput := "Hello Jimmy"
|
|
if !strings.Contains(expectedOutput, msg.Message) {
|
|
t.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
|
|
}
|
|
|
|
expectedHeaderNames := []string{"Content-Type", "Content-Length"}
|
|
expectedHeaderValues := []string{"application/json; charset=utf-8", strconv.Itoa(output.Len())}
|
|
for i, name := range expectedHeaderNames {
|
|
actual := headers.Get(name)
|
|
expected := expectedHeaderValues[i]
|
|
if !strings.Contains(expected, actual) {
|
|
t.Errorf("HTTP header assertion error for %v."+
|
|
"\n\tExpected: %v\n\tActual: %v", name, expected, actual)
|
|
}
|
|
}
|
|
|
|
DeleteApp(t, s.Context, s.Client, s.AppName)
|
|
}
|