Skip to content

Commit b5640f8

Browse files
committed
Fixed issue when accept:*/* and operation is a subscription. (#7732)
1 parent 603beee commit b5640f8

File tree

4 files changed

+157
-2
lines changed

4 files changed

+157
-2
lines changed

src/HotChocolate/AspNetCore/src/AspNetCore/Serialization/DefaultHttpResponseFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ protected virtual void OnWriteResponseHeaders(
547547
return _multiPartFormat;
548548
}
549549

550-
if (mediaType.Kind is EventStream)
550+
if (mediaType.Kind is EventStream or All)
551551
{
552552
return _eventStreamFormat;
553553
}

src/HotChocolate/AspNetCore/test/AspNetCore.Tests.Utilities/ServerTestBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected virtual TestServer CreateStarWarsServer(
4141
.AddStarWarsTypes()
4242
.AddTypeExtension<QueryExtension>()
4343
.AddTypeExtension<SubscriptionsExtensions>()
44+
.AddType<Foo>()
4445
.AddStarWarsRepositories()
4546
.AddInMemorySubscriptions()
4647
.UseInstrumentation()
@@ -169,4 +170,10 @@ protected virtual TestServer CreateServer(
169170
.UseRouting()
170171
.UseEndpoints(endpoints => configureConventions?.Invoke(endpoints)));
171172
}
173+
174+
[DirectiveType(DirectiveLocation.Subscription)]
175+
public class Foo
176+
{
177+
public required int Bar { get; set; }
178+
}
172179
}

src/HotChocolate/AspNetCore/test/AspNetCore.Tests/GraphQLOverHttpSpecTests.cs

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,102 @@ public async Task EventStream_Sends_KeepAlive()
457457
Snapshot
458458
.Create()
459459
.Add(response)
460-
.MatchInline("""
460+
.MatchInline(
461+
"""
462+
Headers:
463+
Cache-Control: no-cache
464+
Content-Type: text/event-stream; charset=utf-8
465+
-------------------------->
466+
Status Code: OK
467+
-------------------------->
468+
event: next
469+
data: {"data":{"delay":"next"}}
470+
471+
:
472+
473+
event: next
474+
data: {"data":{"delay":"next"}}
475+
476+
:
477+
478+
event: complete
479+
480+
481+
""");
482+
}
483+
484+
[Fact]
485+
public async Task EventStream_When_Accept_Is_All()
486+
{
487+
// arrange
488+
var server = CreateStarWarsServer();
489+
var client = server.CreateClient();
490+
client.Timeout = TimeSpan.FromSeconds(30);
491+
492+
// act
493+
using var request = new HttpRequestMessage(HttpMethod.Post, _url);
494+
request.Content = JsonContent.Create(
495+
new ClientQueryRequest
496+
{
497+
Query = "subscription {delay(count: 2, delay:15000)}",
498+
});
499+
request.Headers.Add("Accept", "*/*");
500+
501+
using var response = await client.SendAsync(request, ResponseHeadersRead);
502+
503+
// assert
504+
Snapshot
505+
.Create()
506+
.Add(response)
507+
.MatchInline(
508+
"""
509+
Headers:
510+
Cache-Control: no-cache
511+
Content-Type: text/event-stream; charset=utf-8
512+
-------------------------->
513+
Status Code: OK
514+
-------------------------->
515+
event: next
516+
data: {"data":{"delay":"next"}}
517+
518+
:
519+
520+
event: next
521+
data: {"data":{"delay":"next"}}
522+
523+
:
524+
525+
event: complete
526+
527+
528+
""");
529+
}
530+
531+
[Fact]
532+
public async Task EventStream_When_Accept_Is_All_And_Subscription_Directive()
533+
{
534+
// arrange
535+
var server = CreateStarWarsServer();
536+
var client = server.CreateClient();
537+
client.Timeout = TimeSpan.FromSeconds(30);
538+
539+
// act
540+
using var request = new HttpRequestMessage(HttpMethod.Post, _url);
541+
request.Content = JsonContent.Create(
542+
new ClientQueryRequest
543+
{
544+
Query = "subscription foo @foo(bar: 1) {delay(count: 2, delay:15000)}",
545+
});
546+
request.Headers.Add("Accept", "*/*");
547+
548+
using var response = await client.SendAsync(request, ResponseHeadersRead);
549+
550+
// assert
551+
Snapshot
552+
.Create()
553+
.Add(response)
554+
.MatchInline(
555+
"""
461556
Headers:
462557
Cache-Control: no-cache
463558
Content-Type: text/event-stream; charset=utf-8

src/HotChocolate/Core/test/Types.Tests/Types/SubscriptionTypeTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma warning disable CS0618 // Type or member is obsolete
77
#nullable enable
88

9+
using System.Runtime.CompilerServices;
910
using CookieCrumble;
1011
using HotChocolate.Execution;
1112
using HotChocolate.Subscriptions;
@@ -647,6 +648,44 @@ public async Task Arguments_Can_Be_Declared_On_The_Stream()
647648
""");
648649
}
649650

651+
[Fact]
652+
public async Task Subscription_Directives_Are_Allowed()
653+
{
654+
// arrange
655+
// act
656+
var executor = await new ServiceCollection()
657+
.AddGraphQLServer()
658+
.AddDocumentFromString(
659+
"""
660+
type Subscription {
661+
bookAdded: String!
662+
}
663+
664+
directive @bug(test: Int!) on SUBSCRIPTION
665+
""")
666+
.BindRuntimeType<SubscriptionWithDirective>("Subscription")
667+
.ModifyOptions(o => o.StrictValidation = false)
668+
.BuildRequestExecutorAsync();
669+
670+
var result = await executor.ExecuteAsync(
671+
"""
672+
subscription test @bug(test: 123) {
673+
bookAdded
674+
}
675+
""");
676+
677+
// assert
678+
result.MatchInlineSnapshot(
679+
"""
680+
{
681+
"data": {
682+
"bookAdded": "foo"
683+
}
684+
}
685+
686+
""");
687+
}
688+
650689
public class TestObservable : IObservable<string>, IDisposable
651690
{
652691
public bool DisposeRaised { get; private set; }
@@ -1047,4 +1086,18 @@ public string OnExplicit(
10471086
[EventMessage] string message) =>
10481087
message;
10491088
}
1089+
1090+
1091+
public class SubscriptionWithDirective
1092+
{
1093+
[Subscribe(With = nameof(GetStream))]
1094+
public string BookAdded([EventMessage] string foo) => foo;
1095+
1096+
private async IAsyncEnumerable<string> GetStream(
1097+
[EnumeratorCancellation] CancellationToken ct = default)
1098+
{
1099+
await Task.Delay(200, ct);
1100+
yield return "foo";
1101+
}
1102+
}
10501103
}

0 commit comments

Comments
 (0)