|
7 | 7 | using System.Drawing; |
8 | 8 | using System.Dynamic; |
9 | 9 | using System.Globalization; |
| 10 | +using System.Net; |
| 11 | +using System.Net.Http.Headers; |
| 12 | +using System.Text; |
10 | 13 | using Microsoft.EntityFrameworkCore; |
11 | 14 |
|
12 | 15 | namespace FastCloner.Tests; |
@@ -304,6 +307,118 @@ public void Dynamic_With_Collection_Clone() |
304 | 307 | }); |
305 | 308 | } |
306 | 309 |
|
| 310 | + [Test] |
| 311 | + public void HttpRequest_Clone() |
| 312 | + { |
| 313 | + // Arrange |
| 314 | + HttpRequestMessage original = new HttpRequestMessage |
| 315 | + { |
| 316 | + Method = HttpMethod.Post, |
| 317 | + RequestUri = new Uri("https://api.example.com/data"), |
| 318 | + Version = new Version(2, 0), |
| 319 | + Content = new StringContent( |
| 320 | + "{\"key\":\"value\"}", |
| 321 | + Encoding.UTF8, |
| 322 | + "application/json") |
| 323 | + }; |
| 324 | + |
| 325 | + original.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 326 | + original.Headers.Add("Custom-Header", "test-value"); |
| 327 | + original.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "test-token"); |
| 328 | + |
| 329 | + // Act |
| 330 | + HttpRequestMessage? cloned = FastCloner.DeepClone(original); |
| 331 | + |
| 332 | + // Assert |
| 333 | + Assert.Multiple(() => |
| 334 | + { |
| 335 | + Assert.That(cloned.Method, Is.EqualTo(HttpMethod.Post), "Method should be copied"); |
| 336 | + Assert.That(cloned.RequestUri?.ToString(), Is.EqualTo("https://api.example.com/data"), "URI should be copied"); |
| 337 | + Assert.That(cloned.Version, Is.EqualTo(new Version(2, 0)), "Version should be copied"); |
| 338 | + |
| 339 | + Assert.That(cloned.Headers.Accept.First().MediaType, Is.EqualTo("application/json"), "Accept header should be copied"); |
| 340 | + Assert.That(cloned.Headers.GetValues("Custom-Header").First(), Is.EqualTo("test-value"), "Custom header should be copied"); |
| 341 | + Assert.That(cloned.Headers.Authorization?.Scheme, Is.EqualTo("Bearer"), "Authorization scheme should be copied"); |
| 342 | + Assert.That(cloned.Headers.Authorization?.Parameter, Is.EqualTo("test-token"), "Authorization parameter should be copied"); |
| 343 | + |
| 344 | + Assert.That(cloned.Content, Is.Not.Null, "Content should be cloned"); |
| 345 | + Assert.That(cloned.Content, Is.TypeOf<StringContent>(), "Content type should be preserved"); |
| 346 | + |
| 347 | + string originalContent = original.Content.ReadAsStringAsync().Result; |
| 348 | + string clonedContent = cloned.Content.ReadAsStringAsync().Result; |
| 349 | + Assert.That(clonedContent, Is.EqualTo(originalContent), "Content value should be copied"); |
| 350 | + Assert.That(cloned.Content.Headers.ContentType?.MediaType, Is.EqualTo("application/json"), "Content-Type should be copied"); |
| 351 | + }); |
| 352 | + } |
| 353 | + |
| 354 | + [Test] |
| 355 | + public void HttpRequest_With_MultipartContent_Clone() |
| 356 | + { |
| 357 | + // Arrange |
| 358 | + HttpRequestMessage original = new HttpRequestMessage |
| 359 | + { |
| 360 | + Method = HttpMethod.Post, |
| 361 | + RequestUri = new Uri("https://api.example.com/upload") |
| 362 | + }; |
| 363 | + |
| 364 | + MultipartFormDataContent multipartContent = new MultipartFormDataContent(); |
| 365 | + |
| 366 | + StringContent stringContent = new StringContent("text data", Encoding.UTF8); |
| 367 | + multipartContent.Add(stringContent, "text"); |
| 368 | + |
| 369 | + byte[] binaryData = "binary data"u8.ToArray(); |
| 370 | + ByteArrayContent byteContent = new ByteArrayContent(binaryData); |
| 371 | + byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); |
| 372 | + multipartContent.Add(byteContent, "file", "test.bin"); |
| 373 | + |
| 374 | + original.Content = multipartContent; |
| 375 | + |
| 376 | + // Act |
| 377 | + HttpRequestMessage? cloned = FastCloner.DeepClone(original); |
| 378 | + |
| 379 | + // Assert |
| 380 | + Assert.Multiple(() => |
| 381 | + { |
| 382 | + Assert.That(cloned.Content, Is.TypeOf<MultipartFormDataContent>(), "Content type should be preserved"); |
| 383 | + |
| 384 | + MultipartFormDataContent? originalMultipart = (MultipartFormDataContent)original.Content; |
| 385 | + MultipartFormDataContent? clonedMultipart = (MultipartFormDataContent)cloned.Content; |
| 386 | + |
| 387 | + string originalParts = originalMultipart.ReadAsStringAsync().Result; |
| 388 | + string clonedParts = clonedMultipart.ReadAsStringAsync().Result; |
| 389 | + |
| 390 | + Assert.That(clonedParts, Is.EqualTo(originalParts), "Multipart content should be identical"); |
| 391 | + Assert.That(clonedMultipart.Headers.ContentType?.Parameters.First(p => p.Name == "boundary").Value, Is.Not.Null, "Boundary should be present"); |
| 392 | + }); |
| 393 | + } |
| 394 | + |
| 395 | + [Test] |
| 396 | + public void HttpRequest_With_Handlers_Clone() |
| 397 | + { |
| 398 | + // Arrange |
| 399 | + HttpRequestMessage original = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com"); |
| 400 | + HttpClientHandler handler = new HttpClientHandler |
| 401 | + { |
| 402 | + AllowAutoRedirect = false, |
| 403 | + AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate, |
| 404 | + UseCookies = false |
| 405 | + }; |
| 406 | + |
| 407 | + original.Properties.Add("AllowAutoRedirect", handler.AllowAutoRedirect); |
| 408 | + original.Properties.Add("AutomaticDecompression", handler.AutomaticDecompression); |
| 409 | + original.Properties.Add("UseCookies", handler.UseCookies); |
| 410 | + |
| 411 | + HttpRequestMessage? cloned = FastCloner.DeepClone(original); |
| 412 | + |
| 413 | + Assert.Multiple(() => |
| 414 | + { |
| 415 | + Assert.That(cloned.Properties, Is.Not.Empty, "Properties should be copied"); |
| 416 | + Assert.That(cloned.Properties["AllowAutoRedirect"], Is.EqualTo(false), "Handler property should be copied"); |
| 417 | + Assert.That(cloned.Properties["AutomaticDecompression"], Is.EqualTo(DecompressionMethods.GZip | DecompressionMethods.Deflate), "Handler compression settings should be copied"); |
| 418 | + Assert.That(cloned.Properties["UseCookies"], Is.EqualTo(false), "Handler cookie settings should be copied"); |
| 419 | + }); |
| 420 | + } |
| 421 | + |
307 | 422 | [Test] |
308 | 423 | public void Dynamic_With_Dictionary_Clone() |
309 | 424 | { |
|
0 commit comments