Ejemplo n.º 1
0
        public void Configuration(IAppBuilder appBuilder)
        {
            var config = new HttpConfiguration();

            config.MessageHandlers.Add(new HeaderAppenderHandler());
            config.MessageHandlers.Add(new EndRequestHandler());
            config.Filters.Add(new ArticlesReversedFilter());
            config.Services.Replace(typeof(IAssembliesResolver), new CustomAssembliesResolver());

            // Routing
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
            config.MapHttpAttributeRoutes();

            // Autofac Configuration

            var builder = new ContainerBuilder();

            builder.RegisterApiControllers(typeof(ArticlesController).Assembly);

            // Unit of work.
            var unitOfWork = new Mock <IUnitOfWork>();

            builder.RegisterInstance(unitOfWork.Object).As <IUnitOfWork>();

            // Repositories.
            var articlesRepository = new Mock <IArticleRepository>();

            articlesRepository.Setup(r => r.GetAll()).Returns(
                BloggerInitializer.GetAllArticles()
                );
            builder.RegisterInstance(articlesRepository.Object).As <IArticleRepository>();

            var _blogsRepository = new Mock <IBlogRepository>();

            _blogsRepository.Setup(x => x.GetAll()).Returns(
                BloggerInitializer.GetBlogs
                );
            builder.RegisterInstance(_blogsRepository.Object).As <IBlogRepository>();

            // Services
            builder.RegisterAssemblyTypes(typeof(ArticleService).Assembly)
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces().InstancePerRequest();

            builder.RegisterInstance(new ArticleService(articlesRepository.Object, unitOfWork.Object));
            builder.RegisterInstance(new BlogService(_blogsRepository.Object, unitOfWork.Object));

            IContainer container = builder.Build();

            config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            appBuilder.UseWebApi(config);
        }
Ejemplo n.º 2
0
        public List <Article> SetupArticles()
        {
            int counter  = new int();
            var articles = BloggerInitializer.GetAllArticles();

            foreach (var article in articles)
            {
                article.ID = ++counter;
            }

            return(articles);
        }
Ejemplo n.º 3
0
        public List <Article> SetupArticles()
        {
            int            _counter  = new int();
            List <Article> _articles = BloggerInitializer.GetAllArticles();

            foreach (Article _article in _articles)
            {
                _article.ID = ++_counter;
            }

            return(_articles);
        }
Ejemplo n.º 4
0
        public void ShouldCallToControllerActionAppendCustomHeader()
        {
            // Arrange
            var address = "http://localhost:9000/";

            using (WebApp.Start <Startup>(address))
            {
                HttpClient client   = new HttpClient();
                var        response = client.GetAsync(address + "api/articles").Result;

                Assert.That(response.Headers.Contains("X-WebAPI-Header"), Is.True);
                var returnedArticles = response.Content.ReadAsAsync <List <Article> >().Result;
                Assert.That(returnedArticles.Count, Is.EqualTo(BloggerInitializer.GetAllArticles().Count));
            }
        }
        public void ShouldCallToControllerActionReverseArticles()
        {
            //Arrange
            var address = "http://localhost:9000/";

            using (WebApp.Start <Startup>(address))
            {
                HttpClient _client  = new HttpClient();
                var        response = _client.GetAsync(address + "api/articles").Result;

                var _returnedArticles = response.Content.ReadAsAsync <List <Article> >().Result;

                Assert.That(_returnedArticles.First().Title, Is.EqualTo(BloggerInitializer.GetAllArticles().Last().Title));
            }
        }
        public void ShouldSortArticlesByTitle()
        {
            var filter          = new ArticlesReversedFilter();
            var executedContext = new HttpActionExecutedContext(new HttpActionContext
            {
                Response = new HttpResponseMessage(),
            }, null);

            var articles = BloggerInitializer.GetAllArticles();

            executedContext.Response.Content = new ObjectContent <List <Article> >(
                new List <Article>(articles),
                new JsonMediaTypeFormatter()
                );

            filter.OnActionExecuted(executedContext);

            var returnedArticles = executedContext.Response.Content.ReadAsAsync <List <Article> >().Result;

            Assert.That(returnedArticles.First(), Is.EqualTo(articles.Last()));
        }
 public void Setup()
 {
     _articles = BloggerInitializer.GetAllArticles();
 }