Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

44 lines (39 sloc) 1.347 kB
package mockhttp_test
import (
"github.com/marshall/mockhttp"
"net/http"
"strings"
"testing"
)
func hello(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
w.Header().Add("Allow", "GET")
http.Error(w, "Only GET supported.", http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte("Hello, world.\n"))
}
func TestGET(t *testing.T) {
handler := http.HandlerFunc(hello)
req := mockhttp.NewRequest("GET", "http://foo.example.com/bar", nil)
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
respw := mockhttp.NewResponseWriter()
handler.ServeHTTP(respw, req)
want_hdr := make(http.Header)
want_hdr.Add("Content-Type", "text/plain; charset=utf-8")
respw.Check(t, http.StatusOK, want_hdr, "Hello, world.\n")
}
func TestPUT(t *testing.T) {
handler := http.HandlerFunc(hello)
body := strings.NewReader(`foo`)
req := mockhttp.NewRequest("PUT", "http://foo.example.com/bar", body)
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
respw := mockhttp.NewResponseWriter()
handler.ServeHTTP(respw, req)
want_hdr := make(http.Header)
want_hdr.Add("Content-Type", "text/plain; charset=utf-8")
want_hdr.Add("Allow", "GET")
respw.Check(t, http.StatusMethodNotAllowed, want_hdr, "Only GET supported.\n")
}
Jump to Line
Something went wrong with that request. Please try again.