-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathHttpRequestMessageHelperTests.cs
189 lines (160 loc) · 6.68 KB
/
HttpRequestMessageHelperTests.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
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
185
186
187
188
189
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
using NFluent;
using WireMock.Http;
using WireMock.Models;
using WireMock.Types;
using WireMock.Util;
using Xunit;
namespace WireMock.Net.Tests.Http;
public class HttpRequestMessageHelperTests
{
private const string ClientIp = "::1";
[Fact]
public void HttpRequestMessageHelper_Create()
{
// Assign
var headers = new Dictionary<string, string[]> { { "x", new[] { "value-1" } } };
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, null, headers);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(message.Headers.GetValues("x")).ContainsExactly("value-1");
}
[Fact]
public async Task HttpRequestMessageHelper_Create_Bytes()
{
// Assign
var body = new BodyData
{
BodyAsBytes = Encoding.UTF8.GetBytes("hi"),
DetectedBodyType = BodyType.Bytes
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp, body);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(await message.Content.ReadAsByteArrayAsync().ConfigureAwait(false)).ContainsExactly(Encoding.UTF8.GetBytes("hi"));
}
[Fact]
public async Task HttpRequestMessageHelper_Create_Json()
{
// Assign
var body = new BodyData
{
BodyAsJson = new { x = 42 },
DetectedBodyType = BodyType.Json
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp, body);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(await message.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals("{\"x\":42}");
}
[Fact]
public async Task HttpRequestMessageHelper_Create_Json_With_ContentType_ApplicationJson()
{
// Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/json" } } };
var body = new BodyData
{
BodyAsJson = new { x = 42 },
DetectedBodyType = BodyType.Json
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp, body, headers);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(await message.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals("{\"x\":42}");
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/json");
}
[Fact]
public async Task HttpRequestMessageHelper_Create_Json_With_ContentType_ApplicationJson_UTF8()
{
// Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/json; charset=utf-8" } } };
var body = new BodyData
{
BodyAsJson = new { x = 42 },
DetectedBodyType = BodyType.Json
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "GET", ClientIp, body, headers);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(await message.Content.ReadAsStringAsync().ConfigureAwait(false)).Equals("{\"x\":42}");
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/json; charset=utf-8");
}
[Fact]
public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml()
{
// Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml" } } };
var body = new BodyData
{
BodyAsString = "<xml>hello</xml>",
DetectedBodyType = BodyType.String
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body, headers);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml");
}
[Fact]
public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml_UTF8()
{
// Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml; charset=UTF-8" } } };
var body = new BodyData
{
BodyAsString = "<xml>hello</xml>",
DetectedBodyType = BodyType.String
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body, headers);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml; charset=UTF-8");
}
[Fact]
public void HttpRequestMessageHelper_Create_String_With_ContentType_ApplicationXml_ASCII()
{
// Assign
var headers = new Dictionary<string, string[]> { { "Content-Type", new[] { "application/xml; charset=Ascii" } } };
var body = new BodyData
{
BodyAsString = "<xml>hello</xml>",
DetectedBodyType = BodyType.String
};
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), "PUT", ClientIp, body, headers);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
Check.That(message.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/xml; charset=Ascii");
}
[Theory]
[InlineData("HEAD", true)]
[InlineData("GET", false)]
[InlineData("PUT", false)]
[InlineData("POST", false)]
[InlineData("DELETE", false)]
[InlineData("TRACE", false)]
[InlineData("OPTIONS", false)]
[InlineData("CONNECT", false)]
[InlineData("PATCH", false)]
public void HttpRequestMessageHelper_Create_ContentLengthAllowedForMethod(string method, bool resultShouldBe)
{
// Arrange
var key = "Content-Length";
var value = 1234;
var headers = new Dictionary<string, string[]> { { key, new[] { "1234" } } };
var request = new RequestMessage(new UrlDetails("http://localhost/foo"), method, ClientIp, null, headers);
// Act
var message = HttpRequestMessageHelper.Create(request, "http://url");
// Assert
message.Content?.Headers.ContentLength.Should().Be(resultShouldBe ? value : null);
}
}