diff options
-rw-r--r-- | core.go | 24 | ||||
-rw-r--r-- | core_test.go | 2 |
2 files changed, 24 insertions, 2 deletions
@@ -13,6 +13,8 @@ const ( POST = "POST" PUT = "PUT" DELETE = "DELETE" + HEAD = "HEAD" + PATCH = "PATCH" ) // GetSupported is the interface that provides the Get @@ -39,6 +41,18 @@ type DeleteSupported interface { Delete(url.Values, http.Header) (int, interface{}, http.Header) } +// HeadSupported is the interface that provides the Head +// method a resource must support to receive HTTP HEADs. +type HeadSupported interface { + Head(url.Values, http.Header) (int, interface{}, http.Header) +} + +// PatchSupported is the interface that provides the Patch +// method a resource must support to receive HTTP PATCHs. +type PatchSupported interface { + Patch(url.Values, http.Header) (int, interface{}, http.Header) +} + // An API manages a group of resources by routing requests // to the correct method on a matching resource and marshalling // the returned data to JSON for the HTTP response. @@ -82,6 +96,14 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { if resource, ok := resource.(DeleteSupported); ok { handler = resource.Delete } + case HEAD: + if resource, ok := resource.(HeadSupported); ok { + handler = resource.Head + } + case PATCH: + if resource, ok := resource.(PatchSupported); ok { + handler = resource.Patch + } } if handler == nil { @@ -91,7 +113,7 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { code, data, header := handler(request.Form, request.Header) - content, err := json.Marshal(data) + content, err := json.MarshalIndent(data, "", " ") if err != nil { rw.WriteHeader(http.StatusInternalServerError) return diff --git a/core_test.go b/core_test.go index 2438b98..8027bfe 100644 --- a/core_test.go +++ b/core_test.go @@ -27,7 +27,7 @@ func TestBasicGet(t *testing.T) { t.Error(err) } body, _ := ioutil.ReadAll(resp.Body) - if string(body) != `{"items":["item1","item2"]}` { + if string(body) != "{\n \"items\": [\n \"item1\",\n \"item2\"\n ]\n}" { t.Error("Not equal.") } } |