Repository navigation
Expand file tree
/
Copy pathHttpMockInterfaceTest.cs
More file actions
184 lines (147 loc) · 8.63 KB
/
Copy pathHttpMockInterfaceTest.cs
File metadata and controls
184 lines (147 loc) · 8.63 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/UnitTestEx
using Moq;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Json;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using UnitTestEx.Mocking;
using Xunit;
using Xunit.Abstractions;
namespace UnitTestEx.Xunit.Test
{
/// <summary>
/// Exercises the shared <c>IHttpMock*</c> interfaces' default interface method ("DIM") convenience extras (e.g. <c>Headers</c>, an integer <c>Delay</c>, no-body/raw-JSON-string/
/// embedded-resource overloads) - not just the core abstract members - via interface-typed variables against Tier 1's <see cref="MockHttpClient"/>. These are not otherwise exercised
/// by <see cref="MockHttpClientTest"/> (which calls the concrete native API directly) nor by <see cref="ProductControllerTest.HttpMock_SharedInterface_ConfiguresIdenticallyAcrossTiers"/>
/// (which only proves the core members).
/// </summary>
public class HttpMockInterfaceTest : UnitTestBase
{
public HttpMockInterfaceTest(ITestOutputHelper output) : base(output) { }
[Fact]
public async Task Interface_Request_WithBody_TextDim_MatchesPlainTextBody()
{
var mcf = MockHttpClientFactory.Create();
IHttpMockClient client = mcf.CreateClient("XXX", new System.Uri("https://d365test"));
var stub = await client.Request(HttpMethod.Post, "echo")
.Times(Times.Once())
.WithBody("hello world") // DIM: single-arg overload, defaults to text/plain.
.Respond.WithAsync("received", HttpStatusCode.OK, MediaTypeNames.Text.Plain);
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsync("echo", new StringContent("hello world", Encoding.UTF8, MediaTypeNames.Text.Plain));
Assert.Equal(HttpStatusCode.OK, res.StatusCode);
Assert.Equal("received", await res.Content.ReadAsStringAsync());
await stub.VerifyAsync();
}
[Fact]
public async Task Interface_Request_WithJsonResourceBody_Dim_MatchesResourceContent()
{
var mcf = MockHttpClientFactory.Create();
IHttpMockClient client = mcf.CreateClient("XXX", new System.Uri("https://d365test"));
var stub = await client.Request(HttpMethod.Post, "products/xyz")
.Times(Times.Once())
.WithJsonResourceBody<HttpMockInterfaceTest>("MockHttpClientTest-UriAndBody_WithJsonResponse3.json") // DIM: matches request body against embedded resource content.
.Respond.WithJsonAsync(new { message = "matched" });
var hc = mcf.GetHttpClient("XXX");
var res = await hc.PostAsJsonAsync("products/xyz", new { first = "Bob", last = "Jane" });
Assert.Equal(HttpStatusCode.OK, res.StatusCode);
Assert.Equal("{\"message\":\"matched\"}", await res.Content.ReadAsStringAsync());
await stub.VerifyAsync();
}
[Fact]
public async Task Interface_Response_Extras_HeadersDelayAndNoContentWithAsync_Dim()
{
var mcf = MockHttpClientFactory.Create();
IHttpMockClient client = mcf.CreateClient("XXX", new System.Uri("https://d365test"));
var stub = await client.Request(HttpMethod.Get, "ping")
.Times(Times.Once())
.WithAnyBody()
.Respond
.Headers([new("x-custom", "abc"), new("x-other", "def")]) // DIM: iterates and calls the core Header member.
.Delay(50) // DIM: int milliseconds overload.
.WithAsync(HttpStatusCode.NoContent); // DIM: no-content overload.
var hc = mcf.GetHttpClient("XXX");
var sw = Stopwatch.StartNew();
var res = await hc.GetAsync("ping");
sw.Stop();
Assert.Equal(HttpStatusCode.NoContent, res.StatusCode);
Assert.Equal("abc", res.Headers.GetValues("x-custom").Single());
Assert.Equal("def", res.Headers.GetValues("x-other").Single());
Assert.True(sw.ElapsedMilliseconds >= 45, $"Actual elapsed milliseconds {sw.ElapsedMilliseconds}.");
await stub.VerifyAsync();
}
[Fact]
public async Task Interface_Response_WithJsonAsync_StringAndResource_Dim()
{
var mcf = MockHttpClientFactory.Create();
IHttpMockClient client = mcf.CreateClient("XXX", new System.Uri("https://d365test"));
var stub1 = await client.Request(HttpMethod.Get, "raw-json")
.Times(Times.Once())
.WithAnyBody()
.Respond.WithJsonAsync("{\"literal\":true}"); // DIM: raw JSON string overload.
var stub2 = await client.Request(HttpMethod.Get, "resource-json")
.Times(Times.Once())
.WithAnyBody()
.Respond.WithJsonResourceAsync<HttpMockInterfaceTest>("MockHttpClientTest-UriAndBody_WithJsonResponse3.json", HttpStatusCode.Accepted); // DIM: embedded resource overload.
var hc = mcf.GetHttpClient("XXX");
var res1 = await hc.GetAsync("raw-json");
Assert.Equal(HttpStatusCode.OK, res1.StatusCode);
Assert.Equal("{\"literal\":true}", await res1.Content.ReadAsStringAsync());
var res2 = await hc.GetAsync("resource-json");
Assert.Equal(HttpStatusCode.Accepted, res2.StatusCode);
Assert.Equal("{\"first\":\"Bob\",\"last\":\"Jane\"}", await res2.Content.ReadAsStringAsync());
await stub1.VerifyAsync();
await stub2.VerifyAsync();
}
[Fact]
public async Task Interface_Client_WithRequestsFromResourceAsync_Dim_LoadsStubsFromYaml()
{
var mcf = MockHttpClientFactory.Create();
IHttpMockClient client = mcf.CreateClient("XXX", new System.Uri("https://d365test"));
await client.WithRequestsFromResourceAsync<HttpMockInterfaceTest>("HttpMockInterfaceTest-mock.unittestex.yaml"); // DIM: shared YAML/JSON resource loader.
var hc = mcf.GetHttpClient("XXX");
var res1 = await hc.PostAsJsonAsync("products/xyz", new { product = "xyz", quantity = 1, stamp = "does-not-matter" });
Assert.Equal(HttpStatusCode.Accepted, res1.StatusCode);
Assert.Equal("{\"product\":\"xyz\",\"quantity\":1}", (await res1.Content.ReadAsStringAsync()).Trim());
var res2 = await hc.GetAsync("people/123");
Assert.Equal(HttpStatusCode.OK, res2.StatusCode);
Assert.Equal("abc", res2.Headers.GetValues("x-blah").Single());
var res3 = await hc.GetAsync("sequence");
Assert.Equal("{\"seq\":1}", (await res3.Content.ReadAsStringAsync()).Trim());
var res4 = await hc.GetAsync("sequence");
Assert.Equal("{\"seq\":2}", (await res4.Content.ReadAsStringAsync()).Trim());
}
[Fact]
public async Task Interface_ResponseSequenceItem_Extras_Dim()
{
var mcf = MockHttpClientFactory.Create();
IHttpMockClient client = mcf.CreateClient("XXX", new System.Uri("https://d365test"));
var stub = await client.Request(HttpMethod.Get, "sequence")
.WithAnyBody()
.Respond.WithSequenceAsync(seq =>
{
seq.Respond()
.Headers([new("x-seq", "1")]) // DIM: iterates and calls the core Header member.
.Delay(10) // DIM: int milliseconds overload.
.With(HttpStatusCode.NoContent); // DIM: no-content overload.
seq.Respond().WithJson("{\"raw\":true}", HttpStatusCode.OK); // DIM: raw JSON string overload.
seq.Respond().WithJsonResource<HttpMockInterfaceTest>("MockHttpClientTest-UriAndBody_WithJsonResponse3.json", HttpStatusCode.Accepted); // DIM: embedded resource overload.
});
var hc = mcf.GetHttpClient("XXX");
var res1 = await hc.GetAsync("sequence");
Assert.Equal(HttpStatusCode.NoContent, res1.StatusCode);
Assert.Equal("1", res1.Headers.GetValues("x-seq").Single());
var res2 = await hc.GetAsync("sequence");
Assert.Equal(HttpStatusCode.OK, res2.StatusCode);
Assert.Equal("{\"raw\":true}", await res2.Content.ReadAsStringAsync());
var res3 = await hc.GetAsync("sequence");
Assert.Equal(HttpStatusCode.Accepted, res3.StatusCode);
Assert.Equal("{\"first\":\"Bob\",\"last\":\"Jane\"}", await res3.Content.ReadAsStringAsync());
await stub.VerifyAsync();
}
}
}