-
-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathreacting_to_entity_attributes.cs
More file actions
154 lines (129 loc) · 4.34 KB
/
reacting_to_entity_attributes.cs
File metadata and controls
154 lines (129 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using Alba;
using IntegrationTests;
using Marten;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Refit;
using Shouldly;
using Wolverine.Marten;
using Wolverine.Persistence;
using Xunit.Abstractions;
namespace Wolverine.Http.Tests.Persistence;
// See RequiredTodoEndpoint
public class reacting_to_entity_attributes : IAsyncLifetime
{
private readonly ITestOutputHelper _output;
private IAlbaHost theHost;
public reacting_to_entity_attributes(ITestOutputHelper output)
{
_output = output;
}
public async Task InitializeAsync()
{
var builder = WebApplication.CreateBuilder([]);
// config
builder.Services.AddMarten(opts =>
{
// Establish the connection string to your Marten database
opts.Connection(Servers.PostgresConnectionString);
opts.DatabaseSchemaName = "onmissing";
}).IntegrateWithWolverine().UseLightweightSessions();
builder.Host.UseWolverine(opts => opts.Discovery.IncludeAssembly(GetType().Assembly));
builder.Services.AddWolverineHttp();
// This is using Alba, which uses WebApplicationFactory under the covers
theHost = await AlbaHost.For(builder, app =>
{
app.UseDeveloperExceptionPage();
app.MapWolverineEndpoints();
});
}
async Task IAsyncLifetime.DisposeAsync()
{
if (theHost != null)
{
await theHost.StopAsync();
theHost.Dispose();
}
}
[Fact]
public async Task default_404_behavior_on_missing()
{
var tracked = await theHost.Scenario(x =>
{
x.Get.Url("/required/todo404/nonexistent");
x.StatusCodeShouldBe(404);
});
tracked.ReadAsText().ShouldBeEmpty();
}
[Fact]
public async Task problem_details_400_on_missing()
{
var tracked = await theHost.Scenario(x =>
{
x.Get.Url("/required/todo400/nonexistent");
x.StatusCodeShouldBe(400);
x.ContentTypeShouldBe("application/problem+json");
});
var details = tracked.ReadAsJson<ProblemDetails>();
details.Detail.ShouldBe("Unknown Todo2 with identity nonexistent");
}
[Fact]
public async Task problem_details_404_on_missing()
{
var tracked = await theHost.Scenario(x =>
{
x.Get.Url("/required/todo3/nonexistent");
x.StatusCodeShouldBe(404);
x.ContentTypeShouldBe("application/problem+json");
});
var details = tracked.ReadAsJson<ProblemDetails>();
details.Detail.ShouldBe("Unknown Todo2 with identity nonexistent");
}
[Fact]
public async Task throw_exception_on_missing()
{
var tracked = await theHost.Scenario(x =>
{
x.Get.Url("/required/todo4/nonexistent");
x.StatusCodeShouldBe(500);
});
var text = tracked.ReadAsText();
text.ShouldContain(typeof(RequiredDataMissingException).FullName);
}
[Fact]
public async Task problem_details_400_on_missing_with_custom_message()
{
var tracked = await theHost.Scenario(x =>
{
x.Get.Url("/required/todo5/nonexistent");
x.StatusCodeShouldBe(400);
x.ContentTypeShouldBe("application/problem+json");
});
var details = tracked.ReadAsJson<ProblemDetails>();
details.Detail.ShouldBe("Wrong id man!");
}
[Fact]
public async Task problem_details_400_on_missing_with_custom_message_using_id()
{
var tracked = await theHost.Scenario(x =>
{
x.Get.Url("/required/todo6/nonexistent");
x.StatusCodeShouldBe(400);
x.ContentTypeShouldBe("application/problem+json");
});
var details = tracked.ReadAsJson<ProblemDetails>();
details.Detail.ShouldBe("Id 'nonexistent' is wrong!");
}
[Fact]
public async Task throw_exception_on_missing_with_custom_message()
{
var tracked = await theHost.Scenario(x =>
{
x.Get.Url("/required/todo7/nonexistent");
x.StatusCodeShouldBe(500);
});
var text = tracked.ReadAsText();
text.ShouldContain(typeof(RequiredDataMissingException).FullName);
text.ShouldContain("Id 'nonexistent' is wrong!");
}
}