From ecfbf61e966fe1438465bf175eab44177a391932 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Thu, 1 May 2014 21:14:15 +0200 Subject: [PATCH 1/2] Implemented support to return arbitrary content-types. --- core.go | 23 +++++++++++++++++++++-- core_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/core.go b/core.go index 631d76d..7d643de 100644 --- a/core.go +++ b/core.go @@ -60,7 +60,7 @@ type PatchSupported interface { // You can instantiate multiple APIs on separate ports. Each API // will manage its own set of resources. type API struct { - mux *http.ServeMux + mux *http.ServeMux muxInitialized bool } @@ -113,7 +113,26 @@ func (api *API) requestHandler(resource interface{}) http.HandlerFunc { code, data, header := handler(request.Form, request.Header) - content, err := json.MarshalIndent(data, "", " ") + var content []byte + var err error + + switch data.(type) { + case string: + content = []byte(data.(string)) + case []byte: + content = data.([]byte) + default: + // Encode JSON. + content, err = json.MarshalIndent(data, "", " ") + if err != nil { + if header == nil { + header = http.Header{"Content-Type": {"application/json"}} + } else if header.Get("Content-Type") == "" { + header.Set("Content-Type", "application/json") + } + } + } + if err != nil { rw.WriteHeader(http.StatusInternalServerError) return diff --git a/core_test.go b/core_test.go index 8027bfe..49acc94 100644 --- a/core_test.go +++ b/core_test.go @@ -1,6 +1,7 @@ package sleepy import ( + "fmt" "io/ioutil" "net/http" "net/url" @@ -15,6 +16,11 @@ func (item Item) Get(values url.Values, headers http.Header) (int, interface{}, return 200, data, nil } +func (item Item) Post(values url.Values, headers http.Header) (int, interface{}, http.Header) { + data := fmt.Sprintf("You sent: %s", values.Get("hello")) + return 200, data, http.Header{"Content-Type": {"text/plain"}} +} + func TestBasicGet(t *testing.T) { item := new(Item) @@ -31,3 +37,24 @@ func TestBasicGet(t *testing.T) { t.Error("Not equal.") } } + +func TestBasicPostWithTextPlainResponse(t *testing.T) { + + item := new(Item) + + var api = NewAPI() + api.AddResource(item, "/items", "/bar", "/baz") + go api.Start(3000) + resp, err := http.PostForm("http://localhost:3000/items", url.Values{"hello": {"sleepy"}}) + if err != nil { + t.Error(err) + } + if resp.Header.Get("Content-Type") != "text/plain" { + t.Error("Content-Type wrong.") + } + body, _ := ioutil.ReadAll(resp.Body) + if string(body) != "You sent: sleepy" { + t.Error("Not equal.") + } + +} From 5cd14a4d51e55800a7e1ea08e4da27c6c647ec25 Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Thu, 1 May 2014 21:18:10 +0200 Subject: [PATCH 2/2] Added myself to authors. --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index e54f1e5..98736e2 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -10,3 +10,4 @@ A huge thanks to all of our contributors: - Karan Misra - strandmon - yuyabe +- Simon Eisenmann \ No newline at end of file