|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System; |
4 | 5 | using Xunit;
|
5 | 6 |
|
6 | 7 | namespace Microsoft.AspNetCore.Http.Features;
|
@@ -44,4 +45,62 @@ public void SetNullValueRemoves()
|
44 | 45 | var thing2 = interfaces[typeof(IThing)];
|
45 | 46 | Assert.Null(thing2);
|
46 | 47 | }
|
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void GetMissingStructFeatureThrows() |
| 51 | + { |
| 52 | + var interfaces = new FeatureCollection(); |
| 53 | + |
| 54 | + // Regression test: Used to throw NullReferenceException because it tried to unbox a null object to a struct |
| 55 | + var ex = Assert.Throws<InvalidOperationException>(() => interfaces.Get<int>()); |
| 56 | + Assert.Equal("System.Int32 does not exist in the feature collection and because it is a struct the method can't return null. Use 'featureCollection[typeof(System.Int32)] is not null' to check if the feature exists.", ex.Message); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void GetMissingFeatureReturnsNull() |
| 61 | + { |
| 62 | + var interfaces = new FeatureCollection(); |
| 63 | + |
| 64 | + Assert.Null(interfaces.Get<Thing>()); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void GetStructFeature() |
| 69 | + { |
| 70 | + var interfaces = new FeatureCollection(); |
| 71 | + var value = 20; |
| 72 | + interfaces.Set(value); |
| 73 | + |
| 74 | + Assert.Equal(value, interfaces.Get<int>()); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public void GetNullableStructFeatureWhenSetWithNonNullableStruct() |
| 79 | + { |
| 80 | + var interfaces = new FeatureCollection(); |
| 81 | + var value = 20; |
| 82 | + interfaces.Set(value); |
| 83 | + |
| 84 | + Assert.Null(interfaces.Get<int?>()); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void GetNullableStructFeatureWhenSetWithNullableStruct() |
| 89 | + { |
| 90 | + var interfaces = new FeatureCollection(); |
| 91 | + var value = 20; |
| 92 | + interfaces.Set<int?>(value); |
| 93 | + |
| 94 | + Assert.Equal(value, interfaces.Get<int?>()); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void GetFeature() |
| 99 | + { |
| 100 | + var interfaces = new FeatureCollection(); |
| 101 | + var thing = new Thing(); |
| 102 | + interfaces.Set(thing); |
| 103 | + |
| 104 | + Assert.Equal(thing, interfaces.Get<Thing>()); |
| 105 | + } |
47 | 106 | }
|
0 commit comments