public void WhenQueryingRepeatedArgument_ThenServiceGetsBoth()
    {
        var baseUri = new Uri("http://localhost:20000");
        var service = new ProductsService();
        var config  = HttpHostConfiguration.Create()
                      .SetResourceFactory(new SingletonResourceFactory(service))
                      .UseJsonNet()
                      .AddMessageHandlers(typeof(TracingChannel));

        using (new SafeHostDisposer(
                   new HttpQueryableServiceHost(typeof(ProductsService), 25, config, new Uri(baseUri, "products"))))
        {
            var client = new HttpEntityClient(baseUri);
            var query  = (IHttpEntityQuery <Product>)client.Query <Product>("products/search2",
                                                                            new HttpNameValueCollection {
                { "search", "10" }, { "search", "20" }, { "count", "25" }
            });

            var result = query.Execute();

            Assert.Equal(2, result.TotalCount);
            Assert.Equal(2, result.Count());
            Assert.Equal(10, result.First().Id);
        }
    }
Esempio n. 2
0
        public void WhenQueryingWithExtraCriteria_ThenPopulatesMatchingEntities()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client   = new HttpEntityClient(ws.BaseUri);
                var products = client.Query <Product>(resourceName, new { search = "kzu" }).ToList();

                Assert.True(products.All(x => x.Owner.Name == "kzu"));
            }
        }
Esempio n. 3
0
        public void WhenQueryingAndNoMatches_ThenReturnsEmptyEnumerable()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client   = new HttpEntityClient(ws.BaseUri);
                var products = client.Query <Product>(resourceName).Where(x => x.Owner.Name == "foo").ToList();

                Assert.Equal(0, products.Count);
            }
        }
Esempio n. 4
0
        public void WhenSkipTakeOnly_ThenReturnsSingleElement()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client   = new HttpEntityClient(ws.BaseUri);
                var products = client.Query <Product>(resourceName).Skip(1).Take(1).ToList();

                Assert.Equal(1, products.Count);
                Assert.Equal(2, products[0].Id);
            }
        }
Esempio n. 5
0
        public void WhenDeleteFails_ThenThrows()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client = new HttpEntityClient(ws.BaseUri);

                var exception = Assert.Throws <HttpEntityException>(() => client.Delete(resourceName, "25"));

                Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode);
            }
        }
Esempio n. 6
0
        public void WhenPostFails_ThenThrows()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client = new HttpEntityClient(ws.BaseUri);

                var exception = Assert.Throws <HttpEntityException>(() => client.Post <Product>(resourceName, null));

                Assert.Equal(HttpStatusCode.InternalServerError, exception.StatusCode);
            }
        }
Esempio n. 7
0
        public void WhenOrderByTake_ThenReturnsOrdered()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client   = new HttpEntityClient(ws.BaseUri);
                var products = client.Query <Product>(resourceName).OrderBy(x => x.Title).Take(2).ToList();

                Assert.Equal(2, products.Count);
                Assert.Equal("A", products[0].Title);
                Assert.Equal("B", products[1].Title);
            }
        }
Esempio n. 8
0
        public void WhenGettingWithoutId_ThenRetrieves()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client = new HttpEntityClient(ws.BaseUri);

                var product = client.Get <Product>(resourceName + "/latest");

                Assert.NotNull(product);
                Assert.Equal("vga", product.Owner.Name);
            }
        }
Esempio n. 9
0
        public void WhenTryGetFails_ThenReturnsResponse()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client   = new HttpEntityClient(ws.BaseUri);
                var product  = default(Product);
                var response = client.TryGet <Product>(resourceName, "25", out product);

                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
                Assert.Null(product);
            }
        }
Esempio n. 10
0
		public void WhenGettingWithoutId_ThenRetrieves()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);

				var product = client.Get<Product>(resourceName + "/latest");

				Assert.NotNull(product);
				Assert.Equal("vga", product.Owner.Name);
			}
		}
Esempio n. 11
0
		public void WhenDeletingEntity_ThenGetFails()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);

				client.Delete(resourceName, "1");
				var exception = Assert.Throws<HttpEntityException>(() => client.Get<Product>(resourceName, "25"));

				Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode);
			}
		}
Esempio n. 12
0
        public void WhenQuerying_ThenPopulatesMatchingEntities()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client   = new HttpEntityClient(ws.BaseUri);
                var ids      = client.Query <Product>(resourceName).Where(x => x.Owner.Name == "kzu").Select(x => x.Id).ToList();
                var products = client.Query <Product>(resourceName).Where(x => x.Owner.Name == "kzu").ToList();

                Assert.Equal(2, ids.Count);
                Assert.True(products.All(x => x.Owner.Name == "kzu"));
            }
        }
Esempio n. 13
0
        public void WhenTryGetSucceeds_ThenPopulatesEntity()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client   = new HttpEntityClient(ws.BaseUri);
                var product  = default(Product);
                var response = client.TryGet <Product>(resourceName, "1", out product);

                Assert.True(response.IsSuccessStatusCode);
                Assert.NotNull(product);
                Assert.Equal("kzu", product.Owner.Name);
            }
        }
Esempio n. 14
0
		public void WhenPostNew_ThenSavesAndRetrievesId()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var product = new Product { Owner = new User { Id = 1, Name = "kzu" } };

				var saved = client.Post(resourceName, product);

				Assert.Equal(4, saved.Id);

				Assert.Equal(saved.Owner.Id, product.Owner.Id);
				Assert.Equal(saved.Owner.Name, product.Owner.Name);
			}
		}
Esempio n. 15
0
        public void WhenPutUpdate_ThenSaves()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", true, new ServiceConfiguration()))
            {
                var client  = new HttpEntityClient(ws.BaseUri);
                var product = new Product {
                    Id = 1, Owner = new User {
                        Id = 1, Name = "vga"
                    }
                };

                client.Put(resourceName, "1", product);

                var saved = client.Get <Product>(resourceName, "1");

                Assert.Equal(saved.Owner.Name, product.Owner.Name);
            }
        }
Esempio n. 16
0
        public void WhenPostNew_ThenSavesAndRetrievesId()
        {
            using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
            {
                var client  = new HttpEntityClient(ws.BaseUri);
                var product = new Product {
                    Owner = new User {
                        Id = 1, Name = "kzu"
                    }
                };

                var saved = client.Post(resourceName, product);

                Assert.Equal(4, saved.Id);

                Assert.Equal(saved.Owner.Id, product.Owner.Id);
                Assert.Equal(saved.Owner.Name, product.Owner.Name);
            }
        }
Esempio n. 17
0
        public void WhenQuerying_ThenCanGetTotalCount()
        {
            var baseUri = new Uri("http://localhost:20000");
            var service = new TestService();
            var config  = new ServiceConfiguration(service);

            using (new SafeHostDisposer(
                       new HttpQueryableServiceHost(typeof(TestService), 25, config, new Uri(baseUri, "products"))))
            {
                var client   = new HttpEntityClient(baseUri);
                var products = client.Query <Product>(resourceName, new { search = "kzu" }).Skip(5).Take(10);

                var query    = products as IHttpEntityQuery <Product>;
                var response = query.Execute();

                Assert.Equal(2, response.TotalCount);
                Assert.Equal(0, response.Count());
                Assert.True(response.Response.IsSuccessStatusCode);
            }
        }
    public void WhenQueryingOverLimit_ThenGetsLimitedResults()
    {
        var baseUri = new Uri("http://localhost:20000");
        var service = new ProductsService();
        var config  = HttpHostConfiguration.Create()
                      .SetResourceFactory(new SingletonResourceFactory(service))
                      .AddMessageHandlers(typeof(TracingChannel));

        using (new SafeHostDisposer(
                   new HttpQueryableServiceHost(typeof(ProductsService), 25, config, new Uri(baseUri, "products"))))
        {
            var client = new HttpEntityClient(baseUri);
            var query  = (IHttpEntityQuery <Product>)client.Query <Product>("products").Skip(10).Take(50);

            var result = query.Execute();

            Assert.Equal(100, result.TotalCount);
            Assert.Equal(25, result.Count());
            Assert.Equal(10, result.First().Id);
        }
    }
Esempio n. 19
0
	public void WhenQueryingOverLimit_ThenGetsLimitedResults()
	{
		var baseUri = new Uri("http://localhost:20000");
		var service = new ProductsService();
		var config = HttpHostConfiguration.Create()
			.SetResourceFactory(new SingletonResourceFactory(service))
			.AddMessageHandlers(typeof(TracingChannel));

		using (new SafeHostDisposer(
			new HttpQueryableServiceHost(typeof(ProductsService), 25, config, new Uri(baseUri, "products"))))
		{
			var client = new HttpEntityClient(baseUri);
			var query = (IHttpEntityQuery<Product>)client.Query<Product>("products").Skip(10).Take(50);

			var result = query.Execute();

			Assert.Equal(100, result.TotalCount);
			Assert.Equal(25, result.Count());
			Assert.Equal(10, result.First().Id);
		}
	}
Esempio n. 20
0
	public void WhenExecutingRequest_ThenTracesInformationHeaders()
	{
		var listener = new ConsoleTraceListener();
		TracerExtensibility.AddListener(SourceName.For<TracingChannel>(), listener);
		TracerExtensibility.SetTracingLevel(SourceName.For<TracingChannel>(), SourceLevels.All);

		var config = HttpHostConfiguration.Create()
			.UseJsonNet()
			.AddMessageHandlers(typeof(TracingChannel));

		using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", true, config))
		{
			var client = new HttpEntityClient(ws.BaseUri);
			var products = client.Query<Product>().Skip(1).Take(1).ToList();

			Assert.Equal(1, products.Count);
			Assert.Equal(2, products[0].Id);
		}

		listener.Flush();
	}
Esempio n. 21
0
    public void WhenExecutingRequest_ThenTracesInformationHeaders()
    {
        var listener = new ConsoleTraceListener();

        TracerExtensibility.AddListener(SourceName.For <TracingChannel>(), listener);
        TracerExtensibility.SetTracingLevel(SourceName.For <TracingChannel>(), SourceLevels.All);

        var config = HttpHostConfiguration.Create()
                     .UseJsonNet()
                     .AddMessageHandlers(typeof(TracingChannel));

        using (var ws = new HttpWebService <TestService>("http://localhost:20000", "products", true, config))
        {
            var client   = new HttpEntityClient(ws.BaseUri);
            var products = client.Query <Product>().Skip(1).Take(1).ToList();

            Assert.Equal(1, products.Count);
            Assert.Equal(2, products[0].Id);
        }

        listener.Flush();
    }
Esempio n. 22
0
		public void WhenPutUpdate_ThenSaves()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", true, new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var product = new Product { Id = 1, Owner = new User { Id = 1, Name = "vga" } };

				client.Put(resourceName, "1", product);

				var saved = client.Get<Product>(resourceName, "1");

				Assert.Equal(saved.Owner.Name, product.Owner.Name);
			}
		}
Esempio n. 23
0
		public void WhenTryGetSucceeds_ThenPopulatesEntity()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var product = default(Product);
				var response = client.TryGet<Product>(resourceName, "1", out product);

				Assert.True(response.IsSuccessStatusCode);
				Assert.NotNull(product);
				Assert.Equal("kzu", product.Owner.Name);
			}
		}
Esempio n. 24
0
		public void WhenQuerying_ThenPopulatesMatchingEntities()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var ids = client.Query<Product>(resourceName).Where(x => x.Owner.Name == "kzu").Select(x => x.Id).ToList();
				var products = client.Query<Product>(resourceName).Where(x => x.Owner.Name == "kzu").ToList();

				Assert.Equal(2, ids.Count);
				Assert.True(products.All(x => x.Owner.Name == "kzu"));
			}
		}
Esempio n. 25
0
		public void WhenQueryingAndNoMatches_ThenReturnsEmptyEnumerable()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var products = client.Query<Product>(resourceName).Where(x => x.Owner.Name == "foo").ToList();

				Assert.Equal(0, products.Count);
			}
		}
Esempio n. 26
0
		public void WhenSkipTakeOnly_ThenReturnsSingleElement()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var products = client.Query<Product>(resourceName).Skip(1).Take(1).ToList();

				Assert.Equal(1, products.Count);
				Assert.Equal(2, products[0].Id);
			}
		}
Esempio n. 27
0
		public void WhenOrderByTake_ThenReturnsOrdered()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var products = client.Query<Product>(resourceName).OrderBy(x => x.Title).Take(2).ToList();

				Assert.Equal(2, products.Count);
				Assert.Equal("A", products[0].Title);
				Assert.Equal("B", products[1].Title);
			}
		}
Esempio n. 28
0
		public void WhenQueryingWithExtraCriteria_ThenPopulatesMatchingEntities()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var products = client.Query<Product>(resourceName, new { search = "kzu" }).ToList();

				Assert.True(products.All(x => x.Owner.Name == "kzu"));
			}
		}
Esempio n. 29
0
		public void WhenQuerying_ThenCanGetTotalCount()
		{
			var baseUri = new Uri("http://localhost:20000");
			var service = new TestService();
			var config = new ServiceConfiguration(service);

			using (new SafeHostDisposer(
				new HttpQueryableServiceHost(typeof(TestService), 25, config, new Uri(baseUri, "products"))))
			{
				var client = new HttpEntityClient(baseUri);
				var products = client.Query<Product>(resourceName, new { search = "kzu" }).Skip(5).Take(10);

				var query = products as IHttpEntityQuery<Product>;
				var response = query.Execute();

				Assert.Equal(2, response.TotalCount);
				Assert.Equal(0, response.Count());
				Assert.True(response.Response.IsSuccessStatusCode);
			}
		}
Esempio n. 30
0
		public void WhenPostFails_ThenThrows()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);

				var exception = Assert.Throws<HttpEntityException>(() => client.Post<Product>(resourceName, null));

				Assert.Equal(HttpStatusCode.InternalServerError, exception.StatusCode);
			}
		}
Esempio n. 31
0
	public void WhenQueryingRepeatedArgument_ThenServiceGetsBoth()
	{
		var baseUri = new Uri("http://localhost:20000");
		var service = new ProductsService();
		var config = HttpHostConfiguration.Create()
			.SetResourceFactory(new SingletonResourceFactory(service))
			.UseJsonNet()
			.AddMessageHandlers(typeof(TracingChannel));

		using (new SafeHostDisposer(
			new HttpQueryableServiceHost(typeof(ProductsService), 25, config, new Uri(baseUri, "products"))))
		{
			var client = new HttpEntityClient(baseUri);
			var query = (IHttpEntityQuery<Product>)client.Query<Product>("products/search2",
				new HttpNameValueCollection { { "search", "10" }, { "search", "20" }, { "count", "25" } });

			var result = query.Execute();

			Assert.Equal(2, result.TotalCount);
			Assert.Equal(2, result.Count());
			Assert.Equal(10, result.First().Id);
		}
	}
Esempio n. 32
0
		public void WhenTryGetFails_ThenReturnsResponse()
		{
			using (var ws = new HttpWebService<TestService>("http://localhost:20000", "products", new ServiceConfiguration()))
			{
				var client = new HttpEntityClient(ws.BaseUri);
				var product = default(Product);
				var response = client.TryGet<Product>(resourceName, "25", out product);

				Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
				Assert.Null(product);
			}
		}