-
-
Notifications
You must be signed in to change notification settings - Fork 132
fix: preserve original HTTP mock request content #6731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tests/TUnit.Mocks.Http.Tests/RequestContentPreservationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| using System.Net; | ||
| using System.Net.Http.Headers; | ||
| using System.Text; | ||
|
|
||
| namespace TUnit.Mocks.Http.Tests; | ||
|
|
||
| public class RequestContentPreservationTests | ||
| { | ||
| [Test] | ||
| [Arguments("binary")] | ||
| [Arguments("text")] | ||
| [Arguments("multipart")] | ||
| public async Task ResponseFactoryReceivesOriginalContent(string contentKind) | ||
| { | ||
| using var handler = new MockHttpHandler(); | ||
| using var client = handler.CreateClient("https://example.test"); | ||
| using var content = CreateContent(contentKind); | ||
| content.Headers.Add("X-Body-Tag", "original"); | ||
| var originalBytes = await content.ReadAsByteArrayAsync(); | ||
| var originalContentType = content.Headers.ContentType?.ToString(); | ||
| HttpContent? factoryContent = null; | ||
|
|
||
| handler.OnPost("/upload").RespondWith(request => | ||
| { | ||
| factoryContent = request.Content; | ||
| return new HttpResponseMessage(HttpStatusCode.OK); | ||
| }); | ||
|
|
||
| using var response = await client.PostAsync("/upload", content); | ||
| var factoryBytes = await factoryContent!.ReadAsByteArrayAsync(); | ||
|
|
||
| using (Assert.Multiple()) | ||
| { | ||
| await Assert.That(Convert.ToHexString(factoryBytes)).IsEqualTo(Convert.ToHexString(originalBytes)); | ||
| await Assert.That(factoryContent.Headers.ContentType?.ToString()).IsEqualTo(originalContentType); | ||
| await Assert.That(factoryContent.Headers.Contains("X-Body-Tag")).IsTrue(); | ||
| await Assert.That(factoryContent).IsSameReferenceAs(content); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task ResponseFactoryCanRereadNonSeekableContent() | ||
| { | ||
| using var handler = new MockHttpHandler(); | ||
| using var client = handler.CreateClient("https://example.test"); | ||
| byte[] bytes = [0x00, 0xff, 0x80, 0x01]; | ||
| using var stream = new NonSeekableStream(bytes); | ||
| using var content = new StreamContent(stream); | ||
| content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); | ||
| Task<byte[]>? factoryRead = null; | ||
|
|
||
| handler.OnPost("/upload").RespondWith(request => | ||
| { | ||
| factoryRead = request.Content!.ReadAsByteArrayAsync(); | ||
| return new HttpResponseMessage(HttpStatusCode.OK); | ||
| }); | ||
|
|
||
| using var response = await client.PostAsync("/upload", content); | ||
| var factoryBytes = await factoryRead!; | ||
|
|
||
| await Assert.That(Convert.ToHexString(factoryBytes)).IsEqualTo(Convert.ToHexString(bytes)); | ||
| } | ||
|
|
||
| private static HttpContent CreateContent(string contentKind) | ||
| { | ||
| return contentKind switch | ||
| { | ||
| "binary" => new ByteArrayContent([0x00, 0xff, 0x80, 0x01]), | ||
| "text" => new StringContent("café", Encoding.Unicode, "text/plain"), | ||
| "multipart" => new MultipartFormDataContent("test-boundary") | ||
| { | ||
| { new ByteArrayContent([0x00, 0xff, 0x80, 0x01]), "file", "upload.bin" } | ||
| }, | ||
| _ => throw new ArgumentOutOfRangeException(nameof(contentKind)) | ||
| }; | ||
| } | ||
|
|
||
| private sealed class NonSeekableStream(byte[] bytes) : MemoryStream(bytes) | ||
| { | ||
| public override bool CanSeek => false; | ||
|
|
||
| public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: thomhurst/TUnit
Length of output: 47711
🏁 Script executed:
Repository: thomhurst/TUnit
Length of output: 50371
🤖 get_repo_knowledge executed:
get_repo_knowledge thomhurst/TUnit /tmp/coderabbit-repo-knowledge/thomhurst-tunit-199e8fbe/learnings /tmp/coderabbit-repo-knowledge/thomhurst-tunit-199e8fbe/conventionsLength of output: 3346
🏁 Script executed:
Repository: thomhurst/TUnit
Length of output: 17240
🏁 Script executed:
Repository: thomhurst/TUnit
Length of output: 1472
🏁 Script executed:
Repository: thomhurst/TUnit
Length of output: 1804
Run the HTTP preservation tests in both TUnit modes.
TUnit.Mocks.Http.Testsuses source-generated execution by default.scripts/run-reflection-tests.ps1runs onlytests/TUnit.TestProject, so these tests are not covered with--reflection. Add a targeted reflection run for bothResponseFactoryReceivesOriginalContentandResponseFactoryCanRereadNonSeekableContent.🤖 Prompt for AI Agents
Source: Coding guidelines