Full stack tests

This commit is contained in:
Denis Makogon
2017-07-05 12:38:09 -07:00
committed by Reed Allman
parent c85571f51d
commit adf61c77be
28 changed files with 1264 additions and 60 deletions

38
fn/client/api.go Normal file
View File

@@ -0,0 +1,38 @@
package client
import (
"os"
"log"
"net/url"
fnclient "github.com/funcy/functions_go/client"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
)
func Host() string {
apiURL := os.Getenv("API_URL")
if apiURL == "" {
apiURL = "http://localhost:8080"
}
u, err := url.Parse(apiURL)
if err != nil {
log.Fatalln("Couldn't parse API URL:", err)
}
log.Println("trace: Host:", u.Host)
return u.Host
}
func APIClient() *fnclient.Functions {
transport := httptransport.New(Host(), "/v1", []string{"http"})
if os.Getenv("FN_TOKEN") != "" {
transport.DefaultAuthentication = httptransport.BearerToken(os.Getenv("FN_TOKEN"))
}
// create the API client, with the transport
client := fnclient.New(transport, strfmt.Default)
return client
}

53
fn/client/call_fn.go Normal file
View File

@@ -0,0 +1,53 @@
package client
import (
"fmt"
"io"
"net/http"
"os"
"strings"
)
func EnvAsHeader(req *http.Request, selectedEnv []string) {
detectedEnv := os.Environ()
if len(selectedEnv) > 0 {
detectedEnv = selectedEnv
}
for _, e := range detectedEnv {
kv := strings.Split(e, "=")
name := kv[0]
req.Header.Set(name, os.Getenv(name))
}
}
func CallFN(u string, content io.Reader, output io.Writer, method string, env []string) error {
if method == "" {
if content == nil {
method = "GET"
} else {
method = "POST"
}
}
req, err := http.NewRequest(method, u, content)
if err != nil {
return fmt.Errorf("error running route: %s", err)
}
req.Header.Set("Content-Type", "application/json")
if len(env) > 0 {
EnvAsHeader(req, env)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("error running route: %s", err)
}
io.Copy(output, resp.Body)
return nil
}