49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package middleware
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"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)
|
|
|
|
b, err := ioutil.ReadAll(res.Body)
|
|
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)
|
|
|
|
b, err = ioutil.ReadAll(res.Body)
|
|
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)
|
|
|
|
b, err = ioutil.ReadAll(res.Body)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "pong", string(b))
|
|
}
|