Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented support to return arbitrary content types #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ A huge thanks to all of our contributors:
- Karan Misra
- strandmon
- yuyabe
- Simon Eisenmann
23 changes: 21 additions & 2 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sleepy

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -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)
Expand All @@ -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.")
}

}