Skip to content

Instantly share code, notes, and snippets.

@markbates
Last active October 23, 2018 08:45
Show Gist options
  • Save markbates/f6416a977a161ce40ad489c5b6d2dab6 to your computer and use it in GitHub Desktop.
Save markbates/f6416a977a161ce40ad489c5b6d2dab6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
)
func HelloHandler(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(200)
name := req.URL.Query().Get("name")
if name == "" {
name = "World"
}
fmt.Fprintf(res, "Hello, %s!", name)
}
func FormHandler(res http.ResponseWriter, req *http.Request) {
err := req.ParseForm()
if err != nil {
res.WriteHeader(500)
return
}
name := req.Form.Get("name")
if name == "" {
name = "World"
}
fmt.Fprintf(res, "Posted Hello, %s!", name)
}
type Responder struct {
http.ResponseWriter
status int
}
func (r *Responder) WriteHeader(i int) {
r.status = i
r.ResponseWriter.WriteHeader(i)
}
func App() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/hello", HelloHandler) // GET
mux.HandleFunc("/form", FormHandler) // POST
h := func(res http.ResponseWriter, req *http.Request) {
rs := &Responder{ResponseWriter: res, status: 200}
mux.ServeHTTP(rs, req)
if rs.status == 500 {
rs.Write([]byte("Oops!"))
}
}
return http.HandlerFunc(h)
}
func main() {
http.ListenAndServe(":3000", App())
}
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_I_HelloHandler(t *testing.T) {
r := require.New(t)
ts := httptest.NewServer(App())
defer ts.Close()
res, err := http.Get(ts.URL + "/hello")
r.NoError(err)
r.Equal(200, res.StatusCode)
b, err := ioutil.ReadAll(res.Body)
r.NoError(err)
r.Equal("Hello, World!", string(b))
}
func Test_I_HelloHandler_WithQuery(t *testing.T) {
r := require.New(t)
ts := httptest.NewServer(App())
defer ts.Close()
res, err := http.Get(ts.URL + "/hello?name=Ringo")
r.NoError(err)
r.Equal(200, res.StatusCode)
b, err := ioutil.ReadAll(res.Body)
r.NoError(err)
r.Equal("Hello, Ringo!", string(b))
}
func Test_I_FormHandler(t *testing.T) {
r := require.New(t)
ts := httptest.NewServer(App())
defer ts.Close()
form := url.Values{}
form.Add("name", "Ringo")
res, err := http.PostForm(ts.URL+"/form", form)
r.NoError(err)
r.Equal(200, res.StatusCode)
b, err := ioutil.ReadAll(res.Body)
r.NoError(err)
r.Equal("Posted Hello, Ringo!", string(b))
}
package main
import (
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_U_HelloHandler(t *testing.T) {
r := require.New(t)
req := httptest.NewRequest("GET", "/hello", nil)
res := httptest.NewRecorder()
HelloHandler(res, req)
r.Equal(200, res.Code)
r.Equal("Hello, World!", res.Body.String())
}
func Test_U_HelloHandler_WithQuery(t *testing.T) {
r := require.New(t)
req := httptest.NewRequest("GET", "/hello?name=Ringo", nil)
res := httptest.NewRecorder()
HelloHandler(res, req)
r.Equal(200, res.Code)
r.Equal("Hello, Ringo!", res.Body.String())
}
func Test_U_FormHandler(t *testing.T) {
r := require.New(t)
form := url.Values{}
form.Add("name", "Ringo")
req := httptest.NewRequest("POST", "/form", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res := httptest.NewRecorder()
FormHandler(res, req)
r.Equal(200, res.Code)
r.Equal("Posted Hello, Ringo!", res.Body.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment