NancyFX: Can I force my browser to authenticate to test the default modules?

Here is a unit test that shows me the authentication of my Nancy browser (other code has been disabled). I was wondering if there is a smarter DRYER way?

[Fact]
public void Login__Should_redirect_from_login_to_requested_page_if_credentials_are_correct()
{
    var browser = Fake.Browser();
    var response = browser.Post("/login", with =>
                                         {
                                           with.HttpRequest();
                                           with.FormValue("UserName", userName);
                                           with.FormValue("Password", password);
                                          });
    response.ShouldHaveRedirectedTo("/");
}
+3
source share
1 answer

It looks like you have a method that returns a browser instance: Fake.Browser()so why not just rewrite this to provide an authenticated version if necessary. Something like this is possible:

public static Browser Browser(string username = null, string password = null)
{
    var browser = new Browser(new UnitTestBootstrapper());
    if (username.IsEmpty() || password.IsEmpty()) return browser;
    return browser.Post("/login", with =>
                                        {
                                            with.HttpRequest();
                                            with.FormValue("Username", username);
                                            with.FormValue("Password", password);
                                        }).Then;
}
+4
source

All Articles