| | | 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.IO; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Text; |
| | | 9 | | |
| | | 10 | | namespace Azure.Core.TestFramework |
| | | 11 | | { |
| | | 12 | | public class MockResponse : Response |
| | | 13 | | { |
| | 329533 | 14 | | private readonly Dictionary<string, List<string>> _headers = new Dictionary<string, List<string>>(StringComparer |
| | | 15 | | |
| | 329533 | 16 | | public MockResponse(int status, string reasonPhrase = null) |
| | | 17 | | { |
| | 329533 | 18 | | Status = status; |
| | 329533 | 19 | | ReasonPhrase = reasonPhrase; |
| | 329533 | 20 | | } |
| | | 21 | | |
| | 971201 | 22 | | public override int Status { get; } |
| | | 23 | | |
| | 3750 | 24 | | public override string ReasonPhrase { get; } |
| | | 25 | | |
| | 1375087 | 26 | | public override Stream ContentStream { get; set; } |
| | | 27 | | |
| | 1390 | 28 | | public override string ClientRequestId { get; set; } |
| | | 29 | | |
| | 0 | 30 | | public bool IsDisposed { get; private set; } |
| | | 31 | | |
| | | 32 | | public void SetContent(byte[] content) |
| | | 33 | | { |
| | 588 | 34 | | ContentStream = new MemoryStream(content); |
| | 588 | 35 | | } |
| | | 36 | | |
| | | 37 | | public void SetContent(string content) |
| | | 38 | | { |
| | 460 | 39 | | SetContent(Encoding.UTF8.GetBytes(content)); |
| | 460 | 40 | | } |
| | | 41 | | |
| | | 42 | | public void AddHeader(HttpHeader header) |
| | | 43 | | { |
| | 4879873 | 44 | | if (!_headers.TryGetValue(header.Name, out List<string> values)) |
| | | 45 | | { |
| | 4606339 | 46 | | _headers[header.Name] = values = new List<string>(); |
| | | 47 | | } |
| | | 48 | | |
| | 4879873 | 49 | | values.Add(header.Value); |
| | 4879873 | 50 | | } |
| | | 51 | | |
| | | 52 | | #if HAS_INTERNALS_VISIBLE_CORE |
| | | 53 | | internal |
| | | 54 | | #endif |
| | | 55 | | protected override bool TryGetHeader(string name, out string value) |
| | | 56 | | { |
| | 753536 | 57 | | if (_headers.TryGetValue(name, out List<string> values)) |
| | | 58 | | { |
| | 206972 | 59 | | value = JoinHeaderValue(values); |
| | 206972 | 60 | | return true; |
| | | 61 | | } |
| | | 62 | | |
| | 546564 | 63 | | value = null; |
| | 546564 | 64 | | return false; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | #if HAS_INTERNALS_VISIBLE_CORE |
| | | 68 | | internal |
| | | 69 | | #endif |
| | | 70 | | protected override bool TryGetHeaderValues(string name, out IEnumerable<string> values) |
| | | 71 | | { |
| | 51040 | 72 | | var result = _headers.TryGetValue(name, out List<string> valuesList); |
| | 51040 | 73 | | values = valuesList; |
| | 51040 | 74 | | return result; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | #if HAS_INTERNALS_VISIBLE_CORE |
| | | 78 | | internal |
| | | 79 | | #endif |
| | | 80 | | protected override bool ContainsHeader(string name) |
| | | 81 | | { |
| | 11364 | 82 | | return TryGetHeaderValues(name, out _); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | #if HAS_INTERNALS_VISIBLE_CORE |
| | | 86 | | internal |
| | | 87 | | #endif |
| | 153528 | 88 | | protected override IEnumerable<HttpHeader> EnumerateHeaders() => _headers.Select(h => new HttpHeader(h.Key, Join |
| | | 89 | | |
| | | 90 | | private static string JoinHeaderValue(IEnumerable<string> values) |
| | | 91 | | { |
| | 350372 | 92 | | return string.Join(",", values); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public override void Dispose() |
| | | 96 | | { |
| | 321586 | 97 | | IsDisposed = true; |
| | 321586 | 98 | | } |
| | | 99 | | } |
| | | 100 | | } |