add ping test

This commit is contained in:
Umputun 2021-04-17 23:32:54 -05:00
parent f246618622
commit 1695532aa5

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
@ -74,3 +75,37 @@ func TestHttp_healthHandler(t *testing.T) {
assert.Equal(t, 2, len(res["errors"].([]interface{})))
assert.Contains(t, res["errors"].([]interface{})[0], "400 Bad Request")
}
func TestHttp_pingHandler(t *testing.T) {
port := rand.Intn(10000) + 40000
h := Http{Timeouts: Timeouts{ResponseHeader: 200 * time.Millisecond}, Address: fmt.Sprintf("127.0.0.1:%d", port)}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
svc := discovery.NewService([]discovery.Provider{
&provider.Static{Rules: []string{
"localhost,^/xyz/(.*),/123/$1,/xxx/ping",
},
}}, time.Millisecond*10)
go func() {
_ = svc.Run(context.Background())
}()
h.Matcher = svc
go func() {
_ = h.Run(ctx)
}()
time.Sleep(20 * time.Millisecond)
client := http.Client{}
req, err := http.NewRequest("GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/ping", nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
b, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "pong", string(b))
}