-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathServiceBusFunctionTest.cs
More file actions
125 lines (103 loc) · 5.26 KB
/
ServiceBusFunctionTest.cs
File metadata and controls
125 lines (103 loc) · 5.26 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
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using UnitTestEx.Function;
using UnitTestEx.Json;
using Xunit;
using Xunit.Abstractions;
namespace UnitTestEx.Xunit.Test
{
public class ServiceBusFunctionTest : UnitTestBase
{
public ServiceBusFunctionTest(ITestOutputHelper output) : base(output) { }
[Fact]
public void Object_Success()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Post, "person").WithJsonBody(new { firstName = "Bob", lastName = "Smith" }).Respond.With(HttpStatusCode.OK);
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run(new Person { FirstName = "Bob", LastName = "Smith" }, test.Logger))
.AssertSuccess();
mcf.VerifyAll();
}
[Fact]
public void Object_HttpClientError()
{
var mcf = MockHttpClientFactory.Create().UseJsonComparerOptions(new JsonElementComparerOptions { NullComparison = JsonElementComparison.Semantic });
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Post, "person").WithJsonBody(new { firstName = "Bob", lastName = (string)null }).Respond.With(HttpStatusCode.InternalServerError);
using var test = FunctionTester.Create<Startup>();
var r = test.ReplaceHttpClientFactory(mcf)
.Type<ServiceBusFunction>()
.Run(f => f.Run(new Person { FirstName = "Bob", LastName = (string)null }, test.Logger))
.AssertException<HttpRequestException>("Response status code does not indicate success: 500 (Internal Server Error).");
mcf.VerifyAll();
}
[Fact]
public void Object_ThrowsException()
{
var mcf = MockHttpClientFactory.Create();
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run(new Person { FirstName = null, LastName = "Smith" }, test.Logger))
.AssertException<InvalidOperationException>("First name is required.");
mcf.VerifyAll();
}
[Fact]
public void ServiceBusMessage_Success()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Post, "person").WithJsonBody(new { firstName = "Bob", lastName = "Smith" }).Respond.With(HttpStatusCode.OK);
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run2(test.CreateServiceBusMessageFromValue(new Person { FirstName = "Bob", LastName = "Smith" }), test.Logger))
.AssertSuccess();
mcf.VerifyAll();
}
[Fact]
public void ServiceBusMessage_HttpClientError()
{
var mcf = MockHttpClientFactory.Create().UseJsonSerializer(new Json.JsonSerializer(new JsonSerializerOptions(JsonSerializerDefaults.Web)));
mcf.CreateClient("XXX", new Uri("https://somesys"))
.Request(HttpMethod.Post, "person").WithJsonBody(new { firstName = "Bob", lastName = (string)null }).Respond.With(HttpStatusCode.InternalServerError);
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run2(test.CreateServiceBusMessageFromValue(new Person { FirstName = "Bob", LastName = (string)null }), test.Logger))
.AssertException<HttpRequestException>("Response status code does not indicate success: 500 (Internal Server Error).");
mcf.VerifyAll();
}
[Fact]
public void ServiceBusMessage_ThrowsException()
{
var mcf = MockHttpClientFactory.Create();
using var test = FunctionTester.Create<Startup>();
test.ReplaceHttpClientFactory(mcf)
.ServiceBusTrigger<ServiceBusFunction>()
.Run(f => f.Run2(test.CreateServiceBusMessageFromValue(new Person { FirstName = null, LastName = "Smith" }), test.Logger))
.AssertException<InvalidOperationException>("First name is required.");
mcf.VerifyAll();
}
[Fact]
public async Task ServiceProvider()
{
var mcf = MockHttpClientFactory.Create();
mcf.CreateClient("XXX", new Uri("https://somesys")).Request(HttpMethod.Get, "test").Respond.With("test output");
using var test = FunctionTester.Create<Startup>();
var hc = test.ReplaceHttpClientFactory(mcf)
.Services.GetService<IHttpClientFactory>().CreateClient("XXX");
var r = await hc.GetAsync("test");
Assert.NotNull(r);
Assert.Equal("test output", await r.Content.ReadAsStringAsync());
}
}
}