diff --git a/src/libraries/System.Net.Http/ref/System.Net.Http.cs b/src/libraries/System.Net.Http/ref/System.Net.Http.cs index 1652b5804ae811..46d2b57abfc786 100644 --- a/src/libraries/System.Net.Http/ref/System.Net.Http.cs +++ b/src/libraries/System.Net.Http/ref/System.Net.Http.cs @@ -277,8 +277,10 @@ public HttpRequestOptionsKey(string key) {} public string Key { get { throw null; } } } - public sealed class HttpRequestOptions : System.Collections.Generic.IDictionary + public sealed class HttpRequestOptions : System.Collections.Generic.IDictionary, System.Collections.Generic.IReadOnlyDictionary { + bool System.Collections.Generic.IReadOnlyDictionary.ContainsKey(string key) { throw null; } + int System.Collections.Generic.IReadOnlyCollection>.Count { get { throw null; } } void System.Collections.Generic.IDictionary.Add(string key, object? value) { throw null; } System.Collections.Generic.ICollection System.Collections.Generic.IDictionary.Keys { get { throw null; } } System.Collections.Generic.ICollection System.Collections.Generic.IDictionary.Values { get { throw null; } } @@ -297,6 +299,10 @@ public sealed class HttpRequestOptions : System.Collections.Generic.IDictionary< System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } public bool TryGetValue(HttpRequestOptionsKey key, [MaybeNullWhen(false)] out TValue value) { throw null; } public void Set(HttpRequestOptionsKey key, TValue value) { throw null; } + bool System.Collections.Generic.IReadOnlyDictionary.TryGetValue(string key, out object? value) { throw null; } + object? System.Collections.Generic.IReadOnlyDictionary.this[string key] { get { throw null; } } + System.Collections.Generic.IEnumerable System.Collections.Generic.IReadOnlyDictionary.Keys { get { throw null; } } + System.Collections.Generic.IEnumerable System.Collections.Generic.IReadOnlyDictionary.Values { get { throw null; } } } public partial class HttpResponseMessage : System.IDisposable diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestOptions.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestOptions.cs index 20116559c89303..75cb6d8d7e6e10 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestOptions.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestOptions.cs @@ -9,9 +9,13 @@ namespace System.Net.Http /// /// Represents a collection of options for an HTTP request. /// - public sealed class HttpRequestOptions : IDictionary + public sealed class HttpRequestOptions : IDictionary, IReadOnlyDictionary { private Dictionary Options { get; } = new Dictionary(); + bool IReadOnlyDictionary.TryGetValue(string key, out object? value) => Options.TryGetValue(key, out value); + object? IReadOnlyDictionary.this[string key] => Options[key]; + IEnumerable IReadOnlyDictionary.Keys => Options.Keys; + IEnumerable IReadOnlyDictionary.Values => Options.Values; object? IDictionary.this[string key] { get @@ -38,7 +42,9 @@ public sealed class HttpRequestOptions : IDictionary System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => ((System.Collections.IEnumerable)Options).GetEnumerator(); bool IDictionary.Remove(string key) => Options.Remove(key); bool ICollection>.Remove(KeyValuePair item) => ((IDictionary)Options).Remove(item); + bool IReadOnlyDictionary.ContainsKey(string key) => Options.ContainsKey(key); bool IDictionary.TryGetValue(string key, out object? value) => Options.TryGetValue(key, out value); + int IReadOnlyCollection>.Count => Options.Count; /// /// Initializes a new instance of the HttpRequestOptions class. diff --git a/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpRequestOptionsTest.cs b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpRequestOptionsTest.cs new file mode 100644 index 00000000000000..c79ba6347f77d1 --- /dev/null +++ b/src/libraries/System.Net.Http/tests/UnitTests/Headers/HttpRequestOptionsTest.cs @@ -0,0 +1,49 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Collections.Generic; +using Xunit; + +namespace System.Net.Http.Tests +{ + public class HttpRequestOptionsTest + { + [Fact] + public void HttpRequestOptionsIReadOnlyDictionaryMethods_Should_WorkSameAsIDictionary() + { + const string ExpectedKey = "WebAssemblyEnableStreamingResponse"; + const bool ExpectedValue = true; + const string UnexpectedKey = "hello"; + + var requestOptions = new HttpRequestOptions(); + requestOptions.Set(new HttpRequestOptionsKey(ExpectedKey), ExpectedValue); + + IReadOnlyDictionary readOnlyDictionary = requestOptions; + IDictionary dictionary = requestOptions; + + Assert.Equal(1, readOnlyDictionary.Count); + Assert.Equal(1, dictionary.Count); + + Assert.True(readOnlyDictionary.ContainsKey(ExpectedKey)); + Assert.True(dictionary.ContainsKey(ExpectedKey)); + Assert.False(readOnlyDictionary.ContainsKey(UnexpectedKey)); + Assert.False(dictionary.ContainsKey(UnexpectedKey)); + + Assert.True(readOnlyDictionary.TryGetValue(ExpectedKey, out object? getValueFromReadOnlyDictionary)); + Assert.True(dictionary.TryGetValue(ExpectedKey, out object? getValueFromDictionary)); + Assert.Equal(ExpectedValue, getValueFromReadOnlyDictionary); + Assert.Equal(ExpectedValue, getValueFromDictionary); + + Assert.Equal(ExpectedValue, readOnlyDictionary[ExpectedKey]); + Assert.Equal(ExpectedValue, dictionary[ExpectedKey]); + Assert.Throws(() => readOnlyDictionary[UnexpectedKey]); + Assert.Throws(() => dictionary[UnexpectedKey]); + + Assert.Collection(readOnlyDictionary.Keys, item => Assert.Equal(ExpectedKey, item)); + Assert.Collection(dictionary.Keys, item => Assert.Equal(ExpectedKey, item)); + + Assert.Collection(readOnlyDictionary.Values, item => Assert.Equal(ExpectedValue, item)); + Assert.Collection(dictionary.Values, item => Assert.Equal(ExpectedValue, item)); + } + } +} diff --git a/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj b/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj index 64232533af16b4..2e702c527133a2 100755 --- a/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj +++ b/src/libraries/System.Net.Http/tests/UnitTests/System.Net.Http.Unit.Tests.csproj @@ -285,6 +285,7 @@ +