-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathHeaderDictionaryFake.cs
More file actions
92 lines (71 loc) · 2.98 KB
/
HeaderDictionaryFake.cs
File metadata and controls
92 lines (71 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using System.Linq;
namespace SharpReverseProxy.Tests.HttpContextFakes {
public class HeaderDictionaryFake : IHeaderDictionary {
private Dictionary<string, StringValues> _headers = new Dictionary<string, StringValues>();
private HttpResponseFake _parentResponse;
public HeaderDictionaryFake(HttpResponseFake parentResponse) {
_parentResponse = parentResponse;
}
public StringValues this[string key] {
get => ContainsKey(key) ? _headers[key] : StringValues.Empty;
set => _headers[key] = value;
}
public bool IsReadOnly => false;
public ICollection<string> Keys => _headers.Keys;
public ICollection<StringValues> Values => _headers.Values;
public int Count => _headers.Count;
public long? ContentLength { get => _headers["Content-Length"].ToArray().Select(s => (long?)long.Parse(s)).FirstOrDefault();
set => _headers["Content-Length"] = value.Value.ToString();
}
public void Add(string key, StringValues value) {
if(_parentResponse.HasStarted) {
ThrowReponseAlreadyStartedException();
}
_headers.Add(key, value);
}
public void Add(KeyValuePair<string, StringValues> item) {
Add(item.Key, item.Value);
}
public void Clear() {
if (_parentResponse.HasStarted) {
ThrowReponseAlreadyStartedException();
}
_headers.Clear();
}
public bool Contains(KeyValuePair<string, StringValues> item) {
var hasKey = _headers.ContainsKey(item.Key);
if (!hasKey) {
return false;
}
return _headers[item.Key].Equals(item.Value);
}
public bool ContainsKey(string key) {
return _headers.ContainsKey(key);
}
public void CopyTo(KeyValuePair<string, StringValues>[] array, int arrayIndex) {
throw new NotImplementedException();
}
public IEnumerator<KeyValuePair<string, StringValues>> GetEnumerator() {
return _headers.GetEnumerator();
}
public bool Remove(string key) {
if (_parentResponse.HasStarted) {
ThrowReponseAlreadyStartedException();
}
return _headers.Remove(key);
}
public bool Remove(KeyValuePair<string, StringValues> item) {
return Remove(item.Key);
}
public bool TryGetValue(string key, out StringValues value) => _headers.TryGetValue(key, out value);
IEnumerator IEnumerable.GetEnumerator() => _headers.GetEnumerator();
private void ThrowReponseAlreadyStartedException() {
throw new InvalidOperationException("Headers are read - only, response has already started.");
}
}
}