File: fuzz_test.go

package info (click to toggle)
golang-github-graph-gophers-graphql-go 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,452 kB
  • sloc: sh: 373; javascript: 21; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,769 bytes parent folder | download
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package graphql_test

import (
	"context"
	"testing"

	"github.com/graph-gophers/graphql-go"
	"github.com/graph-gophers/graphql-go/example/starwars"
)

func FuzzSchemaExec(f *testing.F) {
	resolver := &starwars.Resolver{}
	opts := []graphql.SchemaOpt{graphql.MaxDepth(3)}
	schema, err := graphql.ParseSchema(starwars.Schema, resolver, opts...)
	if err != nil {
		f.Errorf("ParseSchema: %v", err)
		return
	}

	// Seed the fuzzing corpus with a variety of valid GraphQL queries.
	queries := []string{
		`{ hero { name } }`,
		`{ hero { name appearsIn } }`,
		`{ hero { name appearsIn friends { name } } }`,
		`{ hero(episode: EMPIRE) { name } }`,
		`{ episode(episode: EMPIRE) { title characters { name } reviews { stars commentary } } }`,
		`{ episode(episode: EMPIRE) { title characters { name friends { name } reviews { stars commentary } } }`,
		`query { episode(episode: EMPIRE) { title characters { name friends { name } } } }`,
		`query HeroName { hero { name } }`,
		`query HeroNameAndFriends { hero { name friends { name } } }`,
		`mutation { createReview(episode: EMPIRE, review: { stars: 5, commentary: "Great!" }) }`,
	}
	for _, q := range queries {
		f.Add(q)
	}

	f.Fuzz(func(t *testing.T, query string) {
		// ignore invalid queries in order to test only the execution against the schema
		errs := schema.Validate(query)
		if len(errs) > 0 {
			t.Skip()
		}

		res := schema.Exec(context.Background(), query, "", nil)
		if res.Data != nil && len(res.Errors) > 0 {
			t.Errorf("Exec(%q) returned both data and errors: %v", query, res.Errors)
		}
		if res.Errors != nil {
			t.Logf("Exec(%q) returned errors: %v", query, res.Errors)
		}
		if res.Data == nil && len(res.Errors) == 0 {
			t.Errorf("Exec(%q) returned nil data and no errors", query)
		}
	})
}