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

Fix missing boundary when MultipartMixed transport aggregates initial and deferred response #3382

Merged
merged 2 commits into from
Nov 22, 2024
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
2 changes: 1 addition & 1 deletion graphql/handler/testserver/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func New() *TestServer {
switch opCtx.Operation.Operation {
case ast.Query:
ran := false
// If the query contains @defer, we will mimick a deferred response.
// If the query contains @defer, we will mimic a deferred response.
if strings.Contains(opCtx.RawQuery, "@defer") {
initialResponse := true
return func(context context.Context) *graphql.Response {
Expand Down
7 changes: 7 additions & 0 deletions graphql/handler/transport/http_multipart_mixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ func (a *multipartResponseAggregator) flush(w http.ResponseWriter) {

writeJson(w, a.initialResponse)
hasNext = a.initialResponse.HasNext != nil && *a.initialResponse.HasNext

// Handle when initial is aggregated with deferred responses.
if len(a.deferResponses) > 0 {
fmt.Fprintf(w, "\r\n")
writeBoundary(w, a.boundary, false)
}

// Reset the initial response so we don't send it again
a.initialResponse = nil
}
Expand Down
67 changes: 66 additions & 1 deletion graphql/handler/transport/http_multipart_mixed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -102,7 +103,7 @@ func TestMultipartMixed(t *testing.T) {
return bs
}

t.Run("initial and incremental patches", func(t *testing.T) {
t.Run("initial and incremental patches un-aggregated", func(t *testing.T) {
handler, srv := initializeWithServer()
defer srv.Close()

Expand Down Expand Up @@ -167,4 +168,68 @@ func TestMultipartMixed(t *testing.T) {

wg.Wait()
})

t.Run("initial and incremental patches aggregated", func(t *testing.T) {
handler := testserver.New()
handler.AddTransport(transport.MultipartMixed{
Boundary: "graphql",
DeliveryTimeout: time.Hour,
})

srv := httptest.NewServer(handler)
defer srv.Close()

var err error
var res *http.Response

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
client := &http.Client{}
req := createHTTPRequest(
srv.URL,
`{"query":"query { ... @defer { name } }"}`,
)
res, err = client.Do(req) //nolint:bodyclose // false positive
}()

handler.SendNextSubscriptionMessage()
handler.SendNextSubscriptionMessage()
handler.SendCompleteSubscriptionMessage()
wg.Wait()

require.NoError(t, err, "Request threw error -> %s", err)
defer func() {
require.NoError(t, res.Body.Close())
}()

assert.Equal(t, 200, res.StatusCode, "Request return wrong status -> %d", res.Status)
assert.Equal(t, "keep-alive", res.Header.Get("Connection"))
assert.Contains(t, res.Header.Get("Content-Type"), "multipart/mixed")
assert.Contains(t, res.Header.Get("Content-Type"), `boundary="graphql"`)

br := bufio.NewReader(res.Body)
assert.Equal(t, "--graphql\r\n", readLine(br))
assert.Equal(t, "Content-Type: application/json\r\n", readLine(br))
assert.Equal(t, "\r\n", readLine(br))
assert.Equal(t,
"{\"data\":{\"name\":null},\"hasNext\":true}\r\n",
readLine(br),
)

assert.Equal(t, "--graphql\r\n", readLine(br))
assert.Equal(t, "Content-Type: application/json\r\n", readLine(br))
assert.Equal(t, "\r\n", readLine(br))
assert.Equal(
t,
"{\"incremental\":[{\"data\":{\"name\":\"test\"},\"hasNext\":false}],\"hasNext\":false}\r\n",
readLine(br),
)

assert.Equal(t, "--graphql--\r\n", readLine(br))

_, err = br.ReadByte()
assert.Equal(t, err, io.EOF)
})
}
Loading