mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Full stack tests
This commit is contained in:
committed by
Reed Allman
parent
c85571f51d
commit
adf61c77be
53
fn/client/call_fn.go
Normal file
53
fn/client/call_fn.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user