|
| 1 | +// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved. |
| 2 | +// ReactiveUI and Contributors licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using System.IO; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Refit.Testing; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Wraps a request body without reading it. Whichever way the reply code consumes the body, as a stream or by copying it |
| 15 | +/// out, the bytes pass through to the reader and are recorded up to the capture limit. |
| 16 | +/// </summary> |
| 17 | +internal sealed class CapturingContent : HttpContent |
| 18 | +{ |
| 19 | + /// <summary>The original request body.</summary> |
| 20 | + private readonly HttpContent _inner; |
| 21 | + |
| 22 | + /// <summary>The record filled as the body is read.</summary> |
| 23 | + private readonly CapturedRequestBody _capture; |
| 24 | + |
| 25 | + /// <summary>Initializes a new instance of the <see cref="CapturingContent"/> class, copying the original content headers.</summary> |
| 26 | + /// <param name="inner">The original request body.</param> |
| 27 | + /// <param name="capture">The record filled as the body is read.</param> |
| 28 | + internal CapturingContent(HttpContent inner, CapturedRequestBody capture) |
| 29 | + { |
| 30 | + _inner = inner; |
| 31 | + _capture = capture; |
| 32 | + foreach (var header in inner.Headers) |
| 33 | + { |
| 34 | + if (!string.Equals(header.Key, "Content-Length", StringComparison.OrdinalIgnoreCase)) |
| 35 | + { |
| 36 | + _ = Headers.TryAddWithoutValidation(header.Key, header.Value); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc/> |
| 42 | + protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context) |
| 43 | + { |
| 44 | + // The copy target belongs to the caller; the wrapper owns nothing and needs no disposal. |
| 45 | + await _inner.CopyToAsync(new CaptureStream(stream, _capture, ownsInner: false)).ConfigureAwait(false); |
| 46 | + _capture.Complete(); |
| 47 | + } |
| 48 | + |
| 49 | + /// <inheritdoc/> |
| 50 | + protected override async Task<Stream> CreateContentReadStreamAsync() => |
| 51 | + new CaptureStream(await _inner.ReadAsStreamAsync().ConfigureAwait(false), _capture, ownsInner: true); |
| 52 | + |
| 53 | + /// <inheritdoc/> |
| 54 | + protected override bool TryComputeLength(out long length) |
| 55 | + { |
| 56 | + var known = _inner.Headers.ContentLength; |
| 57 | + length = known.GetValueOrDefault(); |
| 58 | + return known.HasValue; |
| 59 | + } |
| 60 | + |
| 61 | + /// <inheritdoc/> |
| 62 | + protected override void Dispose(bool disposing) |
| 63 | + { |
| 64 | + _inner.Dispose(); |
| 65 | + base.Dispose(disposing); |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary>Passes reads or writes through to another stream and records the bytes that pass.</summary> |
| 69 | + /// <param name="inner">The stream read from or written to.</param> |
| 70 | + /// <param name="capture">The record to fill.</param> |
| 71 | + /// <param name="ownsInner"> |
| 72 | + /// Whether disposing the wrapper disposes <paramref name="inner"/>: true for a body read stream, false for a copy |
| 73 | + /// target owned by the caller. |
| 74 | + /// </param> |
| 75 | + internal sealed class CaptureStream(Stream inner, CapturedRequestBody capture, bool ownsInner) : Stream |
| 76 | + { |
| 77 | + /// <inheritdoc/> |
| 78 | + public override bool CanRead => inner.CanRead; |
| 79 | + |
| 80 | + /// <inheritdoc/> |
| 81 | + public override bool CanSeek => false; |
| 82 | + |
| 83 | + /// <inheritdoc/> |
| 84 | + public override bool CanWrite => inner.CanWrite; |
| 85 | + |
| 86 | + /// <inheritdoc/> |
| 87 | + public override long Length => throw new NotSupportedException(); |
| 88 | + |
| 89 | + /// <inheritdoc/> |
| 90 | + public override long Position |
| 91 | + { |
| 92 | + get => throw new NotSupportedException(); |
| 93 | + set => throw new NotSupportedException(); |
| 94 | + } |
| 95 | + |
| 96 | + /// <inheritdoc/> |
| 97 | + public override void Flush() => inner.Flush(); |
| 98 | + |
| 99 | + /// <inheritdoc/> |
| 100 | + public override Task FlushAsync(CancellationToken cancellationToken) => inner.FlushAsync(cancellationToken); |
| 101 | + |
| 102 | + /// <inheritdoc/> |
| 103 | + public override int Read(byte[] buffer, int offset, int count) => Record(buffer.AsSpan(offset, inner.Read(buffer, offset, count))); |
| 104 | + |
| 105 | +#if NET8_0_OR_GREATER |
| 106 | + /// <inheritdoc/> |
| 107 | + public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => |
| 108 | + ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); |
| 109 | + |
| 110 | + /// <inheritdoc/> |
| 111 | + public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| 112 | + { |
| 113 | + var read = await inner.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); |
| 114 | + return Record(buffer.Span[..read]); |
| 115 | + } |
| 116 | +#else |
| 117 | + /// <inheritdoc/> |
| 118 | + public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => |
| 119 | + Record(buffer.AsSpan(offset, await inner.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false))); |
| 120 | +#endif |
| 121 | + |
| 122 | + /// <inheritdoc/> |
| 123 | + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| 124 | + |
| 125 | + /// <inheritdoc/> |
| 126 | + public override void SetLength(long value) => throw new NotSupportedException(); |
| 127 | + |
| 128 | + /// <inheritdoc/> |
| 129 | + public override void Write(byte[] buffer, int offset, int count) |
| 130 | + { |
| 131 | + inner.Write(buffer, offset, count); |
| 132 | + capture.Append(buffer.AsSpan(offset, count)); |
| 133 | + } |
| 134 | + |
| 135 | +#if NET8_0_OR_GREATER |
| 136 | + /// <inheritdoc/> |
| 137 | + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) => |
| 138 | + WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask(); |
| 139 | + |
| 140 | + /// <inheritdoc/> |
| 141 | + public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) |
| 142 | + { |
| 143 | + await inner.WriteAsync(buffer, cancellationToken).ConfigureAwait(false); |
| 144 | + capture.Append(buffer.Span); |
| 145 | + } |
| 146 | +#else |
| 147 | + /// <inheritdoc/> |
| 148 | + public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| 149 | + { |
| 150 | + await inner.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); |
| 151 | + capture.Append(buffer.AsSpan(offset, count)); |
| 152 | + } |
| 153 | +#endif |
| 154 | + |
| 155 | + /// <inheritdoc/> |
| 156 | + protected override void Dispose(bool disposing) |
| 157 | + { |
| 158 | + if (ownsInner) |
| 159 | + { |
| 160 | + inner.Dispose(); |
| 161 | + } |
| 162 | + |
| 163 | + base.Dispose(disposing); |
| 164 | + } |
| 165 | + |
| 166 | + /// <summary>Records bytes just read, completing the record at the end of the body.</summary> |
| 167 | + /// <param name="read">The bytes just read; empty at the end of the body.</param> |
| 168 | + /// <returns>The number of bytes read.</returns> |
| 169 | + private int Record(ReadOnlySpan<byte> read) |
| 170 | + { |
| 171 | + if (read.IsEmpty) |
| 172 | + { |
| 173 | + capture.Complete(); |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + capture.Append(read); |
| 178 | + } |
| 179 | + |
| 180 | + return read.Length; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments