Skip to content

Commit 19241db

Browse files
committed
add HttpResponse tests
1 parent 9a39d4f commit 19241db

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

FastCloner.Tests/ArraysSpec.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public void StringArray_Casted_As_Object_Should_Be_Cloned()
6464
public void ByteArray_Should_Be_Cloned()
6565
{
6666
// checking that cached object correctly clones arrays of different length
67-
byte[] arr = Encoding.ASCII.GetBytes("test");
67+
byte[] arr = "test"u8.ToArray();
6868
byte[] cloned = arr.DeepClone();
6969
CollectionAssert.AreEqual(arr, cloned);
7070

71-
arr = Encoding.ASCII.GetBytes("test testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testte");
71+
arr = "test testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testtest testte"u8.ToArray();
7272
cloned = arr.DeepClone();
7373
CollectionAssert.AreEqual(arr, cloned);
7474
}

FastCloner.Tests/SpecificScenariosTest.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,113 @@ public void HttpRequest_With_Handlers_Clone()
419419
});
420420
}
421421

422+
[Test]
423+
public void HttpResponse_Clone()
424+
{
425+
// Arrange
426+
HttpResponseMessage original = new HttpResponseMessage
427+
{
428+
StatusCode = HttpStatusCode.OK,
429+
Version = new Version(2, 0),
430+
Content = new StringContent(
431+
"{\"result\":\"success\"}",
432+
Encoding.UTF8,
433+
"application/json"),
434+
ReasonPhrase = "Custom OK Message"
435+
};
436+
437+
original.Headers.CacheControl = new CacheControlHeaderValue { MaxAge = TimeSpan.FromHours(1) };
438+
original.Headers.Add("X-Custom-Response", "test-response");
439+
440+
// Act
441+
HttpResponseMessage? cloned = FastCloner.DeepClone(original);
442+
443+
// Assert
444+
Assert.Multiple(() =>
445+
{
446+
Assert.That(cloned.StatusCode, Is.EqualTo(HttpStatusCode.OK), "Status code should be copied");
447+
Assert.That(cloned.Version, Is.EqualTo(new Version(2, 0)), "Version should be copied");
448+
Assert.That(cloned.ReasonPhrase, Is.EqualTo("Custom OK Message"), "Reason phrase should be copied");
449+
450+
Assert.That(cloned.Headers.CacheControl?.MaxAge, Is.EqualTo(TimeSpan.FromHours(1)), "Cache control should be copied");
451+
Assert.That(cloned.Headers.GetValues("X-Custom-Response").First(), Is.EqualTo("test-response"), "Custom header should be copied");
452+
453+
string originalContent = original.Content.ReadAsStringAsync().Result;
454+
string clonedContent = cloned.Content.ReadAsStringAsync().Result;
455+
Assert.That(clonedContent, Is.EqualTo(originalContent), "Content should be copied");
456+
});
457+
}
458+
459+
[Test]
460+
public void HttpRequest_With_StreamContent_Clone()
461+
{
462+
// Arrange
463+
HttpRequestMessage original = new HttpRequestMessage(HttpMethod.Post, "https://api.example.com/stream");
464+
MemoryStream streamData = new MemoryStream("stream test data"u8.ToArray());
465+
StreamContent streamContent = new StreamContent(streamData);
466+
streamContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
467+
original.Content = streamContent;
468+
469+
// Act
470+
HttpRequestMessage? cloned = FastCloner.DeepClone(original);
471+
472+
// Assert
473+
Assert.Multiple(() =>
474+
{
475+
Assert.That(cloned.Content, Is.TypeOf<StreamContent>(), "Content type should be preserved");
476+
477+
string originalContent = original.Content.ReadAsStringAsync().Result;
478+
string clonedContent = cloned.Content.ReadAsStringAsync().Result;
479+
Assert.That(clonedContent, Is.EqualTo(originalContent), "Stream content should be copied");
480+
Assert.That(cloned.Content.Headers.ContentType?.MediaType, Is.EqualTo("text/plain"), "Content type should be copied");
481+
});
482+
}
483+
484+
[Test]
485+
public void HttpRequest_With_ComplexHeaders_Clone()
486+
{
487+
// Arrange
488+
HttpRequestMessage original = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com");
489+
490+
original.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json", 1.0));
491+
original.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml", 0.8));
492+
493+
original.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US", 1.0));
494+
original.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("cs-CZ", 0.8));
495+
496+
original.Headers.Add("If-Match", ["\"123\"", "\"456\""]);
497+
original.Headers.Add("X-Custom-Multi", ["value1", "value2"]);
498+
499+
// Act
500+
HttpRequestMessage? cloned = FastCloner.DeepClone(original);
501+
502+
// Assert
503+
Assert.Multiple(() =>
504+
{
505+
List<MediaTypeWithQualityHeaderValue> acceptHeaders = cloned.Headers.Accept.OrderBy(x => x.MediaType).ToList();
506+
Assert.That(acceptHeaders[0].MediaType, Is.EqualTo("application/json"), "First accept header should be copied");
507+
Assert.That(acceptHeaders[0].Quality, Is.EqualTo(1.0), "First accept header quality should be copied");
508+
Assert.That(acceptHeaders[1].MediaType, Is.EqualTo("text/xml"), "Second accept header should be copied");
509+
Assert.That(acceptHeaders[1].Quality, Is.EqualTo(0.8), "Second accept header quality should be copied");
510+
511+
List<StringWithQualityHeaderValue> languageHeaders = cloned.Headers.AcceptLanguage.OrderBy(x => x.Value).ToList();
512+
Assert.That(languageHeaders[0].Value, Is.EqualTo("cs-CZ"), "First language header should be copied");
513+
Assert.That(languageHeaders[0].Quality, Is.EqualTo(0.8), "First language header quality should be copied");
514+
Assert.That(languageHeaders[1].Value, Is.EqualTo("en-US"), "Second language header should be copied");
515+
Assert.That(languageHeaders[1].Quality, Is.EqualTo(1.0), "Second language header quality should be copied");
516+
517+
List<string> ifMatchValues = cloned.Headers.GetValues("If-Match").ToList();
518+
Assert.That(ifMatchValues, Has.Count.EqualTo(2), "If-Match headers count should match");
519+
Assert.That(ifMatchValues, Contains.Item("\"123\""), "First If-Match value should be copied");
520+
Assert.That(ifMatchValues, Contains.Item("\"456\""), "Second If-Match value should be copied");
521+
522+
List<string> customMultiValues = cloned.Headers.GetValues("X-Custom-Multi").ToList();
523+
Assert.That(customMultiValues, Has.Count.EqualTo(2), "Custom multi-value header count should match");
524+
Assert.That(customMultiValues, Contains.Item("value1"), "First custom multi-value should be copied");
525+
Assert.That(customMultiValues, Contains.Item("value2"), "Second custom multi-value should be copied");
526+
});
527+
}
528+
422529
[Test]
423530
public void Dynamic_With_Dictionary_Clone()
424531
{

0 commit comments

Comments
 (0)