Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In .NET Core 3.1, the RequestCookieCollection can no longer be used to create cookies in unit tests

I have just upgraded from .NET Core 2.2 to 3.1. I have tests to confirm that extension methods I've added to HttpContext.Request are working. I was previously able to do things like:

    var context = new DefaultHttpContext();
    var c = new Dictionary<string, string> {{"test", "passed"}};
    context.Request.Cookies = new RequestCookieCollection(cookies);

    var result = context.Request.GetPrimedValue();

Is this impossible now? I tried using Moq for this, but there are far too many things blocking me from being able to set the Cookies property with anything usable, it seems. What is the resolution for this?

note: I understand that this was using an internal class which shouldn't have been internal, so I don't disagree with the internal namespace being hidden, but I'm not sure what my alternatives are.

like image 460
Dinerdo Avatar asked Feb 28 '20 04:02

Dinerdo


5 Answers

Another way of mocking cookies by appending request headers.

[TestMethod]
public void Test()
{
    // Arrange
    var controller = new Controller();
    var cookie = new StringValues(COOKIE_NAME + "=" + COOKIE_VALUE);
    controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() };
    controller.ControllerContext.HttpContext.Request.Headers.Add(HeaderNames.Cookie, cookie);

    // Act
    var result = Sut.Action();

    // Assert
    // TODO
}
like image 70
SoniCue Avatar answered Oct 24 '22 07:10

SoniCue


I found that the simplest solution for my purposes was to leverage the fact that a Dictionary<string, string> is almost a complete implementation of IRequestCookieCollection. Just need to expose the Keys property as an ICollection<string>:

public class RequestCookieCollection : Dictionary<string, string>, IRequestCookieCollection
{
    public new ICollection<string> Keys => base.Keys;
}
like image 21
grin0048 Avatar answered Oct 24 '22 05:10

grin0048


By manipulating some foundation classes, I am able to produce a mock cookie collection. However it is more like a workaround and might not work in future releases. You might want to give it a try and see how long it can go ...

With the helper function:

    private static IRequestCookieCollection MockRequestCookieCollection(string key, string value)
    {
            var requestFeature = new HttpRequestFeature();
            var featureCollection = new FeatureCollection();

            requestFeature.Headers = new HeaderDictionary();
            requestFeature.Headers.Add(HeaderNames.Cookie, new StringValues(key + "=" + value));

            featureCollection.Set<IHttpRequestFeature>(requestFeature);

            var cookiesFeature = new RequestCookiesFeature(featureCollection);

            return cookiesFeature.Cookies;
    }

Now your unit test code shall become

    var context = new DefaultHttpContext();

    context.Request.Cookies = MockRequestCookieCollection("test", "passed");
like image 17
victor6510 Avatar answered Oct 24 '22 05:10

victor6510


Sorry I can't add this as a comment, but if you create your own RequestCookieCollection class, based on the original one in the .net core codebase:

https://github.com/dotnet/aspnetcore/blob/4ef204e13b88c0734e0e94a1cc4c0ef05f40849e/src/Http/Http/src/Internal/RequestCookieCollection.cs

Then you could use this new class to create your cookie collection in your unit tests project. I tried this approach and it works.

like image 2
Matt B Avatar answered Oct 24 '22 07:10

Matt B


@Victor6510's answer worked great for me until i needed to create a fake cookie that contained JSON. then i ran into this bug: https://github.com/dotnet/aspnetcore/issues/29304 so i had to come up with additional way.

Using NSubstitute:

[TestClass]
public class ImpersonationMiddlewareTest
{
    private HttpContext _sHttpContext;
    private HttpRequest _sHttpRequest;
    private IRequestCookieCollection _sCookieCollection;

    [TestInitialize]
    public void Initialize()
    {
        _sHttpContext = Substitute.For<HttpContext>();
        _sHttpRequest = Substitute.For<HttpRequest>();
        _sCookieCollection = Substitute.For<IRequestCookieCollection>();
    }

    [TestMethod]
    public async Task Invoke_GoodCookie_CreatesImpersonationIdentity()
    {
        //arrange
        var cookieList = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string,string>("cookiename", "cookievalue")
        };
        _sCookieCollection.GetEnumerator().Returns(e => cookieList.GetEnumerator());
        _sCookieCollection.ContainsKey("cookiename").Returns(true);

        _sHttpRequest.Cookies.Returns(_sCookieCollection);

        _sHttpContext.Request.Returns(_sHttpRequest);

        //act
        myMiddleware.InvokeAsync(_sHttpContext);

        //assert
    }
}
like image 1
josh Avatar answered Oct 24 '22 06:10

josh