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

graphql: improve test coverage #112

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import (
"github.com/shurcooL/graphql"
)

func TestClient_NewClient_nilHTTPClient(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
mustWrite(w, `{"data": {"user": {"name": "Gopher"}}}`)
}))

client := graphql.NewClient(srv.URL, nil)

var q struct {
User struct {
Name string
}
}
err := client.Query(context.Background(), &q, nil)
if err != nil {
t.Fatal(err)
}
if got, want := q.User.Name, "Gopher"; got != want {
t.Errorf("got q.User.Name: %q, want: %q", got, want)
}
}

func TestClient_Query_partialDataWithErrorResponse(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -152,6 +174,40 @@ func TestClient_Query_emptyVariables(t *testing.T) {
}
}

func TestClient_Mutate_withVariables(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/graphql", func(w http.ResponseWriter, req *http.Request) {
body := mustRead(req.Body)
if got, want := body, `{"query":"mutation($ep:ID!$review:!){createReview(episode: $ep, review: $review){stars}}","variables":{"ep":"JEDI","review":{"Stars":5}}}`+"\n"; got != want {
t.Errorf("got body: %v, want %v", got, want)
}
w.Header().Set("Content-Type", "application/json")
mustWrite(w, `{"data": {"createReview": {"stars": 5}}}`)
})
client := graphql.NewClient("/graphql", &http.Client{Transport: localRoundTripper{handler: mux}})

var q struct {
CreateReview struct {
Stars graphql.Int
} `graphql:"createReview(episode: $ep, review: $review)"`
}
variables := map[string]any{
"ep": "JEDI",
"review": struct {
Stars graphql.Int
}{
Stars: graphql.Int(5),
},
}
err := client.Mutate(context.Background(), &q, variables)
if err != nil {
t.Fatal(err)
}
if got, want := q.CreateReview.Stars, 5; int(got) != want {
t.Errorf("got q.CreateReview.Stars: %d, want: %d", got, want)
}
}

// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using handler directly, instead of going over an HTTP connection.
type localRoundTripper struct {
Expand Down