mirror of
https://github.com/openfaas/faasd.git
synced 2025-06-08 08:05:03 +00:00
* Fixes upstream deprecations for containerd * Fixes deprecations in ioutil package usage * Break out separate files for function handlers * Upgrades containerd to 1.7.22 * Fixes default namespace functionality * Pre-deploy checks as per license agreement * Removes extra log messages for payload in HTTP handlers, you can enable FAAS_DEBUG=1 in the CLI instead to see this from the client's perspective * Add additional Google DNS server 8.8.4.4 for functions Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
87 lines
1.5 KiB
Go
87 lines
1.5 KiB
Go
package pkg
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_Proxy_ToPrivateServer(t *testing.T) {
|
|
|
|
wantBodyText := "OK"
|
|
wantBody := []byte(wantBodyText)
|
|
upstreamSvr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Body != nil {
|
|
defer r.Body.Close()
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write(wantBody)
|
|
|
|
}))
|
|
|
|
defer upstreamSvr.Close()
|
|
port := 8080
|
|
u, _ := url.Parse(upstreamSvr.URL)
|
|
log.Println("Host", u.Host)
|
|
|
|
upstreamAddr := u.Host
|
|
proxy := NewProxy(upstreamAddr, 8080, "127.0.0.1", time.Second*1, &mockResolver{})
|
|
|
|
gwChan := make(chan string, 1)
|
|
doneCh := make(chan bool)
|
|
|
|
go proxy.Start()
|
|
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(1)
|
|
go func() {
|
|
gwChan <- u.Host
|
|
wg.Done()
|
|
}()
|
|
wg.Wait()
|
|
|
|
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d", port), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for i := 1; i < 11; i++ {
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Logf("Try %d, gave error: %s", i, err)
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
} else {
|
|
|
|
resBody, _ := io.ReadAll(res.Body)
|
|
if string(resBody) != string(wantBody) {
|
|
t.Errorf("want %s, but got %s in body", string(wantBody), string(resBody))
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
go func() {
|
|
doneCh <- true
|
|
}()
|
|
}
|
|
|
|
type mockResolver struct {
|
|
}
|
|
|
|
func (m *mockResolver) Start() {
|
|
|
|
}
|
|
|
|
func (m *mockResolver) Get(upstream string, got chan<- string, timeout time.Duration) {
|
|
got <- upstream
|
|
}
|