github.com/99designs/gqlgen@v0.17.45/graphql/handler/transport/options_test.go (about) 1 package transport_test 2 3 import ( 4 "net/http" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/99designs/gqlgen/graphql/handler/testserver" 10 "github.com/99designs/gqlgen/graphql/handler/transport" 11 ) 12 13 func TestOptions(t *testing.T) { 14 t.Run("responds to options requests with default methods", func(t *testing.T) { 15 h := testserver.New() 16 h.AddTransport(transport.Options{}) 17 resp := doRequest(h, "OPTIONS", "/graphql?query={me{name}}", ``, "application/json") 18 assert.Equal(t, http.StatusOK, resp.Code) 19 assert.Equal(t, "OPTIONS, GET, POST", resp.Header().Get("Allow")) 20 }) 21 22 t.Run("responds to options requests with specified methods", func(t *testing.T) { 23 h := testserver.New() 24 h.AddTransport(transport.Options{ 25 AllowedMethods: []string{http.MethodOptions, http.MethodPost, http.MethodHead}, 26 }) 27 resp := doRequest(h, "OPTIONS", "/graphql?query={me{name}}", ``, "application/json") 28 assert.Equal(t, http.StatusOK, resp.Code) 29 assert.Equal(t, "OPTIONS, POST, HEAD", resp.Header().Get("Allow")) 30 }) 31 32 t.Run("responds to head requests", func(t *testing.T) { 33 h := testserver.New() 34 h.AddTransport(transport.Options{}) 35 resp := doRequest(h, "HEAD", "/graphql?query={me{name}}", ``, "application/json") 36 assert.Equal(t, http.StatusMethodNotAllowed, resp.Code) 37 }) 38 }