| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Text; |
| | | 7 | | using Azure.Core.Pipeline; |
| | | 8 | | |
| | | 9 | | namespace Azure.Core.TestFramework |
| | | 10 | | { |
| | | 11 | | public class RecordEntryMessage |
| | | 12 | | { |
| | 0 | 13 | | public SortedDictionary<string, string[]> Headers { get; set; } = new SortedDictionary<string, string[]>(StringC |
| | | 14 | | |
| | 3044916 | 15 | | public byte[] Body { get; set; } |
| | | 16 | | |
| | | 17 | | public bool TryGetContentType(out string contentType) |
| | | 18 | | { |
| | 38324 | 19 | | contentType = null; |
| | 38324 | 20 | | if (Headers.TryGetValue("Content-Type", out var contentTypes) && |
| | 38324 | 21 | | contentTypes.Length == 1) |
| | | 22 | | { |
| | 24332 | 23 | | contentType = contentTypes[0]; |
| | 24332 | 24 | | return true; |
| | | 25 | | } |
| | 13992 | 26 | | return false; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public bool IsTextContentType(out Encoding encoding) |
| | | 30 | | { |
| | 19162 | 31 | | encoding = null; |
| | 19162 | 32 | | return TryGetContentType(out string contentType) && |
| | 19162 | 33 | | ContentTypeUtilities.TryGetTextEncoding(contentType, out encoding); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public bool TryGetBodyAsText(out string text) |
| | | 37 | | { |
| | 19162 | 38 | | text = null; |
| | | 39 | | |
| | 19162 | 40 | | if (IsTextContentType(out Encoding encoding)) |
| | | 41 | | { |
| | 12058 | 42 | | text = encoding.GetString(Body); |
| | | 43 | | |
| | 12058 | 44 | | return true; |
| | | 45 | | } |
| | | 46 | | |
| | 7104 | 47 | | return false; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | } |
| | | 51 | | } |