middleware/ping_test.go

49 lines
1.1 KiB
Go
Raw Permalink Normal View History

2020-11-07 11:59:33 +00:00
package middleware
import (
2023-12-25 00:47:12 +00:00
"io"
2020-11-07 11:59:33 +00:00
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPing(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("resp"))
require.NoError(t, err)
})
server := httptest.NewServer(Ping(handler))
defer server.Close()
res, err := http.Get(server.URL)
require.Nil(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
2023-12-25 00:47:12 +00:00
b, err := io.ReadAll(res.Body)
2020-11-07 11:59:33 +00:00
assert.NoError(t, err)
assert.Equal(t, "resp", string(b))
res, err = http.Get(server.URL + "/ping")
require.Nil(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
2023-12-25 00:47:12 +00:00
b, err = io.ReadAll(res.Body)
2020-11-07 11:59:33 +00:00
assert.NoError(t, err)
assert.Equal(t, "pong", string(b))
res, err = http.Get(server.URL + "/a/b/c/ping")
require.Nil(t, err)
defer res.Body.Close()
assert.Equal(t, http.StatusOK, res.StatusCode)
2023-12-25 00:47:12 +00:00
b, err = io.ReadAll(res.Body)
2020-11-07 11:59:33 +00:00
assert.NoError(t, err)
assert.Equal(t, "pong", string(b))
}