/
ResolvingHalResourceTest.cs
90 lines (75 loc) · 3.1 KB
/
ResolvingHalResourceTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Buffers;
using System.IO;
using Assent;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WebApi.Hal.Tests.HypermediaAppenders;
using WebApi.Hal.Tests.Representations;
using Xunit;
namespace WebApi.Hal.Tests
{
public class ResolvingHalResourceTest
{
readonly ProductRepresentation representation;
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"
}
};
}
[Fact]
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, new MvcOptions(), config);
// act
using (var stream = new StringWriter())
{
mediaFormatter.WriteObject(stream, representation);
string serialisedResult = stream.ToString();
// assert
this.Assent(serialisedResult);
}
}
[Fact]
public void ProperlySerializesRepresentationWithoutLinksToJson()
{
// arrange
var builder = Hypermedia.CreateBuilder();
var config = builder.Build();
var mediaFormatter = new JsonHalMediaTypeOutputFormatter(
new JsonSerializerSettings { Formatting = Formatting.Indented }, ArrayPool<char>.Shared, new MvcOptions(), config);
// act
using (var stream = new StringWriter())
{
mediaFormatter.WriteObject(stream, representation);
string serialisedResult = stream.ToString();
// assert
this.Assent(serialisedResult);
}
}
}
}