-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathFluentHttpClientTest.cs
143 lines (120 loc) · 4.71 KB
/
FluentHttpClientTest.cs
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
using Microsoft.Extensions.DependencyInjection;
using Sketch7.MessagePack.MediaTypeFormatter;
using static FluentlyHttpClient.Test.ServiceTestUtil;
// ReSharper disable once CheckNamespace
namespace Test;
public class HttpClient
{
private readonly MessagePackMediaTypeFormatter _messagePackMediaTypeFormatter = new();
[Fact]
public async void Get_ShouldReturnContent()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp.When("https://sketch7.com/api/heroes/azmodan")
.Respond("application/json", "{ 'name': 'Azmodan' }");
var httpClient = GetNewClientFactory().CreateBuilder("sketch7")
.WithBaseUrl("https://sketch7.com")
.WithMockMessageHandler(mockHttp)
.Build();
var hero = await httpClient.Get<Hero>("/api/heroes/azmodan");
Assert.NotNull(hero);
Assert.Equal("Azmodan", hero.Name);
}
[Fact]
public async void Post_ShouldReturnContent()
{
var mockHttp = new MockHttpMessageHandler();
mockHttp.When(HttpMethod.Post, "https://sketch7.com/api/heroes/azmodan")
//.WithContent("{\"Title\":\"Lord of Sin\"}")
.With(request =>
{
var contentTask = request.Content.ReadAsAsync<Hero>();
contentTask.Wait();
return contentTask.Result.Title == "Lord of Sin";
})
.Respond("application/json", "{ 'name': 'Azmodan', 'title': 'Lord of Sin' }");
var httpClient = GetNewClientFactory().CreateBuilder("sketch7")
.WithBaseUrl("https://sketch7.com")
.WithMockMessageHandler(mockHttp)
.Build();
var hero = await httpClient.Post<Hero>("/api/heroes/azmodan", new
{
Title = "Lord of Sin"
});
Assert.NotNull(hero);
Assert.Equal("Lord of Sin", hero.Title);
}
[Fact]
public void CreateClient_ShouldInheritOptions()
{
var serviceProvider = CreateContainer().BuildServiceProvider();
var httpClientFactory = serviceProvider.GetRequiredService<IFluentHttpClientFactory>();
var clientBuilder = httpClientFactory.CreateBuilder("sketch7")
.WithBaseUrl("https://sketch7.com")
.WithHeader("locale", "en-GB")
.UseTimer()
.ConfigureFormatters(x => x.Formatters.Add(_messagePackMediaTypeFormatter))
.WithRequestBuilderDefaults(requestBuilder =>
{
requestBuilder.WithMethod(HttpMethod.Trace)
.WithUri("api/graphql")
.WithItem("error-mapping", "map this")
.WithItem("context", "user")
;
})
;
var httpClient = clientBuilder.Build();
var subClient = httpClient.CreateClient("subclient")
.WithRequestBuilderDefaults(x => x.WithItem("context", "reward"))
.WithHeader("locale", "de")
.WithHeader("country", "de")
.UseLogging()
.Build();
var httpClientRequest = httpClient.CreateRequest();
var subClientRequest = subClient.CreateRequest();
var httpClientLocale = httpClient.Headers.GetValues("locale").FirstOrDefault();
var subClientLocale = subClient.Headers.GetValues("locale").FirstOrDefault();
httpClient.Headers.TryGetValues("country", out var countryValues);
var subClientCountry = subClient.Headers.GetValues("country").FirstOrDefault();
Assert.Equal("sketch7", httpClient.Identifier);
Assert.Equal("sketch7.subclient", subClient.Identifier);
Assert.Equal("en-GB", httpClientLocale);
Assert.Equal("de", subClientLocale);
Assert.Null(countryValues?.FirstOrDefault());
Assert.Equal("de", subClientCountry);
Assert.Equal(httpClientRequest.HttpMethod, subClientRequest.HttpMethod);
Assert.Equal(httpClientRequest.Items["error-mapping"], subClientRequest.Items["error-mapping"]);
Assert.Equal("user", httpClientRequest.Items["context"]);
Assert.Equal("reward", subClientRequest.Items["context"]);
Assert.Equal(httpClient.Formatters.Count, subClient.Formatters.Count);
// todo: check middleware count?
Assert.Equal(2, httpClientFactory.Count);
}
[Fact]
public async void GraphQL_ShouldReturnContent()
{
const string query = "{hero {name,title}}";
const string operationName = "heroGet";
var mockHttp = new MockHttpMessageHandler();
mockHttp.When(HttpMethod.Post, "https://sketch7.com/api/graphql")
.With(request =>
{
var contentTask = request.Content.ReadAsAsync<GqlRequest>();
contentTask.Wait();
var result = contentTask.Result;
return result.Query == query
&& result.OperationName == operationName;
})
.Respond("application/json", "{ 'data': {'name': 'Azmodan', 'title': 'Lord of Sin' }}");
var httpClient = GetNewClientFactory().CreateBuilder("sketch7")
.WithBaseUrl("https://sketch7.com")
.WithRequestBuilderDefaults(requestBuilder => requestBuilder.WithUri("api/graphql"))
.WithMockMessageHandler(mockHttp)
.Build();
var response = await httpClient.CreateGqlRequest(query, operationName)
.ReturnAsGqlResponse<Hero>();
Assert.True(response.IsSuccessStatusCode);
Assert.NotNull(response.Data);
Assert.Equal("Lord of Sin", response.Data.Title);
}
}