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 JsonHalOutputFormatter(config) { }; 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 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 JsonHalOutputFormatter(config) { }; var type = representation.GetType(); var httpContext = new Mock<HttpContext>(); var httpRequest = new DefaultHttpContext().Request; httpContext.SetupGet(o => o.Request).Returns(httpRequest); // act using (var stream = new MemoryStream()) { mediaFormatter.WriteResponseBodyAsync(new OutputFormatterWriteContext(httpContext.Object, (s,e)=> new HttpResponseStreamWriter(s, e), type, representation)); //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 JsonHalMediaTypeOutputFormatter( new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool <char> .Shared); //, config); // act using (var stream = new StringWriter()) { mediaFormatter.WriteObject(stream, representation); string serialisedResult = stream.ToString(); // assert this.Assent(serialisedResult); } }
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 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 JsonHalMediaTypeOutputFormatter( new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool <char> .Shared); //, config); // act using (var stream = new StringWriter()) { mediaFormatter.WriteObject(stream, representation); string serialisedResult = stream.ToString(); // assert this.Assent(serialisedResult); } }
public HypermediaConfiguration( ILatestApiVersion version, IHypermediaAppender <ApiError> errorAppender, IHypermediaAppender <ApiVersion> versionAppender, IHypermediaAppender <PagedList <ApiVersion> > versionPagedAppender, IHypermediaAppender <Job> jobAppender, IHypermediaAppender <PagedList <Job> > jobPagedAppender, IHypermediaAppender <JobByDescriptionPagedList> jobByDescriptionPagedAppender, IHypermediaAppender <Schedule> scheduleAppender, IHypermediaAppender <PagedList <Schedule> > schedulePagedAppender, IHypermediaAppender <ScheduleByJobPagedList> scheduleByJobPagedAppender, IHypermediaAppender <ScheduleByDescriptionPagedList> scheduleByDescriptionPagedAppender) { _version = version; _errorAppender = errorAppender; _versionAppender = versionAppender; _versionPagedAppender = versionPagedAppender; _jobAppender = jobAppender; _jobPagedAppender = jobPagedAppender; _jobByDescriptionPagedAppender = jobByDescriptionPagedAppender; _scheduleAppender = scheduleAppender; _schedulePagedAppender = schedulePagedAppender; _scheduleByJobPagedAppender = scheduleByJobPagedAppender; _scheduleByDescriptionPagedAppender = scheduleByDescriptionPagedAppender; _curie = new CuriesLink(Curies.Jobbie, $"https://jobbie-api.com/v{_version}/docs/{{rel}}"); _builder = Hal.Hypermedia.CreateBuilder(); RegisterApiError(); RegisterVersion(); RegisterJob(); RegisterSchedule(); }
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); } }
private static IHypermediaResolver BuildHypermediaConfiguration() { var builder = Hypermedia.CreateBuilder(); // // Define the self-links var curie = new CuriesLink("beerco", "http://api.beerco.com/docs{?rel}"); var selfLink = curie.CreateLink<Model>("self", "~/api/value"); var helpLink = new Link("help", "http://www.iana.org/assignments/link-relations/link-relations.xhtml"); // // Register things with the container builder.Register(selfLink, new ModelAppender(), helpLink); return builder.Build(); }
protected override void CreateHypermedia() { //var curiesNew = new CuriesLink("testnew", "doc/testnew/{#rel}"); //Links.Add(curiesNew.CreateLink("create", "/do-good")); var curiesUser = new CuriesLink("users", "doc/users/{#rel}"); Links.Add(curiesUser.CreateLink("create", LinkTemplates.Users.CreateUser.Href)); Links.Add(curiesUser.CreateLink("update", LinkTemplates.Users.User.Href)); Links.Add(curiesUser.CreateLink("retreive", LinkTemplates.Users.User.Href)); Links.Add(curiesUser.CreateLink("delete", LinkTemplates.Users.User.Href)); Links.Add(curiesUser.CreateLink("list", LinkTemplates.Users.GetUsers.Href)); var curiesReview = new CuriesLink("reviews", "/doc/reviews/{#rel}"); Links.Add(curiesReview.CreateLink("create", LinkTemplates.Reviews.CreateReview.Href)); Links.Add(curiesReview.CreateLink("update", LinkTemplates.Reviews.Review.Href)); Links.Add(curiesReview.CreateLink("retreive", LinkTemplates.Reviews.Review.Href)); Links.Add(curiesReview.CreateLink("delete", LinkTemplates.Reviews.Review.Href)); Links.Add(curiesReview.CreateLink("list", LinkTemplates.Reviews.GetReviews.Href)); var curiesPost = new CuriesLink("posts", "/doc/posts/{#rel}"); Links.Add(curiesPost.CreateLink("create", LinkTemplates.Posts.CreatePost.Href)); Links.Add(curiesPost.CreateLink("update", LinkTemplates.Posts.Post.Href)); Links.Add(curiesPost.CreateLink("retreive", LinkTemplates.Posts.Post.Href)); Links.Add(curiesPost.CreateLink("delete", LinkTemplates.Posts.Post.Href)); Links.Add(curiesPost.CreateLink("list", LinkTemplates.Posts.GetPosts.Href)); var curiesCategory = new CuriesLink("categories", "/doc/categories/{#rel}"); Links.Add(curiesCategory.CreateLink("create", LinkTemplates.Categories.CreateCategory.Href)); Links.Add(curiesCategory.CreateLink("update", LinkTemplates.Categories.Category.Href)); Links.Add(curiesCategory.CreateLink("retreive", LinkTemplates.Categories.Category.Href)); Links.Add(curiesCategory.CreateLink("delete", LinkTemplates.Categories.Category.Href)); Links.Add(curiesCategory.CreateLink("list", LinkTemplates.Categories.GetCategories.Href)); Links.Add(new Link("new", "/do-smth/")); }
protected override void CreateHypermedia() { Href = LinkTemplates.Entities.CreateLink().Href; var curiesAnimalType = new CuriesLink("animalTypes", "doc/animalTypes/{#rel}"); Links.Add(curiesAnimalType.CreateLink("create", LinkTemplates.AnimalTypes.CreateAnimalType.Href)); Links.Add(curiesAnimalType.CreateLink("update", LinkTemplates.AnimalTypes.UpdateAnimalType.Href)); Links.Add(curiesAnimalType.CreateLink("retreive", LinkTemplates.AnimalTypes.AnimalType.Href)); Links.Add(curiesAnimalType.CreateLink("delete", LinkTemplates.AnimalTypes.DeleteAnimalType.Href)); Links.Add(curiesAnimalType.CreateLink("list", LinkTemplates.AnimalTypes.GetAnimalTypes.Href)); var curiesAnimalGenus = new CuriesLink("animalGenuses", "/doc/animalGenuses/{#rel}"); Links.Add(curiesAnimalGenus.CreateLink("create", LinkTemplates.AnimalGenuses.CreateAnimalGenus.Href)); Links.Add(curiesAnimalGenus.CreateLink("update", LinkTemplates.AnimalGenuses.UpdateAnimalGenus.Href)); Links.Add(curiesAnimalGenus.CreateLink("retreive", LinkTemplates.AnimalGenuses.AnimalGenus.Href)); Links.Add(curiesAnimalGenus.CreateLink("delete", LinkTemplates.AnimalGenuses.DeleteAnimalGenus.Href)); Links.Add(curiesAnimalGenus.CreateLink("list", LinkTemplates.AnimalGenuses.GetAnimalGenuses.Href)); var curiesPark = new CuriesLink("parks", "/doc/parks/{#rel}"); Links.Add(curiesPark.CreateLink("create", LinkTemplates.Parks.CreatePark.Href)); Links.Add(curiesPark.CreateLink("update", LinkTemplates.Parks.UpdatePark.Href)); Links.Add(curiesPark.CreateLink("retreive", LinkTemplates.Parks.Park.Href)); Links.Add(curiesPark.CreateLink("delete", LinkTemplates.Parks.DeletePark.Href)); Links.Add(curiesPark.CreateLink("list", LinkTemplates.Parks.GetParks.Href)); var curiesRingStation = new CuriesLink("ringStations", "/doc/ringStations/{#rel}"); Links.Add(curiesRingStation.CreateLink("create", LinkTemplates.RingStations.CreateRingStation.Href)); Links.Add(curiesRingStation.CreateLink("update", LinkTemplates.RingStations.UpdateRingStation.Href)); Links.Add(curiesRingStation.CreateLink("retreive", LinkTemplates.RingStations.RingStation.Href)); Links.Add(curiesRingStation.CreateLink("delete", LinkTemplates.RingStations.DeleteRingStation.Href)); Links.Add(curiesRingStation.CreateLink("list", LinkTemplates.RingStations.GetRingStations.Href)); }
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 ResolvingHalResourceTest() { // // Create representation representation = new ProductRepresentation { Id = 9, Title = "Morpheus in a chair statuette", Price = 20.14, Category = new CategoryRepresentation { Id = 99, Title = "Action Figures" } }; // // Build hypermedia configuration 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); config = builder.Build(); }
public ResolvingHalResourceTest() { // // Create representation representation = new ProductRepresentation { Id = 9, Title = "Morpheus in a chair statuette", Price = 20.14, Category = new CategoryRepresentation { Id = 99, Title = "Action Figures" } }; // // Build hypermedia configuration 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); config = builder.Build(); }