| | 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.Linq; |
| | 7 | |
|
| | 8 | | namespace Azure.Core.TestFramework |
| | 9 | | { |
| | 10 | | public class MockRequest : Request |
| | 11 | | { |
| 334230 | 12 | | public MockRequest() |
| | 13 | | { |
| 334230 | 14 | | ClientRequestId = Guid.NewGuid().ToString(); |
| 334230 | 15 | | } |
| | 16 | |
|
| 334230 | 17 | | private readonly Dictionary<string, List<string>> _headers = new Dictionary<string, List<string>>(StringComparer |
| | 18 | |
|
| 94 | 19 | | public bool IsDisposed { get; private set; } |
| | 20 | |
|
| | 21 | | public override RequestContent Content |
| | 22 | | { |
| 1000680 | 23 | | get { return base.Content; } |
| | 24 | | set |
| | 25 | | { |
| 23986 | 26 | | if (value != null && value.TryComputeLength(out long length)) |
| | 27 | | { |
| 23106 | 28 | | _headers["Content-Length"] = new List<string> { length.ToString() }; |
| | 29 | |
|
| | 30 | | } |
| | 31 | | else |
| | 32 | | { |
| 880 | 33 | | _headers.Remove("Content-Length"); |
| | 34 | | } |
| 23986 | 35 | | base.Content = value; |
| 23986 | 36 | | } |
| | 37 | | } |
| | 38 | |
|
| | 39 | | protected override void AddHeader(string name, string value) |
| | 40 | | { |
| 1556192 | 41 | | if (!_headers.TryGetValue(name, out List<string> values)) |
| | 42 | | { |
| 1556184 | 43 | | _headers[name] = values = new List<string>(); |
| | 44 | | } |
| | 45 | |
|
| 1556192 | 46 | | values.Add(value); |
| 1556192 | 47 | | } |
| | 48 | |
|
| | 49 | | protected override bool TryGetHeader(string name, out string value) |
| | 50 | | { |
| 1360666 | 51 | | if (_headers.TryGetValue(name, out List<string> values)) |
| | 52 | | { |
| 340660 | 53 | | value = JoinHeaderValue(values); |
| 340660 | 54 | | return true; |
| | 55 | | } |
| | 56 | |
|
| 1020006 | 57 | | value = null; |
| 1020006 | 58 | | return false; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | protected override bool TryGetHeaderValues(string name, out IEnumerable<string> values) |
| | 62 | | { |
| 1621444 | 63 | | var result = _headers.TryGetValue(name, out List<string> valuesList); |
| 1621444 | 64 | | values = valuesList; |
| 1621444 | 65 | | return result; |
| | 66 | | } |
| | 67 | |
|
| | 68 | | protected override bool ContainsHeader(string name) |
| | 69 | | { |
| 71690 | 70 | | return TryGetHeaderValues(name, out _); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | protected override bool RemoveHeader(string name) |
| | 74 | | { |
| 1130560 | 75 | | return _headers.Remove(name); |
| | 76 | | } |
| | 77 | |
|
| 2165038 | 78 | | protected override IEnumerable<HttpHeader> EnumerateHeaders() => _headers.Select(h => new HttpHeader(h.Key, Join |
| | 79 | |
|
| | 80 | | private static string JoinHeaderValue(IEnumerable<string> values) |
| | 81 | | { |
| 2136748 | 82 | | return string.Join(",", values); |
| | 83 | | } |
| | 84 | |
|
| 1106558 | 85 | | public override string ClientRequestId { get; set; } |
| | 86 | |
|
| 0 | 87 | | public override string ToString() => $"{Method} {Uri}"; |
| | 88 | |
|
| | 89 | | public override void Dispose() |
| | 90 | | { |
| 326300 | 91 | | IsDisposed = true; |
| 326300 | 92 | | } |
| | 93 | | } |
| | 94 | | } |