Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adjust to use net/http/httptest.

With the addition of httptest into Go core, mockhttp is now just two
utility functions.

This changes the API!
  • Loading branch information...
commit cd11eb1f0b23ffffdbcfd7531ad2b7cd1e3d0325 1 parent aa93208
@tv42 authored
Showing with 11 additions and 35 deletions.
  1. +5 −4 mockhttp_test.go
  2. +6 −31 mockresponsewriter.go
View
9 mockhttp_test.go
@@ -3,6 +3,7 @@ package mockhttp_test
import (
"github.com/tv42/mockhttp.go"
"net/http"
+ "net/http/httptest"
"strings"
"testing"
)
@@ -22,11 +23,11 @@ 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()
+ respw := httptest.NewRecorder()
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")
+ mockhttp.CheckResponse(t, respw, http.StatusOK, want_hdr, "Hello, world.\n")
}
func TestPUT(t *testing.T) {
@@ -34,10 +35,10 @@ func TestPUT(t *testing.T) {
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()
+ respw := httptest.NewRecorder()
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")
+ mockhttp.CheckResponse(t, respw, http.StatusMethodNotAllowed, want_hdr, "Only GET supported.\n")
}
View
37 mockresponsewriter.go
@@ -1,38 +1,20 @@
package mockhttp
import (
- "bytes"
"net/http"
+ "net/http/httptest"
"reflect"
"testing"
)
-type MockResponseWriter struct {
- Headers http.Header
- Body bytes.Buffer
- Status int
-}
-
-func (w *MockResponseWriter) Header() http.Header {
- return w.Headers
-}
-
-func (w *MockResponseWriter) Write(data []byte) (int, error) {
- return w.Body.Write(data)
-}
-
-func (w *MockResponseWriter) WriteHeader(status int) {
- w.Status = status
-}
-
-func (w *MockResponseWriter) Check(t *testing.T, want_status int, want_headers http.Header, want_body string) (ok bool) {
+func CheckResponse(t *testing.T, w *httptest.ResponseRecorder, want_status int, want_headers http.Header, want_body string) (ok bool) {
ok = true
- if w.Status != want_status {
- t.Errorf("Bad HTTP status: got %d want %d", w.Status, want_status)
+ if w.Code != want_status {
+ t.Errorf("Bad HTTP status: got %d want %d", w.Code, want_status)
ok = false
}
- if !reflect.DeepEqual(w.Headers, want_headers) {
- t.Errorf("Bad HTTP response headers: %v", w.Headers)
+ if !reflect.DeepEqual(w.HeaderMap, want_headers) {
+ t.Errorf("Bad HTTP response headers: %v", w.HeaderMap)
ok = false
}
resp_body := w.Body.String()
@@ -42,10 +24,3 @@ func (w *MockResponseWriter) Check(t *testing.T, want_status int, want_headers h
}
return
}
-
-func NewResponseWriter() *MockResponseWriter {
- var w MockResponseWriter
- w.Headers = make(http.Header)
- w.Status = 200
- return &w
-}
Please sign in to comment.
Something went wrong with that request. Please try again.