1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
package relay_test
import (
"net/http/httptest"
"strings"
"testing"
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/example/starwars"
"github.com/graph-gophers/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 := w.Body.String()
if expectedResponse != actualResponse {
t.Fatalf("Invalid response. Expected [%s], but instead got [%s]", expectedResponse, actualResponse)
}
}
|