Created
November 28, 2016 16:28
-
-
Save kaneshin/aadf49c5d91043e7b65c458e1fd0e557 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) { | |
t.Parallel() | |
s := httptest.NewServer(http.HandlerFunc(echoHandler())) | |
defer s.Close() | |
candidates := []struct { | |
query string | |
expected string | |
}{ | |
{"", ""}, | |
{"foo=bar", ""}, | |
{"msg=foo", "foo"}, | |
} | |
for _, c := range candidates { | |
c := c | |
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