.Net fluent MVC assertions

A really neat package for unit testing MVC controllers, it’s called TestStack.FluentMVCTesting and looks like:


using TestStack.FluentMVCTesting;
...
[Test]
...
          _Controller
                .WithCallTo(c => c.Action())
                .ShouldRenderView("View")
                .WithModel<Model>(
                    m =>
                    {
                        m.name.Should().NotBeNull();
                        m.Offers.Count().Should().Be(3);
                        RunOfferAssertions(m.Offers.First());
                    })
                .AndNoModelErrors();
...

.Net Make a JSON PUT request with cookies

Make a put request to a Restful API endpoint using the webclient:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace WebClientDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var baseAddress = new Uri("https://yourdomain.com/endpoint");
            const string parsedContent = "{\"BirthDate\": \"2009-04-12\",\"PostCode\": \"ZZZABC\",\"Gender\":\"1\",\"CountryCode\": \"GB\"}";

            var container = new CookieContainer();
            container.Add(new Cookie("ss-a", "08L4pWQuTMxLtmAWVHyt", "/", "somedomain.net"));
            container.Add(new Cookie("ss-b", "5gQB0eBeSUhVh9Vu1hi5", "/", "somedomain.net"));
            container.Add(new Cookie("ss-c", "tmp", "/", "somedomain.net"));
            container.Add(new Cookie("X-1", "00001", "/", "somedomain.net"));

            var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress.ToString()));
            http.Accept = "application/json";
            http.ContentType = "application/json";
            http.Method = "PUT";
            http.Host = baseAddress.Host;
            http.CookieContainer = container;
            // Custom header
            http.Headers["X-SomeHeader"] = "9B11ADAD-00C0-4D4A-A2B7-7068C5EBA86C";

            var encoding = new ASCIIEncoding();
            var bytes = encoding.GetBytes(parsedContent);

            var newStream = http.GetRequestStream();
            newStream.Write(bytes, 0, bytes.Length);
            newStream.Close();

            var response = http.GetResponse();

            var stream = response.GetResponseStream();
            if (stream != null)
            {
                var sr = new StreamReader(stream);
                var content = sr.ReadToEnd();

                Console.WriteLine(content);
            }
            Console.ReadKey();
        }
    }
}