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

neon-sunset / U8String / 6005208900

28 Aug 2023 09:36PM UTC coverage: 18.096% (-0.2%) from 18.326%
6005208900

push

github

neon-sunset
feat: Extend NativeU8String and restructure solution to account for increased line count, add roadmap draft

134 of 1050 branches covered (0.0%)

Branch coverage included in aggregate %.

1058 of 1058 new or added lines in 25 files covered. (100.0%)

478 of 2332 relevant lines covered (20.5%)

35305.41 hits per line

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

0.0
/src/Extensions/Http/U8HttpExtensions.cs
1
using U8Primitives;
2

3
namespace System.Net.Http;
4

5
/// <summary>
6
/// Provides extension methods to integrate <see cref="U8String"/> with the .NET type system.
7
/// </summary>
8
public static class U8HttpExtensions
9
{
10
    // TODO: Consider using "validating stream" to interrupt the request as soon as invalid UTF-8 is detected. Or not?
11
    /// <inheritdoc cref="GetU8StringAsync(HttpClient, Uri?, CancellationToken)"/>
12
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
13
    public static Task<U8String> GetU8StringAsync(this HttpClient client, string? requestUri)
14
    {
15
        return client.GetU8StringAsync(CreateUri(requestUri));
×
16
    }
17

18
    /// <inheritdoc cref="GetU8StringAsync(HttpClient, Uri?, CancellationToken)"/>
19
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
20
    public static Task<U8String> GetU8StringAsync(this HttpClient client, Uri? requestUri)
21
    {
22
        return client.GetU8StringAsync(requestUri, CancellationToken.None);
×
23
    }
24

25
    /// <inheritdoc cref="GetU8StringAsync(HttpClient, Uri?, CancellationToken)"/>
26
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
27
    public static Task<U8String> GetU8StringAsync(
28
        this HttpClient client, string? requestUri, CancellationToken cancellationToken)
29
    {
30
        return client.GetU8StringAsync(CreateUri(requestUri), cancellationToken);
×
31
    }
32

33
    /// <summary>
34
    /// Sends a GET request to the specified Uri and returns the response body as a <see cref="U8String"/>.
35
    /// </summary>
36
    /// <param name="client">The <see cref="HttpClient"/> instance.</param>
37
    /// <param name="requestUri">The Uri the request is sent to.</param>
38
    /// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
39
    /// <returns>The task object representing the asynchronous operation.</returns>
40
    /// <exception cref="HttpRequestException">The HTTP response failed.</exception>
41
    /// <exception cref="TaskCanceledException">The request was canceled.</exception>
42
    /// <exception cref="FormatException">The response body is not a valid UTF-8 sequence.</exception>
43
    public static async Task<U8String> GetU8StringAsync(
44
        this HttpClient client, Uri? requestUri, CancellationToken cancellationToken)
45
    {
46
        var bytes = await client.GetByteArrayAsync(requestUri, cancellationToken);
×
47

48
        U8String.Validate(bytes);
×
49
        return new(bytes, 0, bytes.Length);
×
50
    }
×
51

52
    /// <inheritdoc cref="ReadAsU8StringAsync(HttpContent, CancellationToken)"/>
53
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
54
    public static Task<U8String> ReadAsU8StringAsync(this HttpContent content)
55
    {
56
        return content.ReadAsU8StringAsync(CancellationToken.None);
×
57
    }
58

59
    /// <summary>
60
    /// Serialize the HTTP content to a <see cref="U8String"/> as an asynchronous operation.
61
    /// </summary>
62
    /// <param name="content">The HTTP content to serialize.</param>
63
    /// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
64
    /// <returns>The task object representing the asynchronous operation.</returns>
65
    /// <exception cref="TaskCanceledException">The request was canceled.</exception>
66
    /// <exception cref="FormatException">The response body is not a valid UTF-8 sequence.</exception>
67
    public static async Task<U8String> ReadAsU8StringAsync(
68
        this HttpContent content, CancellationToken cancellationToken)
69
    {
70
        var bytes = await content.ReadAsByteArrayAsync(cancellationToken);
×
71

72
        U8String.Validate(bytes);
×
73
        return new(bytes, 0, bytes.Length);
×
74
    }
×
75

76
    static Uri? CreateUri(string? requestUri)
77
    {
78
        return !string.IsNullOrEmpty(requestUri)
×
79
            ? new(requestUri, UriKind.RelativeOrAbsolute)
×
80
            : null;
×
81
    }
82
}
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

© 2025 Coveralls, Inc