Skip to content

Commit e5c058e

Browse files
xperiandriclaude
andcommitted
Named the persisted query constants and the media type in the tests
The tests take the error message, its code, the cache directive and the query string parameter from `PersistedQueries` and the media type from `MediaTypeNames` instead of spelling them out again, and assert with `Assert.Equal` and `Assert.Single` instead of `Assert.True` on a comparison. The error extensions are built with `kvpObj` and without an upcast. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1b08823 commit e5c058e

2 files changed

Lines changed: 19 additions & 29 deletions

File tree

‎src/FSharp.Data.GraphQL.Server.AspNetCore/GraphQLRequestHandler.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ module internal PersistedQueries =
6969
/// The error Apollo Server answers with. Shared by every request, which is safe because both the record and its
7070
/// immutable extensions dictionary cannot be changed.
7171
let notSupportedError =
72-
let extensions = ImmutableDictionary.CreateRange (StringComparer.Ordinal, [ KeyValuePair ("code", box NotSupportedCode) ])
73-
GQLProblemDetails.Create (NotSupportedMessage, extensions :> IReadOnlyDictionary<string, obj>)
72+
let extensions = ImmutableDictionary.CreateRange (StringComparer.Ordinal, [ kvpObj "code" NotSupportedCode ])
73+
GQLProblemDetails.Create (NotSupportedMessage, extensions)
7474

7575
/// <summary>
7676
/// Whether the <c>extensions</c> of a request ask for an Apollo automatic persisted query: whether their

‎tests/FSharp.Data.GraphQL.Tests/AspNetCore/RequestHandlerExtensibilityTests.fs‎

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module FSharp.Data.GraphQL.Tests.AspNetCore.RequestHandlerExtensibilityTests
33
open System
44
open System.IO
55
open System.Net.Http
6+
open System.Net.Mime
67
open System.Text
78
open System.Text.Json
89
open System.Threading
@@ -75,7 +76,7 @@ let private createHandlerFor<'Handler when 'Handler :> GraphQLRequestHandler<Roo
7576
struct (handler, (handler :?> 'Handler), (ctx :> HttpContext), (scope :> IDisposable))
7677

7778
let private setJsonBody (json : string) (request : HttpRequest) =
78-
request.ContentType <- "application/json"
79+
request.ContentType <- MediaTypeNames.Application.Json
7980
let bytes = Encoding.UTF8.GetBytes json
8081
request.Body <- new MemoryStream (bytes)
8182
request.ContentLength <- int64 bytes.Length
@@ -211,22 +212,20 @@ let private assertPersistedQueryNotSupported (recorder : RecordingHandler) (ctx
211212

212213
let! struct (statusCode, responseBody) = executeOutcome ctx outcome
213214

214-
Assert.True (
215-
(statusCode = StatusCodes.Status200OK),
216-
$"Expected HTTP 200, as Apollo Server answers an unsupported persisted query, but got HTTP %d{statusCode} with body:\n%s{responseBody}"
217-
)
215+
// HTTP 200, as Apollo Server answers an unsupported persisted query
216+
Assert.Equal (StatusCodes.Status200OK, statusCode)
218217
use document = JsonDocument.Parse responseBody
219218
let root = document.RootElement
220219
// Apollo Client 4 falls back to the full query only for a result without other top-level members than data, errors and
221220
// extensions, so the answer has the errors alone, as Apollo Server's
222221
let members = root.EnumerateObject () |> Seq.map _.Name |> List.ofSeq
223-
Assert.True ((members = [ "errors" ]), $"Expected the response to have the member 'errors' alone, but got:\n%s{responseBody}")
222+
Assert.Equal<string list> ([ "errors" ], members)
224223
let errors = root |> wantMember "errors" responseBody
225224
let error = Assert.Single (errors.EnumerateArray ())
226-
Assert.Equal ("PersistedQueryNotSupported", (error |> wantMember "message" responseBody).GetString ())
225+
Assert.Equal (PersistedQueries.NotSupportedMessage, (error |> wantMember "message" responseBody).GetString ())
227226
let code = error |> wantMember "extensions" responseBody |> wantMember "code" responseBody
228-
Assert.Equal ("PERSISTED_QUERY_NOT_SUPPORTED", code.GetString ())
229-
Assert.Equal ("private, no-cache, must-revalidate", ctx.Response.Headers.CacheControl.ToString ())
227+
Assert.Equal (PersistedQueries.NotSupportedCode, code.GetString ())
228+
Assert.Equal (PersistedQueries.notSupportedCacheControl.ToString (), ctx.Response.Headers.CacheControl.ToString ())
230229
}
231230

232231
[<Fact>]
@@ -252,7 +251,7 @@ let ``GET with only a persisted query hash is answered with PersistedQueryNotSup
252251
|> setQueryString [
253252
"operationName", persistedQueryName
254253
"variables", "{}"
255-
"extensions", persistedQueryExtensions
254+
PersistedQueries.ExtensionsParameterName, persistedQueryExtensions
256255
])
257256
use _ = scope
258257

@@ -301,11 +300,8 @@ let ``Multipart POST operation is executed as before`` () : Task = task {
301300

302301
let! outcome = handler.HandleAsync ()
303302

304-
Assert.True (
305-
(recorder.OperationCalls.Count = 1),
306-
$"Expected the operation of a multipart request to be executed once, but ExecuteOperation was called %d{recorder.OperationCalls.Count} time(s); outcome: %A{outcome}"
307-
)
308-
Assert.Equal (query, recorder.OperationCalls[0].Query)
303+
let call = Assert.Single recorder.OperationCalls
304+
Assert.Equal (query, call.Query)
309305
Assert.Equal (0, recorder.IntrospectionCalls.Count)
310306
assertOkResponse outcome
311307
}
@@ -319,7 +315,7 @@ let ``GET with both a query and a persisted query hash is answered with Persiste
319315
|> setQueryString [
320316
"query", persistedQueryText
321317
"operationName", persistedQueryName
322-
"extensions", persistedQueryExtensions
318+
PersistedQueries.ExtensionsParameterName, persistedQueryExtensions
323319
])
324320
use _ = scope
325321

@@ -349,11 +345,8 @@ let ``POST operation whose extensions do not ask for a persisted query is execut
349345

350346
let! outcome = handler.HandleAsync ()
351347

352-
Assert.True (
353-
(recorder.OperationCalls.Count = 1),
354-
$"Expected the operation to be executed once despite extensions %s{extensions}, but ExecuteOperation was called %d{recorder.OperationCalls.Count} time(s)"
355-
)
356-
Assert.Equal (query, recorder.OperationCalls[0].Query)
348+
let call = Assert.Single recorder.OperationCalls
349+
Assert.Equal (query, call.Query)
357350
Assert.Equal (0, recorder.IntrospectionCalls.Count)
358351
assertOkResponse outcome
359352
}
@@ -396,16 +389,13 @@ let ``GET whose extensions do not ask for a persisted query is still answered wi
396389
let struct (handler, recorder, _, scope) =
397390
createHandlerFor<RecordingHandler> (fun request ->
398391
request.Method <- HttpMethods.Get
399-
request |> setQueryString [ "extensions", extensions ])
392+
request |> setQueryString [ PersistedQueries.ExtensionsParameterName, extensions ])
400393
use _ = scope
401394

402395
let! outcome = handler.HandleAsync ()
403396

404-
Assert.True (
405-
(recorder.IntrospectionCalls.Count = 1),
406-
$"Expected the introspection query to be executed once, but ExecuteIntrospectionQuery was called %d{recorder.IntrospectionCalls.Count} time(s)"
407-
)
408-
Assert.Equal (ValueNone, recorder.IntrospectionCalls[0])
397+
let call = Assert.Single recorder.IntrospectionCalls
398+
Assert.Equal (ValueNone, call)
409399
Assert.Equal (0, recorder.OperationCalls.Count)
410400
assertOkResponse outcome
411401
}

0 commit comments

Comments
 (0)