Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Implements Operation RoundTripper (#9)
Browse files Browse the repository at this point in the history
* Adds stretchr/testify

* Implements operation roundtrip

* Fixes go.mod

* Improves http/operation_test.go

* Improves test coverage

* Improves handling when operation is not provided

* Calls Close on copyBody after read
  • Loading branch information
GiancarloJung authored Jun 28, 2022
1 parent 4451327 commit 3e3b0be
Show file tree
Hide file tree
Showing 68 changed files with 22,743 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ require (
github.com/matryer/is v1.4.0
github.com/mitchellh/mapstructure v1.4.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.5
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/mitchellh/mapstructure v1.4.2 h1:6h7AQ0yhTcIsmFmnAwQls75jp2Gzs4iB8W7pjMO+rqo=
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
54 changes: 54 additions & 0 deletions http/operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package http

import (
"io"
"net/http"
"regexp"
)

type addOperation struct {
inner http.RoundTripper
}

const operationRegex = `((?:query\s)|(?:mutation\s))(\w+)`

func SetGraphqlOperation(inner http.RoundTripper) http.RoundTripper {
return &addOperation{
inner: inner,
}
}

func (ug *addOperation) RoundTrip(r *http.Request) (*http.Response, error) {
values := r.URL.Query()
operation := getOperationName(r)

if operation != "" {
values.Add("operation", operation)
r.URL.RawQuery = values.Encode()
}

return ug.inner.RoundTrip(r)
}

func getOperationName(r *http.Request) string {
regex := regexp.MustCompile(operationRegex)
getBody := r.GetBody

copyBody, err := getBody()
if err != nil {
return ""
}

b, err := io.ReadAll(copyBody)
if err != nil {
return ""
}
copyBody.Close()

operation := regex.FindAllSubmatch(b, -1)
if operation != nil {
return string(operation[0][2])
}

return ""
}
101 changes: 101 additions & 0 deletions http/operation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package http

import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

graphql "github.com/sumup/graphql"
)

func Test_SetGraphqlOperation(t *testing.T) {
t.Run("SetGraphqlOperation with query", func(t *testing.T) {
handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.URL.RawQuery, "operation=FooBar")

_, _ = io.WriteString(w, "{}")
})

runner := func(t *testing.T, client *graphql.Client) {
ctx := context.Background()
req := graphql.NewRequest("query FooBar {}")
var resp struct{}

err := client.Run(ctx, req, &resp)
assert.NoError(t, err)
}

runTest(t, handlerFunc, runner)
})

t.Run("SetGraphqlOperation with mutation", func(t *testing.T) {
handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.URL.RawQuery, "operation=FooBar")

_, _ = io.WriteString(w, `{
"data": {
"fooBar": {
"messages": [],
"result": null,
"successful": false
}
}
}`)
})

runner := func(t *testing.T, client *graphql.Client) {
ctx := context.Background()
req := graphql.NewMutation(`
mutation FooBar {
fooBar {
successful
messages
result
}
}
`)
var resp struct{}

err := client.Run(ctx, req, &resp)
assert.NoError(t, err)
}

runTest(t, handlerFunc, runner)
})

t.Run("SetGraphqlOperation when operation name is not provided", func(t *testing.T) {
handlerFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Empty(t, r.URL.RawQuery)

_, _ = io.WriteString(w, "{}")
})

runner := func(t *testing.T, client *graphql.Client) {
ctx := context.Background()
req := graphql.NewRequest("query {}")
var resp struct{}

err := client.Run(ctx, req, &resp)
assert.NoError(t, err)
}

runTest(t, handlerFunc, runner)
})
}

func runTest(t *testing.T, handlerFunc http.HandlerFunc, runner func(*testing.T, *graphql.Client)) {
srv := httptest.NewServer(handlerFunc)
defer srv.Close()

transport := http.DefaultTransport
transport = SetGraphqlOperation(transport)
httpClient := &http.Client{Transport: transport}

client := graphql.NewClient(srv.URL, graphql.WithHTTPClient(httpClient))

runner(t, client)
}
15 changes: 15 additions & 0 deletions vendor/github.com/davecgh/go-spew/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypass.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3e3b0be

Please sign in to comment.