• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Aldaviva / Unfucked / 41068530179

30 Oct 2026 02:03PM UTC coverage: 46.159% (+0.1%) from 46.063%
41068530179

push

github

Aldaviva
HTTP: revert making all MessageBodyReaders take lots of arguments and not have access to the HttpResponseMessage or HttpContent just so they would always be forced to use the PartiallyBufferedStream, because most readers don't sniff body prefixes (only XML and JSON object mapping readers do), and it prevents the rest of them from using the optimized methods on HttpContent to read the body as a byte array or string. Solved by making a richer type hierarchy where the prefix sniffing lives in an abstract superclass that implements MessageBodyReader, and WebTarget is aware of it and can call it only when it's actually trying to detect prefixes. This has the added benefit that non-prefix-aware readers are skipped if they've already mismatched and won't be re-asked if they can handle the response when in the second phase prefix sniffing pass of deserialization.

838 of 2337 branches covered (35.86%)

44 of 67 new or added lines in 9 files covered. (65.67%)

2 existing lines in 2 files now uncovered.

1454 of 3150 relevant lines covered (46.16%)

151.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/HTTP/Serialization/HtmlDocumentReader.cs
1
using AngleSharp;
2
using AngleSharp.Dom;
3
using AngleSharp.Io;
4
using System.Net.Http.Headers;
5
using System.Text;
6
using Unfucked.Caching;
7
using Unfucked.HTTP.Config;
8

9
namespace Unfucked.HTTP.Serialization;
10

11
public sealed class HtmlDocumentReader: AbstractMessageBodyReader {
12

13
    private static readonly InMemoryCache<string, IBrowsingContext> BROWSERS_BY_USER_AGENT = new(new CacheOptions { ExpireAfterRead = new TimeSpan(15) }, (userAgent, _) =>
×
14
        new ValueTask<IBrowsingContext>(BrowsingContext.New(Configuration.Default.With(new DefaultHttpRequester(userAgent)).WithDefaultLoader())));
×
15

16
    static HtmlDocumentReader() {
17
        BROWSERS_BY_USER_AGENT.Removal += (_, _, value, _) => value.Dispose();
×
18
    }
×
19

NEW
20
    public override bool CanRead<T>(MediaTypeHeaderValue? contentType) => typeof(T) == typeof(IDocument);
×
21

22
    public override async Task<T> Read<T>(HttpResponseMessage response, Encoding? responseEncoding, Configurable? clientConfig, CancellationToken cancellationToken) {
NEW
23
        IBrowsingContext browser = await BROWSERS_BY_USER_AGENT.Get(response.RequestMessage?.Headers.UserAgent.ToString() ?? string.Empty, cancellationToken: cancellationToken).ConfigureAwait(false);
×
24

NEW
25
        Stream bodyStream = await GetBodyStream(response, cancellationToken);
×
26

27
        return (T) await browser.OpenAsync(virtualResponse => {
×
NEW
28
            if (response.RequestMessage?.RequestUri is {} uri) {
×
29
                virtualResponse.Address(uri);
×
30
            }
×
31

×
32
            virtualResponse
×
NEW
33
                .Status(response.StatusCode)
×
NEW
34
                .Headers(response.Headers.Concat(response.Content.Headers).ToDictionary(pair => pair.Key, pair => pair.Value.Join(", ")))
×
NEW
35
                .Content(bodyStream, true);
×
36
        }, cancellationToken).ConfigureAwait(false);
×
37
    }
×
38

39
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc