Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
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
7 changes: 4 additions & 3 deletions examples/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (h *ExampleHandler) createBlog(w http.ResponseWriter, r *http.Request) {

// ...do stuff with your blog...

w.WriteHeader(http.StatusCreated)
w.Header().Set(headerContentType, jsonapi.MediaType)
w.WriteHeader(http.StatusCreated)

if err := jsonapiRuntime.MarshalPayload(w, blog); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -68,8 +68,9 @@ func (h *ExampleHandler) echoBlogs(w http.ResponseWriter, r *http.Request) {
// but, for now
blogs := fixtureBlogsList()

w.WriteHeader(http.StatusOK)
w.Header().Set(headerContentType, jsonapi.MediaType)
w.WriteHeader(http.StatusOK)

if err := jsonapiRuntime.MarshalPayload(w, blogs); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand All @@ -90,9 +91,9 @@ func (h *ExampleHandler) showBlog(w http.ResponseWriter, r *http.Request) {

// but, for now
blog := fixtureBlogCreate(intID)
w.Header().Set(headerContentType, jsonapi.MediaType)
w.WriteHeader(http.StatusOK)

w.Header().Set(headerContentType, jsonapi.MediaType)
if err := jsonapiRuntime.MarshalPayload(w, blog); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down
16 changes: 16 additions & 0 deletions examples/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func TestExampleHandler_post(t *testing.T) {
if e, a := http.StatusCreated, rr.Code; e != a {
t.Fatalf("Expected a status of %d, got %d", e, a)
}

if e, a := jsonapi.MediaType, rr.Result().Header.Get(headerContentType); e != a {
t.Fatalf("Expected a Content-Type of %s, got %s", e, a)
}
}

func TestExampleHandler_put(t *testing.T) {
Expand All @@ -51,6 +55,10 @@ func TestExampleHandler_put(t *testing.T) {
if e, a := http.StatusOK, rr.Code; e != a {
t.Fatalf("Expected a status of %d, got %d", e, a)
}

if e, a := jsonapi.MediaType, rr.Result().Header.Get(headerContentType); e != a {
t.Fatalf("Expected a Content-Type of %s, got %s", e, a)
}
}

func TestExampleHandler_get_show(t *testing.T) {
Expand All @@ -67,6 +75,10 @@ func TestExampleHandler_get_show(t *testing.T) {
if e, a := http.StatusOK, rr.Code; e != a {
t.Fatalf("Expected a status of %d, got %d", e, a)
}

if e, a := jsonapi.MediaType, rr.Result().Header.Get(headerContentType); e != a {
t.Fatalf("Expected a Content-Type of %s, got %s", e, a)
}
}

func TestExampleHandler_get_list(t *testing.T) {
Expand All @@ -83,6 +95,10 @@ func TestExampleHandler_get_list(t *testing.T) {
if e, a := http.StatusOK, rr.Code; e != a {
t.Fatalf("Expected a status of %d, got %d", e, a)
}

if e, a := jsonapi.MediaType, rr.Result().Header.Get(headerContentType); e != a {
t.Fatalf("Expected a Content-Type of %s, got %s", e, a)
}
}

func TestHttpErrorWhenHeaderDoesNotMatch(t *testing.T) {
Expand Down