Skip to content

Commit f3b608c

Browse files
[Backport to 4.1] Validates composition can be called multiple times (#830) (#831)
* Add a test that validates composition can be called multiple times (#830) * Use the correct context (#803) Related to #802
1 parent b6b3c52 commit f3b608c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/ServiceComposer.AspNetCore.Tests/Get_with_2_handlers.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,45 @@ public async Task Returns_expected_response()
7575
Assert.Equal("sample", responseObj?.SelectToken("AString")?.Value<string>());
7676
Assert.Equal(1, responseObj?.SelectToken("ANumber")?.Value<int>());
7777
}
78+
79+
[Fact]
80+
public async Task Returns_expected_response_when_invoked_more_than_once()
81+
{
82+
// Arrange
83+
var client = new SelfContainedWebApplicationFactoryWithWebHost<Dummy>
84+
(
85+
configureServices: services =>
86+
{
87+
services.AddViewModelComposition(options =>
88+
{
89+
options.AssemblyScanner.Disable();
90+
options.RegisterCompositionHandler<TestGetStringHandler>();
91+
options.RegisterCompositionHandler<TestGetIntegerHandler>();
92+
});
93+
services.AddRouting();
94+
services.AddControllers();
95+
},
96+
configure: app =>
97+
{
98+
app.UseRouting();
99+
app.UseEndpoints(builder => builder.MapCompositionHandlers());
100+
}
101+
).CreateClient();
102+
103+
client.DefaultRequestHeaders.Add("Accept-Casing", "casing/pascal");
104+
// Act
105+
_ = await client.GetAsync("/sample/1");
106+
var response = await client.GetAsync("/sample/1");
107+
108+
// Assert
109+
Assert.True(response.IsSuccessStatusCode);
110+
111+
var responseString = await response.Content.ReadAsStringAsync();
112+
var responseObj = JObject.Parse(responseString);
113+
114+
Assert.Equal("sample", responseObj?.SelectToken("AString")?.Value<string>());
115+
Assert.Equal(1, responseObj?.SelectToken("ANumber")?.Value<int>());
116+
}
78117

79118
[Fact]
80119
public async Task Returns_expected_response_using_output_formatters()

src/ServiceComposer.AspNetCore/CompositionEndpointBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public override Endpoint Build()
106106

107107
RequestDelegate composer = async composerHttpContext =>
108108
{
109-
var requestId = context.EnsureRequestIdIsSetup();
109+
var requestId = composerHttpContext.EnsureRequestIdIsSetup();
110110
var compositionContext = new CompositionContext
111111
(
112112
requestId,
@@ -166,4 +166,4 @@ public override Endpoint Build()
166166
return routeEndpoint;
167167
}
168168
}
169-
}
169+
}

0 commit comments

Comments
 (0)