| | | 1 | | #region Usings |
| | | 2 | | using System.Net.Http.Headers; |
| | | 3 | | #endregion |
| | | 4 | | |
| | | 5 | | namespace ZhEaIsNsAaBn.IntegrationTestingBases; |
| | | 6 | | |
| | | 7 | | /// <summary>HTTP helpers for integration tests: JSON request bodies, response reading and multipart uploads.</summary> |
| | | 8 | | public static class HttpTestUtilities |
| | | 9 | | { |
| | 3 | 10 | | private static readonly JsonSerializerOptions _jsonOptions = new() |
| | 3 | 11 | | { |
| | 3 | 12 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 3 | 13 | | WriteIndented = true |
| | 3 | 14 | | }; |
| | | 15 | | |
| | | 16 | | |
| | | 17 | | /// <summary>Serializes <paramref name="obj"/> to camelCase JSON content.</summary> |
| | | 18 | | /// <typeparam name="T">The value type.</typeparam> |
| | | 19 | | /// <param name="obj">The value to send.</param> |
| | | 20 | | /// <returns><c>application/json</c> content in UTF-8. The caller disposes it.</returns> |
| | | 21 | | public static StringContent ToJsonContent< T >( T obj ) |
| | 6 | 22 | | => new( JsonSerializer.Serialize( obj, _jsonOptions ), Encoding.UTF8, "application/json" ); |
| | | 23 | | |
| | | 24 | | /// <summary>Reads and deserializes a JSON response (camelCase names).</summary> |
| | | 25 | | /// <typeparam name="T">The expected body type.</typeparam> |
| | | 26 | | /// <param name="response">The response to read.</param> |
| | | 27 | | /// <param name="cancellationToken">Token to cancel the operation.</param> |
| | | 28 | | /// <returns>The deserialized body.</returns> |
| | | 29 | | /// <exception cref="InvalidOperationException">The body is empty, <c>null</c> or not valid JSON for <typeparamref n |
| | | 30 | | public static async Task<T> ReadResponseAsync< T >( HttpResponseMessage response, CancellationToken cancellationToke |
| | | 31 | | { |
| | 18 | 32 | | var content = await response.Content.ReadAsStringAsync( cancellationToken ).ConfigureAwait( false ); |
| | | 33 | | |
| | 21 | 34 | | if ( string.IsNullOrEmpty( content ) ) { throw new InvalidOperationException( "Response content is empty" ); } |
| | | 35 | | |
| | | 36 | | |
| | | 37 | | try |
| | | 38 | | { |
| | 15 | 39 | | return JsonSerializer.Deserialize<T>( content, _jsonOptions ) ?? throw new InvalidOperationException( "Deser |
| | | 40 | | } |
| | 3 | 41 | | catch ( JsonException ex ) |
| | | 42 | | { |
| | 3 | 43 | | throw new InvalidOperationException( $"Failed to deserialize response. Content: {content}", ex ); |
| | | 44 | | } |
| | 9 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary>Reads the response body as a string.</summary> |
| | | 48 | | /// <param name="response">The response to read.</param> |
| | | 49 | | /// <param name="cancellationToken">Token to cancel the operation.</param> |
| | | 50 | | /// <returns>The body.</returns> |
| | | 51 | | public static Task<string> ReadResponseAsStringAsync( HttpResponseMessage response, CancellationToken cancellationTo |
| | 0 | 52 | | => response.Content.ReadAsStringAsync( cancellationToken ); |
| | | 53 | | |
| | | 54 | | /// <summary>Creates empty multipart form-data content for uploads.</summary> |
| | | 55 | | /// <returns>New multipart content. The caller disposes it.</returns> |
| | 3 | 56 | | public static MultipartFormDataContent CreateMultipartContent() => new(); |
| | | 57 | | |
| | | 58 | | /// <summary>Adds a file (<c>application/octet-stream</c>) to multipart content.</summary> |
| | | 59 | | /// <param name="content">The multipart content.</param> |
| | | 60 | | /// <param name="fileBytes">The file bytes.</param> |
| | | 61 | | /// <param name="fileName">The file name sent to the server.</param> |
| | | 62 | | /// <param name="fieldName">The form field name. Default: <c>file</c>.</param> |
| | | 63 | | public static void AddFileToMultipart( MultipartFormDataContent content, byte[] fileBytes, string fileName, string f |
| | | 64 | | { |
| | 3 | 65 | | var fileContent = new ByteArrayContent( fileBytes ); |
| | 3 | 66 | | fileContent.Headers.ContentType = new MediaTypeHeaderValue( "application/octet-stream" ); |
| | | 67 | | |
| | | 68 | | |
| | 3 | 69 | | content.Add( fileContent, fieldName, fileName ); |
| | 3 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary>Adds a text field to multipart content.</summary> |
| | | 73 | | /// <param name="content">The multipart content.</param> |
| | | 74 | | /// <param name="name">The field name.</param> |
| | | 75 | | /// <param name="value">The field value.</param> |
| | | 76 | | public static void AddFormFieldToMultipart( MultipartFormDataContent content, string name, string value ) |
| | 3 | 77 | | => content.Add( new StringContent( value ), name ); |
| | | 78 | | } |