Created
November 28, 2016 16:37
-
-
Save kaneshin/c1d30a55a11e17cf8655a72a8a6d1725 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
func TestHandler(t *testing.T) { | |
t.Run("pingHandler", func(t *testing.T) { | |
t.Parallel() | |
s := httptest.NewServer(http.HandlerFunc(pingHandler())) | |
defer s.Close() | |
res, err := http.Get(s.URL) | |
assert.NoError(t, err) | |
assert.Equal(t, "text/plain", res.Header.Get("Content-Type")) | |
assert.Equal(t, 200, res.StatusCode) | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
assert.NoError(t, err) | |
assert.Equal(t, "pong", string(body)) | |
}) | |
t.Run("echoHandler", func(t *testing.T) { | |
candidates := []struct { | |
query string | |
expected string | |
}{ | |
{"", ""}, | |
{"foo=bar", ""}, | |
{"msg=foo", "foo"}, | |
} | |
for _, c := range candidates { | |
c := c | |
t.Run(c.query, func(t *testing.T) { | |
t.Parallel() | |
s := httptest.NewServer(http.HandlerFunc(echoHandler())) | |
defer s.Close() | |
res, err := http.Get(fmt.Sprintf("%v?%v", s.URL, c.query)) | |
assert.NoError(t, err) | |
assert.Equal(t, "text/plain", res.Header.Get("Content-Type")) | |
assert.Equal(t, 200, res.StatusCode) | |
defer res.Body.Close() | |
body, err := ioutil.ReadAll(res.Body) | |
assert.NoError(t, err) | |
assert.Equal(t, c.expected, string(body)) | |
}) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment