Skip to content

Commit 985fb9b

Browse files
committed
test(response): verify all supported sequential media types
1 parent 0425f7b commit 985fb9b

3 files changed

Lines changed: 53 additions & 34 deletions

File tree

tests/Example.OpenApi32.IntegrationTests/ExportFooEventsTests.cs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,26 @@ namespace Example.OpenApi32.IntegrationTests;
77

88
public class ExportFooEventsTests(FooApplicationFactory app, ITestOutputHelper testOutput) : FooTestSpecification, IClassFixture<FooApplicationFactory>
99
{
10-
[Fact]
11-
public async Task ExportingFooEvents_ShouldReturnOkWithJsonl()
10+
[Theory]
11+
[InlineData("application/jsonl")]
12+
[InlineData("application/x-jsonlines")]
13+
[InlineData("application/x-ndjson")]
14+
[InlineData("application/json-seq")]
15+
[InlineData("application/geo+json-seq")]
16+
public async Task ExportingFooEvents_ShouldReturnOkWithSequentialJson(string mediaType)
1217
{
1318
using var client = app.CreateClient();
1419
var request = new HttpRequestMessage
1520
{
1621
RequestUri = new Uri(client.BaseAddress!, "/foo/1/events"),
1722
Method = HttpMethod.Get
1823
};
19-
request.Headers.Accept.ParseAdd("application/jsonl");
24+
request.Headers.Accept.ParseAdd(mediaType);
2025

2126
var result = await client.SendAsync(request, CancellationToken);
2227

2328
result.StatusCode.Should().Be(HttpStatusCode.OK);
24-
result.Content.Headers.ContentType?.MediaType.Should().Be("application/jsonl");
25-
26-
var content = await result.Content.ReadAsStringAsync(CancellationToken);
27-
testOutput.WriteLine("Content:");
28-
testOutput.WriteLine(content);
29-
var lines = content.Split('\n', StringSplitOptions.RemoveEmptyEntries);
30-
lines.Should().HaveCount(2);
31-
JsonNode.Parse(lines[0]).GetValue<string>("#/Name").Should().Be("foo1");
32-
JsonNode.Parse(lines[1]).GetValue<string>("#/Name").Should().Be("foo2");
33-
}
34-
35-
[Fact]
36-
public async Task ExportingFooEvents_ShouldReturnOkWithJsonSeq()
37-
{
38-
using var client = app.CreateClient();
39-
var request = new HttpRequestMessage
40-
{
41-
RequestUri = new Uri(client.BaseAddress!, "/foo/1/events"),
42-
Method = HttpMethod.Get
43-
};
44-
request.Headers.Accept.ParseAdd("application/json-seq");
45-
46-
var result = await client.SendAsync(request, CancellationToken);
47-
48-
result.StatusCode.Should().Be(HttpStatusCode.OK);
49-
result.Content.Headers.ContentType?.MediaType.Should().Be("application/json-seq");
29+
result.Content.Headers.ContentType?.MediaType.Should().Be(mediaType);
5030

5131
var content = await result.Content.ReadAsStringAsync(CancellationToken);
5232
testOutput.WriteLine("Content:");
@@ -58,4 +38,4 @@ public async Task ExportingFooEvents_ShouldReturnOkWithJsonSeq()
5838
JsonNode.Parse(lines[0]).GetValue<string>("#/Name").Should().Be("foo1");
5939
JsonNode.Parse(lines[1]).GetValue<string>("#/Name").Should().Be("foo2");
6040
}
61-
}
41+
}
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Example.OpenApi32.Components.Schemas;
2+
13
namespace Example.OpenApi32.Paths.FooFooIdEvents.Get;
24

35
internal partial class Operation
@@ -9,16 +11,33 @@ internal partial Task<Response> HandleAsync(Request request, CancellationToken c
911
case false:
1012
case true when matchedMediaType == Response.OK200.ApplicationJsonl.ContentMediaType:
1113
var jsonl = new Response.OK200.ApplicationJsonl(request);
12-
jsonl.WriteItem(Components.Schemas.FooProperties.Create(name: "foo1"));
13-
jsonl.WriteItem(Components.Schemas.FooProperties.Create(name: "foo2"));
14+
WriteItems(jsonl.WriteItem);
1415
return Task.FromResult<Response>(jsonl);
16+
case true when matchedMediaType == Response.OK200.ApplicationXJsonlines.ContentMediaType:
17+
var jsonLines = new Response.OK200.ApplicationXJsonlines(request);
18+
WriteItems(jsonLines.WriteItem);
19+
return Task.FromResult<Response>(jsonLines);
20+
case true when matchedMediaType == Response.OK200.ApplicationXNdjson.ContentMediaType:
21+
var ndJson = new Response.OK200.ApplicationXNdjson(request);
22+
WriteItems(ndJson.WriteItem);
23+
return Task.FromResult<Response>(ndJson);
1524
case true when matchedMediaType == Response.OK200.ApplicationJsonSeq.ContentMediaType:
1625
var jsonSeq = new Response.OK200.ApplicationJsonSeq(request);
17-
jsonSeq.WriteItem(Components.Schemas.FooProperties.Create(name: "foo1"));
18-
jsonSeq.WriteItem(Components.Schemas.FooProperties.Create(name: "foo2"));
26+
WriteItems(jsonSeq.WriteItem);
1927
return Task.FromResult<Response>(jsonSeq);
28+
case true when matchedMediaType == Response.OK200.ApplicationGeoJsonSeq.ContentMediaType:
29+
var geoJsonSeq = new Response.OK200.ApplicationGeoJsonSeq(request);
30+
WriteItems(geoJsonSeq.WriteItem);
31+
return Task.FromResult<Response>(geoJsonSeq);
2032
default:
2133
throw new NotImplementedException($"Content media type {matchedMediaType} has not been implemented");
34+
35+
}
36+
37+
void WriteItems(Action<FooProperties> write)
38+
{
39+
write(FooProperties.Create(name: "foo1"));
40+
write(FooProperties.Create(name: "foo2"));
2241
}
2342
}
2443
}

tests/Example.OpenApi32/openapi.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,26 @@
126126
"itemSchema": {
127127
"$ref": "#/components/schemas/FooProperties"
128128
}
129+
},
130+
"application/x-jsonlines": {
131+
"itemSchema": {
132+
"$ref": "#/components/schemas/FooProperties"
133+
}
134+
},
135+
"application/x-ndjson": {
136+
"itemSchema": {
137+
"$ref": "#/components/schemas/FooProperties"
138+
}
139+
},
140+
"application/json-seq": {
141+
"itemSchema": {
142+
"$ref": "#/components/schemas/FooProperties"
143+
}
144+
},
145+
"application/geo+json-seq": {
146+
"itemSchema": {
147+
"$ref": "#/components/schemas/FooProperties"
148+
}
129149
}
130150
}
131151
},

0 commit comments

Comments
 (0)