AngleSharp by AngleSharp

<PackageReference Include="AngleSharp" Version="0.9.9.2" />

 VirtualResponse

The virtual response class.
using AngleSharp.Extensions; using System; using System.Collections.Generic; using System.IO; using System.Net; namespace AngleSharp.Network { public class VirtualResponse : IResponse, IDisposable { private Url _address; private HttpStatusCode _status; private Dictionary<string, string> _headers; private Stream _content; private bool _dispose; Url IResponse.Address { get { return _address; } } Stream IResponse.Content { get { return _content; } } IDictionary<string, string> IResponse.Headers { get { return _headers; } } HttpStatusCode IResponse.StatusCode { get { return _status; } } private VirtualResponse() { _address = Url.Create("http://localhost/"); _status = HttpStatusCode.OK; _headers = new Dictionary<string, string>(); _content = Stream.Null; _dispose = false; } public static IResponse Create(Action<VirtualResponse> request) { VirtualResponse virtualResponse = new VirtualResponse(); request(virtualResponse); return virtualResponse; } public VirtualResponse Address(Url url) { _address = url; return this; } public VirtualResponse Address(string address) { return Address(Url.Create(address ?? string.Empty)); } public VirtualResponse Address(Uri url) { return Address(Url.Convert(url)); } public VirtualResponse Status(HttpStatusCode code) { _status = code; return this; } public VirtualResponse Status(int code) { return Status((HttpStatusCode)code); } public VirtualResponse Header(string name, string value) { _headers[name] = value; return this; } public VirtualResponse Headers(object obj) { Dictionary<string, string> headers = obj.ToDictionary(); return Headers(headers); } public VirtualResponse Headers(IDictionary<string, string> headers) { foreach (KeyValuePair<string, string> header in headers) { Header(header.Key, header.Value); } return this; } public VirtualResponse Content(string text) { Release(); byte[] bytes = TextEncoding.Utf8.GetBytes(text); _content = new MemoryStream(bytes); _dispose = true; return this; } public VirtualResponse Content(Stream stream, bool shouldDispose = false) { Release(); _content = stream; _dispose = shouldDispose; return this; } private void Release() { if (_dispose) _content?.Dispose(); _dispose = false; _content = null; } void IDisposable.Dispose() { Release(); } } }