Skip to content

Commit

Permalink
enable testifylint.encoded-compare and fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Dec 13, 2024
1 parent 1f1acd0 commit 10b0e05
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 92 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ linters-settings:
- bool-compare
- compares
- empty
- encoded-compare
- error-is-as
- error-nil
- expected-actual
Expand Down
6 changes: 3 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestClient(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if assert.NoError(t, err) {
assert.Equal(t, `{"query":"user(id:$id){name}","variables":{"id":1}}`, string(b))
assert.JSONEq(t, `{"query":"user(id:$id){name}","variables":{"id":1}}`, string(b))

err = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestAddExtensions(t *testing.T) {
if !assert.NoError(t, err) {
return
}
assert.Equal(t, `{"query":"user(id:1){name}","extensions":{"persistedQuery":{"sha256Hash":"ceec2897e2da519612279e63f24658c3e91194cbb2974744fa9007a7e1e9f9e7","version":1}}}`, string(b))
assert.JSONEq(t, `{"query":"user(id:1){name}","extensions":{"persistedQuery":{"sha256Hash":"ceec2897e2da519612279e63f24658c3e91194cbb2974744fa9007a7e1e9f9e7","version":1}}}`, string(b))
err = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
"Name": "Bob",
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestClientWithCustomTarget(t *testing.T) {
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if assert.NoError(t, err) {
assert.Equal(t, `{"query":"user(id:$id){name}","variables":{"id":1}}`, string(b))
assert.JSONEq(t, `{"query":"user(id:$id){name}","variables":{"id":1}}`, string(b))

err = json.NewEncoder(w).Encode(map[string]any{
"data": map[string]any{
Expand Down
12 changes: 6 additions & 6 deletions client/withfilesoption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func TestWithFiles(t *testing.T) {
contentDisposition := p.Header.Get("Content-Disposition")

if contentDisposition == `form-data; name="operations"` {
assert.EqualValues(t, `{"query":"{ id }","variables":{"file":{}}}`, slurp)
assert.JSONEq(t, `{"query":"{ id }","variables":{"file":{}}}`, string(slurp))
}
if contentDisposition == `form-data; name="map"` {
assert.EqualValues(t, `{"0":["variables.file"]}`, slurp)
assert.JSONEq(t, `{"0":["variables.file"]}`, string(slurp))
}
if regexp.MustCompile(`form-data; name="0"; filename=.*`).MatchString(contentDisposition) {
assert.Equal(t, `text/plain; charset=utf-8`, p.Header.Get("Content-Type"))
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestWithFiles(t *testing.T) {
contentDisposition := p.Header.Get("Content-Disposition")

if contentDisposition == `form-data; name="operations"` {
assert.EqualValues(t, `{"query":"{ id }","variables":{"input":{"files":[{},{}]}}}`, slurp)
assert.JSONEq(t, `{"query":"{ id }","variables":{"input":{"files":[{},{}]}}}`, string(slurp))
}
if contentDisposition == `form-data; name="map"` {
// returns `{"0":["variables.input.files.0"],"1":["variables.input.files.1"]}`
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestWithFiles(t *testing.T) {
contentDisposition := p.Header.Get("Content-Disposition")

if contentDisposition == `form-data; name="operations"` {
assert.EqualValues(t, `{"query":"{ id }","variables":{"req":{"files":[{},{}],"foo":{"bar":{}}}}}`, slurp)
assert.JSONEq(t, `{"query":"{ id }","variables":{"req":{"files":[{},{}],"foo":{"bar":{}}}}}`, string(slurp))
}
if contentDisposition == `form-data; name="map"` {
// returns `{"0":["variables.req.files.0"],"1":["variables.req.files.1"],"2":["variables.req.foo.bar"]}`
Expand Down Expand Up @@ -228,10 +228,10 @@ func TestWithFiles(t *testing.T) {
contentDisposition := p.Header.Get("Content-Disposition")

if contentDisposition == `form-data; name="operations"` {
assert.EqualValues(t, `{"query":"{ id }","variables":{"files":[{},{},{}]}}`, slurp)
assert.JSONEq(t, `{"query":"{ id }","variables":{"files":[{},{},{}]}}`, string(slurp))
}
if contentDisposition == `form-data; name="map"` {
assert.EqualValues(t, `{"0":["variables.files.0","variables.files.2"],"1":["variables.files.1"]}`, slurp)
assert.JSONEq(t, `{"0":["variables.files.0","variables.files.2"],"1":["variables.files.1"]}`, string(slurp))
// returns `{"0":["variables.files.0","variables.files.2"],"1":["variables.files.1"]}`
// but the order of file inputs is unpredictable between different OS systems
assert.Contains(t, string(slurp), `{"0":`)
Expand Down
16 changes: 8 additions & 8 deletions graphql/executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestExecutor(t *testing.T) {

t.Run("calls query on executable schema", func(t *testing.T) {
resp := query(exec, "", "{name}")
assert.Equal(t, `{"name":"test"}`, string(resp.Data))
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
})

t.Run("validates operation", func(t *testing.T) {
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestExecutor(t *testing.T) {
})

resp := query(exec, "", "{name}")
assert.Equal(t, `{"name":"test"}`, string(resp.Data))
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
assert.Equal(t, []string{"first", "second"}, calls)
})

Expand All @@ -67,7 +67,7 @@ func TestExecutor(t *testing.T) {
})

resp := query(exec, "", "{name}")
assert.Equal(t, `{"name":"test"}`, string(resp.Data))
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
assert.Equal(t, []string{"first", "second"}, calls)
})

Expand All @@ -83,7 +83,7 @@ func TestExecutor(t *testing.T) {
})

resp := query(exec, "", "{name}")
assert.Equal(t, `{"name":"test"}`, string(resp.Data))
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
assert.Equal(t, []string{"first", "second"}, calls)
})

Expand All @@ -99,7 +99,7 @@ func TestExecutor(t *testing.T) {
})

resp := query(exec, "", "{name}")
assert.Equal(t, `{"name":"test"}`, string(resp.Data))
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
assert.Equal(t, []string{"first", "second"}, calls)
})

Expand All @@ -118,7 +118,7 @@ func TestExecutor(t *testing.T) {
},
})
resp := query(exec, "", "{name}")
assert.Equal(t, `{"name":"test"}`, string(resp.Data))
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))
assert.Equal(t, []string{"param", "context"}, calls)
})

Expand Down Expand Up @@ -147,7 +147,7 @@ func TestExecutor(t *testing.T) {

t.Run("cache miss populates cache", func(t *testing.T) {
resp := query(exec, "Foo", qry)
assert.Equal(t, `{"name":"test"}`, string(resp.Data))
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))

cacheDoc, ok := cache.Get(ctx, qry)
require.True(t, ok)
Expand All @@ -160,7 +160,7 @@ func TestExecutor(t *testing.T) {
cache.Add(ctx, qry, doc)

resp := query(exec, "Bar", qry)
assert.Equal(t, `{"name":"test"}`, string(resp.Data))
assert.JSONEq(t, `{"name":"test"}`, string(resp.Data))

cacheDoc, ok := cache.Get(ctx, qry)
require.True(t, ok)
Expand Down
2 changes: 1 addition & 1 deletion graphql/handler/extension/apq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestAPQIntegration(t *testing.T) {

resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }","extensions":{"persistedQuery":{"version":1,"sha256Hash":"30166fc3298853f22709fce1e4a00e98f1b6a3160eaaaf9cb3b7db6a16073b07"}}}`)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"name":"test"}}`, resp.Body.String())

require.NotNil(t, stats)
require.True(t, stats.SentQuery)
Expand Down
12 changes: 6 additions & 6 deletions graphql/handler/extension/complexity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestHandlerComplexity(t *testing.T) {
h.SetCalculatedComplexity(2)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"name":"test"}}`, resp.Body.String())

require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 2, stats.Complexity)
Expand All @@ -48,7 +48,7 @@ func TestHandlerComplexity(t *testing.T) {
h.SetCalculatedComplexity(4)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"operation has complexity 4, which exceeds the limit of 2","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}],"data":null}`, resp.Body.String())
require.JSONEq(t, `{"errors":[{"message":"operation has complexity 4, which exceeds the limit of 2","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}],"data":null}`, resp.Body.String())

require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 4, stats.Complexity)
Expand All @@ -59,7 +59,7 @@ func TestHandlerComplexity(t *testing.T) {
h.SetCalculatedComplexity(4)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ ok: name }"}`)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"name":"test"}}`, resp.Body.String())

require.Equal(t, 4, stats.ComplexityLimit)
require.Equal(t, 4, stats.Complexity)
Expand All @@ -81,7 +81,7 @@ func TestFixedComplexity(t *testing.T) {
h.SetCalculatedComplexity(2)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"name":"test"}}`, resp.Body.String())

require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 2, stats.Complexity)
Expand All @@ -91,7 +91,7 @@ func TestFixedComplexity(t *testing.T) {
h.SetCalculatedComplexity(4)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"operation has complexity 4, which exceeds the limit of 2","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}],"data":null}`, resp.Body.String())
require.JSONEq(t, `{"errors":[{"message":"operation has complexity 4, which exceeds the limit of 2","extensions":{"code":"COMPLEXITY_LIMIT_EXCEEDED"}}],"data":null}`, resp.Body.String())

require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 4, stats.Complexity)
Expand All @@ -101,7 +101,7 @@ func TestFixedComplexity(t *testing.T) {
h.SetCalculatedComplexity(4)
resp := doRequest(h, "POST", "/graphql", `{ "operationName":"IntrospectionQuery", "query":"query IntrospectionQuery { __schema { queryType { name } mutationType { name }}}"}`)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"name":"test"}}`, resp.Body.String())

require.Equal(t, 2, stats.ComplexityLimit)
require.Equal(t, 0, stats.Complexity)
Expand Down
12 changes: 6 additions & 6 deletions graphql/handler/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,25 @@ func TestServer(t *testing.T) {
t.Run("returns an error if no transport matches", func(t *testing.T) {
resp := post(srv, "/foo", "application/json")
assert.Equal(t, http.StatusBadRequest, resp.Code)
assert.Equal(t, `{"errors":[{"message":"transport not supported"}],"data":null}`, resp.Body.String())
assert.JSONEq(t, `{"errors":[{"message":"transport not supported"}],"data":null}`, resp.Body.String())
})

t.Run("calls query on executable schema", func(t *testing.T) {
resp := get(srv, "/foo?query={name}")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
assert.JSONEq(t, `{"data":{"name":"test"}}`, resp.Body.String())
})

t.Run("mutations are forbidden", func(t *testing.T) {
resp := get(srv, "/foo?query=mutation{name}")
assert.Equal(t, http.StatusNotAcceptable, resp.Code)
assert.Equal(t, `{"errors":[{"message":"GET requests only allow query operations"}],"data":null}`, resp.Body.String())
assert.JSONEq(t, `{"errors":[{"message":"GET requests only allow query operations"}],"data":null}`, resp.Body.String())
})

t.Run("subscriptions are forbidden", func(t *testing.T) {
resp := get(srv, "/foo?query=subscription{name}")
assert.Equal(t, http.StatusNotAcceptable, resp.Code)
assert.Equal(t, `{"errors":[{"message":"GET requests only allow query operations"}],"data":null}`, resp.Body.String())
assert.JSONEq(t, `{"errors":[{"message":"GET requests only allow query operations"}],"data":null}`, resp.Body.String())
})

t.Run("invokes operation middleware in order", func(t *testing.T) {
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestServer(t *testing.T) {
t.Run("cache miss populates cache", func(t *testing.T) {
resp := get(srv, "/foo?query="+url.QueryEscape(qry))
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
assert.JSONEq(t, `{"data":{"name":"test"}}`, resp.Body.String())

cacheDoc, ok := cache.Get(ctx, qry)
require.True(t, ok)
Expand All @@ -134,7 +134,7 @@ func TestServer(t *testing.T) {

resp := get(srv, "/foo?query="+url.QueryEscape(qry))
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
assert.JSONEq(t, `{"data":{"name":"test"}}`, resp.Body.String())

cacheDoc, ok := cache.Get(ctx, qry)
require.True(t, ok)
Expand Down
22 changes: 11 additions & 11 deletions graphql/handler/transport/http_form_multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"singleUpload":"test"}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"singleUpload":"test"}}`, resp.Body.String())
})

t.Run("valid single file upload with payload", func(t *testing.T) {
Expand All @@ -92,7 +92,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"singleUploadWithPayload":"test"}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"singleUploadWithPayload":"test"}}`, resp.Body.String())
})

t.Run("valid file list upload", func(t *testing.T) {
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"multipleUpload":[{"id":1},{"id":2}]}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"multipleUpload":[{"id":1},{"id":2}]}}`, resp.Body.String())
})

t.Run("valid file list upload with payload", func(t *testing.T) {
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code)
require.Equal(t, `{"data":{"multipleUploadWithPayload":[{"id":1},{"id":2}]}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"multipleUploadWithPayload":[{"id":1},{"id":2}]}}`, resp.Body.String())
})

t.Run("valid file list upload with payload and file reuse", func(t *testing.T) {
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"data":{"multipleUploadWithPayload":[{"id":1},{"id":2}]}}`, resp.Body.String())
require.JSONEq(t, `{"data":{"multipleUploadWithPayload":[{"id":1},{"id":2}]}}`, resp.Body.String())
}

t.Run("payload smaller than UploadMaxMemory, stored in memory", func(t *testing.T) {
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"first part must be operations"}],"data":null}`, resp.Body.String())
require.JSONEq(t, `{"errors":[{"message":"first part must be operations"}],"data":null}`, resp.Body.String())
})

t.Run("fail parse operation", func(t *testing.T) {
Expand All @@ -226,7 +226,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"operations form field could not be decoded"}],"data":null}`, resp.Body.String())
require.JSONEq(t, `{"errors":[{"message":"operations form field could not be decoded"}],"data":null}`, resp.Body.String())
})

t.Run("fail parse map", func(t *testing.T) {
Expand All @@ -236,7 +236,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"map form field could not be decoded"}],"data":null}`, resp.Body.String())
require.JSONEq(t, `{"errors":[{"message":"map form field could not be decoded"}],"data":null}`, resp.Body.String())
})

t.Run("fail missing file", func(t *testing.T) {
Expand All @@ -246,7 +246,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"failed to get key 0 from form"}],"data":null}`, resp.Body.String())
require.JSONEq(t, `{"errors":[{"message":"failed to get key 0 from form"}],"data":null}`, resp.Body.String())
})

t.Run("fail map entry with invalid operations paths prefix", func(t *testing.T) {
Expand All @@ -256,7 +256,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"invalid operations paths for key 0"}],"data":null}`, resp.Body.String())
require.JSONEq(t, `{"errors":[{"message":"invalid operations paths for key 0"}],"data":null}`, resp.Body.String())
})

t.Run("fail parse request big body", func(t *testing.T) {
Expand All @@ -266,7 +266,7 @@ func TestFileUpload(t *testing.T) {
resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
require.Equal(t, `{"errors":[{"message":"failed to parse multipart form, request body too large"}],"data":null}`, resp.Body.String())
require.JSONEq(t, `{"errors":[{"message":"failed to parse multipart form, request body too large"}],"data":null}`, resp.Body.String())
})
}

Expand Down
Loading

0 comments on commit 10b0e05

Please sign in to comment.