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

Add AllowedMethods field to transport.Options #2080

Merged
merged 2 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions graphql/handler/transport/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package transport

import (
"net/http"
"strings"

"github.com/99designs/gqlgen/graphql"
)

// Options responds to http OPTIONS and HEAD requests
type Options struct{}
type Options struct {
// AllowedMethods is a list of allowed HTTP methods.
AllowedMethods []string
}

var _ graphql.Transport = Options{}

Expand All @@ -18,9 +22,16 @@ func (o Options) Supports(r *http.Request) bool {
func (o Options) Do(w http.ResponseWriter, r *http.Request, exec graphql.GraphExecutor) {
switch r.Method {
case http.MethodOptions:
w.Header().Set("Allow", "OPTIONS, GET, POST")
w.Header().Set("Allow", o.allowedMethods())
w.WriteHeader(http.StatusOK)
case http.MethodHead:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

func (o Options) allowedMethods() string {
if len(o.AllowedMethods) == 0 {
return "OPTIONS, GET, POST"
}
return strings.Join(o.AllowedMethods, ", ")
}
22 changes: 17 additions & 5 deletions graphql/handler/transport/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"

"github.com/99designs/gqlgen/graphql/handler/testserver"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/stretchr/testify/assert"
)

func TestOptions(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.Options{})

t.Run("responds to options requests", func(t *testing.T) {
t.Run("responds to options requests with default methods", func(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.Options{})
resp := doRequest(h, "OPTIONS", "/graphql?query={me{name}}", ``)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "OPTIONS, GET, POST", resp.Header().Get("Allow"))
})

t.Run("responds to options requests with specified methods", func(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.Options{
AllowedMethods: []string{http.MethodOptions, http.MethodPost, http.MethodHead},
})
resp := doRequest(h, "OPTIONS", "/graphql?query={me{name}}", ``)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "OPTIONS, POST, HEAD", resp.Header().Get("Allow"))
})

t.Run("responds to head requests", func(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.Options{})
resp := doRequest(h, "HEAD", "/graphql?query={me{name}}", ``)
assert.Equal(t, http.StatusMethodNotAllowed, resp.Code)
})
Expand Down