public void CanUseRegisterExtensionMethod()
        {
            var curie = new CuriesLink("aap", "http://www.helpt.com/{?rel}");

            var builder = Hypermedia.CreateBuilder();
            var selfLink = new Link<ProductRepresentation>("product", "http://www.product.com?id=1");
            var link2 = new Link("related", "http://www.related.com");
            var link3 = curie.CreateLink<CategoryRepresentation>("category", "http://www.category.com");
            
            builder.Register(selfLink, link2, link3);
            
            var config = builder.Build();

            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter(config) { Indent = true };
            var content = new StringContent(string.Empty);
            var type = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
        public void ProperlySerializesRepresentationToJson()
        {
			// arrange
			var example = new CuriesLink("example-namespace", "http://api.example.com/docs/{rel}");
			var productLink = example.CreateLink("product", "http://api.example.com/products/{id}");
			var relatedProductLink = example.CreateLink("related-product", productLink.Href);
			var saleProductLink = example.CreateLink("product-on-sale", "http://api.example.com/products/sale/{id}");
			var categoryLink = example.CreateLink("category", "http://api.example.com/categories/{id}");

			var builder = Hypermedia.CreateBuilder();

			builder.RegisterAppender(new ProductRepresentationHypermediaAppender());
			builder.RegisterAppender(new CategoryRepresentationHypermediaAppender());

			builder.RegisterSelf<ProductRepresentation>(productLink);
			builder.RegisterSelf<CategoryRepresentation>(categoryLink);
			builder.RegisterLinks<ProductRepresentation>(relatedProductLink, saleProductLink);

			var config = builder.Build();
			var mediaFormatter = new JsonHalMediaTypeFormatter(config) { Indent = true };
            var content = new StringContent(string.Empty);
            var type = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
Esempio n. 3
0
        public void CanUseRegisterExtensionMethod()
        {
            var curie = new CuriesLink("aap", "http://www.helpt.com/{?rel}");

            var builder  = Hypermedia.CreateBuilder();
            var selfLink = new Link <ProductRepresentation>("product", "http://www.product.com?id=1");
            var link2    = new Link("related", "http://www.related.com");
            var link3    = curie.CreateLink <CategoryRepresentation>("category", "http://www.category.com");

            builder.Register(selfLink, link2, link3);

            var config = builder.Build();

            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter(config)
            {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
        public void representation_get_json__with_no_links_test()
        {
            RootRepresentation resourcesRepresentation = new RootRepresentation
            {
                items = new[]
                {
                    new NoLinkRepresentation {id = "aaaaa"},
                    new NoLinkRepresentation {id = "bbbbb"}
                }
            };

            JsonHalMediaTypeFormatter mediaFormatter = new JsonHalMediaTypeFormatter() { Indent = true };
            StringContent content = new StringContent(string.Empty);

            var type = resourcesRepresentation.GetType();

            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, resourcesRepresentation, stream, content, null).Wait();
                stream.Seek(0, SeekOrigin.Begin);
                string serialisedResult = new StreamReader(stream).ReadToEnd();

                Approvals.Verify(serialisedResult);
            }
        }
Esempio n. 5
0
        public void organisation_get_json_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter {Indent = true};
            var content = new StringContent(string.Empty);
            var type = resource.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, resource, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
        public void children_have_self_link_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter { Indent = true };
            var content = new StringContent(string.Empty);
            var type = organisations.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, organisations, stream, content, null).Wait();
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                Console.WriteLine(serialisedResult);
                Approvals.Verify(serialisedResult);
            }
        }
        public void ProperlySerializesRepresentationToJson()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter(config) { Indent = true };
            var content = new StringContent(string.Empty);
            var type = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
Esempio n. 8
0
        public void organisation_get_json_with_no_href_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter { Indent = true };
            var content = new StringContent(string.Empty);
            var resourceWithAppPath = new OrganisationWithNoHrefRepresentation(1, "Org Name");
            var type = resourceWithAppPath.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, resourceWithAppPath, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult, s => s.Replace("\r\n", "\n"));
            }
        }
Esempio n. 9
0
        public void children_have_self_link_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = organisations.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, organisations, stream, content, null).Wait();
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                Console.WriteLine(serialisedResult);
                Approvals.Verify(serialisedResult);
            }
        }
        public void one_item_organisation_list_get_json_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = oneitemrepresentation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, oneitemrepresentation, stream, content, null).Wait();
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult, s => s.Replace("\r\n", "\n"));
            }
        }
Esempio n. 11
0
        public void organisation_get_json_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = resource.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, resource, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
Esempio n. 12
0
        public void organisation_get_json_with_app_path_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter {
                Indent = true
            };
            var content             = new StringContent(string.Empty);
            var resourceWithAppPath = new OrganisationWithAppPathRepresentation(1, "Org Name");
            var type = resourceWithAppPath.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, resourceWithAppPath, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult, s => s.Replace("\r\n", "\n"));
            }
        }
Esempio n. 13
0
        public void ProperlySerializesRepresentationToJson()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter(config)
            {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
        public void ProperlySerializesRepresentationToJson()
        {
            // arrange
            var example            = new CuriesLink("example-namespace", "http://api.example.com/docs/{rel}");
            var productLink        = example.CreateLink("product", "http://api.example.com/products/{id}");
            var relatedProductLink = example.CreateLink("related-product", productLink.Href);
            var saleProductLink    = example.CreateLink("product-on-sale", "http://api.example.com/products/sale/{id}");
            var categoryLink       = example.CreateLink("category", "http://api.example.com/categories/{id}");

            var builder = Hypermedia.CreateBuilder();

            builder.RegisterAppender(new ProductRepresentationHypermediaAppender());
            builder.RegisterAppender(new CategoryRepresentationHypermediaAppender());

            builder.RegisterSelf <ProductRepresentation>(productLink);
            builder.RegisterSelf <CategoryRepresentation>(categoryLink);
            builder.RegisterLinks <ProductRepresentation>(relatedProductLink, saleProductLink);

            var config         = builder.Build();
            var mediaFormatter = new JsonHalMediaTypeFormatter(config)
            {
                Indent = true
            };
            var content = new StringContent(string.Empty);
            var type    = representation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, representation, stream, content, null);
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult);
            }
        }
        public void peopledetail_get_json_with_no_curies_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter {
                Indent = true
            };
            var content = new StringContent(string.Empty);

            resource.People[0].Links.Add(new Curie {
                Name = "br", Href = "http://test.me"
            });
            var type = resource.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, resource, stream, content, null).Wait();
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult, s => s.Replace("\r\n", "\n"));
            }
        }
        public void one_item_organisation_list_get_json_test()
        {
            // arrange
            var mediaFormatter = new JsonHalMediaTypeFormatter { Indent = true };
            var content = new StringContent(string.Empty);
            var type = oneitemrepresentation.GetType();

            // act
            using (var stream = new MemoryStream())
            {
                mediaFormatter.WriteToStreamAsync(type, oneitemrepresentation, stream, content, null).Wait();
                stream.Seek(0, SeekOrigin.Begin);
                var serialisedResult = new StreamReader(stream).ReadToEnd();

                // assert
                Approvals.Verify(serialisedResult, s => s.Replace("\r\n", "\n"));
            }
        }