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

Aldaviva / Unfucked / 19128584806

06 Nov 2025 07:47AM UTC coverage: 0.396% (-39.5%) from 39.923%
19128584806

push

github

Aldaviva
DateTime: added IsBefore and IsAfter for ZonedDateTime, not just OffsetDateTime. DI: Allow super registration for keyed services; fixed super registration of a hosted service causing an infinite recursion during injection. STUN: updated fallback server list, most of which have gone offline (including Google, confusingly); made HttpClient optional.

2 of 1605 branches covered (0.12%)

0 of 55 new or added lines in 2 files covered. (0.0%)

945 existing lines in 35 files now uncovered.

9 of 2272 relevant lines covered (0.4%)

0.04 hits per line

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

0.0
/HTTP/UnfuckedWebTarget.Responses.cs
1
using System.Net;
2
using System.Net.Http.Headers;
3
using System.Runtime.Serialization;
4
using System.Text;
5
using Unfucked.HTTP.Config;
6
using Unfucked.HTTP.Exceptions;
7
using Unfucked.HTTP.Serialization;
8
using NotSupportedException = Unfucked.HTTP.Exceptions.NotSupportedException;
9

10
namespace Unfucked.HTTP;
11

12
public partial class WebTarget {
13

UNCOV
14
    private static readonly IReadOnlyList<MessageBodyReader> DEFAULT_MESSAGE_BODY_READERS = [
×
UNCOV
15
        new StreamBodyReader(),
×
UNCOV
16
        new ByteArrayBodyReader(),
×
UNCOV
17
        new ByteArrayBodyReader.MemoryBodyReader(),
×
UNCOV
18
        new ByteArrayBodyReader.ReadOnlyMemoryBodyReader(),
×
UNCOV
19
        new StringBodyReader(),
×
UNCOV
20
        new JsonBodyReader(),
×
UNCOV
21
        new XmlBodyReader.XDocumentReader(),
×
UNCOV
22
        new XmlBodyReader.XmlDocumentReader(),
×
UNCOV
23
        new XmlBodyReader.XPathReader(),
×
UNCOV
24
        new XmlBodyReader()
×
UNCOV
25
    ];
×
26

27
    /// <exception cref="ProcessingException">response parsing failed</exception>
28
    /// <exception cref="WebApplicationException">the response status code was not succesful, and <see cref="PropertyKey.ThrowOnUnsuccessfulStatusCode"/> was left enabled</exception>
29
    private async Task<T> ParseResponseBody<T>(HttpResponseMessage response, CancellationToken cancellationToken) {
UNCOV
30
        if (!Property(PropertyKey.ThrowOnUnsuccessfulStatusCode, out bool value) || value) {
×
UNCOV
31
            await ThrowIfUnsuccessful(response, cancellationToken).ConfigureAwait(false);
×
32
        }
33

UNCOV
34
        MediaTypeHeaderValue? responseContentType = response.Content.Headers.ContentType;
×
UNCOV
35
        Encoding?             responseEncoding    = null;
×
36
        try {
UNCOV
37
            responseEncoding = responseContentType?.CharSet is { } responseEncodingName ? Encoding.GetEncoding(responseEncodingName) : null;
×
UNCOV
38
        } catch (ArgumentException) { }
×
39

UNCOV
40
        IEnumerable<MessageBodyReader> messageBodyReaders = (clientConfig?.MessageBodyReaders ?? []).Concat(DEFAULT_MESSAGE_BODY_READERS).ToList();
×
41

UNCOV
42
        foreach (MessageBodyReader reader in messageBodyReaders) {
×
43
            // not sure if the read stream needs to be rewound between attempts
UNCOV
44
            if (reader.CanRead<T>(responseContentType?.MediaType, null) && !cancellationToken.IsCancellationRequested) {
×
45
                try {
UNCOV
46
                    return await reader.Read<T>(response.Content, responseEncoding, clientConfig, cancellationToken).ConfigureAwait(false);
×
47
                } catch (Exception e) when (e is not OutOfMemoryException) {
×
48
                    throw new ProcessingException(e, await HttpExceptionParams.FromResponse(response, cancellationToken).ConfigureAwait(false));
×
49
                }
50
            }
51
        }
52

53
        await response.Content.LoadIntoBufferAsync().ConfigureAwait(false);
×
54

55
        /*
56
         * Don't dispose the bodyStream yet, because it's a buffered shared instance in the HttpContent at this point and must be read multiple times by message body readers below who each call
57
         * ReadAsStreamAsync(). It will get disposed in Get<T>() and similar by DisposeIfNotStream().
58
         */
59
#if NET5_0_OR_GREATER
60
        Stream bodyStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
61
#else
62
        Stream bodyStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
63
#endif
64
        using StreamReader bodyReader   = new(bodyStream, responseEncoding ?? Encoding.UTF8, true);
×
65
        char[]             prefixBuffer = new char[32];
×
66

67
        int prefixSize;
68
#if NETCOREAPP2_1_OR_GREATER
69
        try {
70
            prefixSize = await bodyReader.ReadAsync(prefixBuffer.AsMemory(), cancellationToken).ConfigureAwait(false);
71
        } catch (OperationCanceledException e) {
72
            throw new ProcessingException(e, await HttpExceptionParams.FromResponse(response, CancellationToken.None).ConfigureAwait(false));
73
        }
74
#else
75
        prefixSize = await bodyReader.ReadAsync(prefixBuffer, 0, prefixBuffer.Length).ConfigureAwait(false);
76
#endif
77
        string bodyPrefix = new string(prefixBuffer, 0, prefixSize).Trim();
×
78
        bodyStream.Position = 0;
×
79

80
        foreach (MessageBodyReader reader in messageBodyReaders) {
×
81
            if (reader.CanRead<T>(responseContentType?.MediaType, bodyPrefix) && !cancellationToken.IsCancellationRequested) {
×
82
                try {
83
                    return await reader.Read<T>(response.Content, responseEncoding, clientConfig, cancellationToken).ConfigureAwait(false);
×
84
                } catch (Exception e) when (e is not OutOfMemoryException) {
×
85
                    throw new ProcessingException(e, await HttpExceptionParams.FromResponse(response, cancellationToken).ConfigureAwait(false));
×
86
                }
87
            }
88
        }
89

90
        HttpExceptionParams p = await HttpExceptionParams.FromResponse(response, cancellationToken).ConfigureAwait(false);
×
91
        throw new ProcessingException(
×
92
            new SerializationException($"Could not determine content type of response body to deserialize (URI: {p.RequestUrl}, Content-Type: {responseContentType}, .NET type: {typeof(T)})"), p);
×
UNCOV
93
    }
×
94

95
    /// <exception cref="WebApplicationException">the response status code was not successful</exception>
96
    internal static async Task ThrowIfUnsuccessful(HttpResponseMessage response, CancellationToken cancellationToken) {
UNCOV
97
        if (!response.IsSuccessStatusCode) {
×
98
            HttpStatusCode      statusCode = response.StatusCode;
×
99
            string              reason     = response.ReasonPhrase ?? statusCode.ToString();
×
100
            HttpExceptionParams p          = await HttpExceptionParams.FromResponse(response, cancellationToken).ConfigureAwait(false);
×
101
            response.Dispose();
×
102
            throw statusCode switch {
×
103
                HttpStatusCode.BadRequest           => new BadRequestException(reason, p),
×
104
                HttpStatusCode.Unauthorized         => new NotAuthorizedException(reason, p),
×
105
                HttpStatusCode.Forbidden            => new ForbiddenException(reason, p),
×
106
                HttpStatusCode.NotFound             => new NotFoundException(reason, p),
×
107
                HttpStatusCode.MethodNotAllowed     => new NotAllowedException(reason, p),
×
108
                HttpStatusCode.NotAcceptable        => new NotAcceptableException(reason, p),
×
109
                HttpStatusCode.UnsupportedMediaType => new NotSupportedException(reason, p),
×
110
                HttpStatusCode.InternalServerError  => new InternalServerErrorException(reason, p),
×
111
                HttpStatusCode.ServiceUnavailable   => new ServiceUnavailableException(reason, p),
×
112

×
113
                >= HttpStatusCode.MultipleChoices and < HttpStatusCode.BadRequest     => new RedirectionException(statusCode, response.Headers.Location, reason, p),
×
114
                >= HttpStatusCode.BadRequest and < HttpStatusCode.InternalServerError => new ClientErrorException(statusCode, reason, p),
×
115
                >= HttpStatusCode.InternalServerError and < (HttpStatusCode) 600      => new ServerErrorException(statusCode, reason, p),
×
116

×
117
                _ => new WebApplicationException(statusCode, reason, p)
×
118
            };
×
119
        }
UNCOV
120
    }
×
121

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

© 2026 Coveralls, Inc