So it turns out the best way we could come up with to solve this is to use a CookieContainer
and pass it as a part of the client.
First we made a base class for our ServiceInterfaceTests
public class ServiceInterfaceTestBase
{
protected IRestClient Client;
protected void AuthenticateClient(string email, string password)
{
Client = new RestClient( ServiceTestAppHostBase.BaseUrl );
var login = new RestRequest( "/auth", Method.POST );
login.AddParameter( "username", email );
login.AddParameter( "password", password );
var response = Client.Execute( login );
var cookieJar = new CookieContainer();
if ( response.StatusCode == HttpStatusCode.OK )
{
var cookie = response.Cookies.FirstOrDefault();
cookieJar.Add( new Cookie( cookie.Name, cookie.Value, cookie.Path, cookie.Domain ) );
}
Client.CookieContainer = cookieJar;
}
}
The ServiceInterfaceTests inherit from it
[TestFixture]
public class UserServiceInterfaceTests : ServiceInterfaceTestBase
{
Then in our Setup, we call the auth method.
[SetUp]
public void SetUp ()
{
_restSchemaValidator = new RestSchemaValidator();
_testLoginEmail = UserFixture.SystemAccount.Email;
_testLoginPassword = "password"; // the database contains a hashed password version of "password".
AuthenticateClient(_testLoginEmail, _testLoginPassword);
}
And lastly our Test will look like
[Test]
public void ShouldGetAListOfUsersAndReturnStatusOk ()
{
// Setup
var request = new RestRequest( "/users/", Method.GET ) { RequestFormat = DataFormat.Json, };
// Execute
var response = Client.Execute( request );
// Assert
Assert.That( response.ErrorMessage, Is.Null );
Assert.That( response.StatusCode, Is.EqualTo( HttpStatusCode.OK ) );
_restSchemaValidator.ValidateResponse( "ExpectedUsersResponse.json", response.Content );
Trace.Write( response.Content );
}