Skip to content

Commit 2656edb

Browse files
committed
test(response): json-seq sequential media type
1 parent b24dcb1 commit 2656edb

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

tests/Example.OpenApi32.IntegrationTests/ExportFooEventsTests.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,31 @@ public async Task ExportingFooEvents_ShouldReturnOkWithJsonl()
3131
JsonNode.Parse(lines[0]).GetValue<string>("#/Name").Should().Be("foo1");
3232
JsonNode.Parse(lines[1]).GetValue<string>("#/Name").Should().Be("foo2");
3333
}
34-
}
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");
50+
51+
var content = await result.Content.ReadAsStringAsync(CancellationToken);
52+
testOutput.WriteLine("Content:");
53+
testOutput.WriteLine(content);
54+
var lines = content.Split('\n', StringSplitOptions.RemoveEmptyEntries)
55+
.Select(s => s.Trim((char)0x1E))
56+
.ToArray();
57+
lines.Should().HaveCount(2);
58+
JsonNode.Parse(lines[0]).GetValue<string>("#/Name").Should().Be("foo1");
59+
JsonNode.Parse(lines[1]).GetValue<string>("#/Name").Should().Be("foo2");
60+
}
61+
}

tests/Example.OpenApi32/Paths/FooFooIdEvents/Get/Operation.Handler.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ internal partial Task<Response> HandleAsync(Request request, CancellationToken c
88
{
99
case false:
1010
case true when matchedMediaType == Response.OK200.ApplicationJsonl.ContentMediaType:
11-
var response = new Response.OK200.ApplicationJsonl(request);
12-
response.WriteItem(Components.Schemas.FooProperties.Create(name: "foo1"));
13-
response.WriteItem(Components.Schemas.FooProperties.Create(name: "foo2"));
14-
return Task.FromResult<Response>(response);
11+
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+
return Task.FromResult<Response>(jsonl);
15+
case true when matchedMediaType == Response.OK200.ApplicationJsonSeq.ContentMediaType:
16+
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"));
19+
return Task.FromResult<Response>(jsonSeq);
1520
default:
1621
throw new NotImplementedException($"Content media type {matchedMediaType} has not been implemented");
1722
}

tests/Example.OpenApi32/openapi.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@
9292
"itemSchema": {
9393
"$ref": "#/components/schemas/FooProperties"
9494
}
95+
},
96+
"application/json-seq": {
97+
"itemSchema": {
98+
"$ref": "#/components/schemas/FooProperties"
99+
}
95100
}
96101
}
97102
}

0 commit comments

Comments
 (0)