Skip to content

Commit

Permalink
Set content-type header to application/json
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Nikolov authored and neelance committed Jun 27, 2017
1 parent c76ff4d commit 7f3f712
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package relay

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"

"encoding/json"

graphql "github.com/neelance/graphql-go"
)

Expand Down Expand Up @@ -66,5 +65,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

w.Header().Set("Content-Type", "application/json")
w.Write(responseJSON)
}
36 changes: 36 additions & 0 deletions relay/relay_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package relay_test

import (
"net/http/httptest"
"strings"
"testing"

"github.com/neelance/graphql-go"
"github.com/neelance/graphql-go/example/starwars"
"github.com/neelance/graphql-go/relay"
)

var starwarsSchema = graphql.MustParseSchema(starwars.Schema, &starwars.Resolver{})

func TestServeHTTP(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/some/path/here", strings.NewReader(`{"query":"{ hero { name } }", "operationName":"", "variables": null}`))
h := relay.Handler{Schema: starwarsSchema}

h.ServeHTTP(w, r)

if w.Code != 200 {
t.Fatalf("Expected status code 200, got %d.", w.Code)
}

contentType := w.Header().Get("Content-Type")
if contentType != "application/json" {
t.Fatalf("Invalid content-type. Expected [application/json], but instead got [%s]", contentType)
}

expectedResponse := `{"data":{"hero":{"name":"R2-D2"}}}`
actualResponse := string(w.Body.Bytes())
if expectedResponse != actualResponse {
t.Fatalf("Invalid response. Expected [%s], but instead got [%s]", expectedResponse, actualResponse)
}
}

0 comments on commit 7f3f712

Please sign in to comment.