Skip to content

Commit 3e7b658

Browse files
committed
test: added tests for RelativeLinks true/false
1 parent 047e5dd commit 3e7b658

File tree

2 files changed

+93
-5
lines changed
  • src/Examples/GettingStarted
  • test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests

2 files changed

+93
-5
lines changed

src/Examples/GettingStarted/Startup.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ public void ConfigureServices(IServiceCollection services)
1515
services.AddDbContext<SampleDbContext>(
1616
options => options.UseSqlite("Data Source=sample.db"));
1717

18-
services.AddJsonApi<SampleDbContext>(options =>
19-
{
20-
options.Namespace = "api";
21-
//options.RelativeLinks = true;
22-
});
18+
services.AddJsonApi<SampleDbContext>(
19+
options => options.Namespace = "api");
2320
}
2421

2522
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Net;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using JsonApiDotNetCore.Models;
5+
using JsonApiDotNetCoreExample.Models;
6+
using Newtonsoft.Json;
7+
using Xunit;
8+
using Person = JsonApiDotNetCoreExample.Models.Person;
9+
using JsonApiDotNetCore.Configuration;
10+
11+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec.DocumentTests
12+
{
13+
public sealed class Links : FunctionalTestCollection<StandardApplicationFactory>
14+
{
15+
public Links(StandardApplicationFactory factory) : base(factory) { }
16+
17+
[Fact]
18+
public async Task GET_RelativeLinks_True_Returns_RelativeLinks()
19+
{
20+
// Arrange
21+
var person = new Person
22+
{
23+
Id = 123,
24+
FirstName = "John",
25+
LastName = "Doe",
26+
Age = 57,
27+
Gender = Gender.Male,
28+
Category = "Family"
29+
};
30+
31+
_dbContext.People.RemoveRange(_dbContext.People);
32+
_dbContext.People.Add(person);
33+
_dbContext.SaveChanges();
34+
35+
var httpMethod = new HttpMethod("GET");
36+
var route = $"http://localhost/api/v1/people/{person.Id}";
37+
38+
var request = new HttpRequestMessage(httpMethod, route);
39+
40+
var options = (JsonApiOptions) _factory.GetService<IJsonApiOptions>();
41+
options.RelativeLinks = true;
42+
43+
// Act
44+
var response = await _factory.Client.SendAsync(request);
45+
var responseString = await response.Content.ReadAsStringAsync();
46+
var document = JsonConvert.DeserializeObject<Document>(responseString);
47+
var expectedOwnerSelfLink = $"/api/v1/people/{person.Id}";
48+
49+
// Assert
50+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
51+
Assert.Equal(expectedOwnerSelfLink, document.Links.Self);
52+
}
53+
54+
[Fact]
55+
public async Task GET_RelativeLinks_False_Returns_HostsLinks()
56+
{
57+
// Arrange
58+
var person = new Person
59+
{
60+
Id = 123,
61+
FirstName = "John",
62+
LastName = "Doe",
63+
Age = 57,
64+
Gender = Gender.Male,
65+
Category = "Family"
66+
};
67+
68+
_dbContext.People.RemoveRange(_dbContext.People);
69+
_dbContext.People.Add(person);
70+
_dbContext.SaveChanges();
71+
72+
var httpMethod = new HttpMethod("GET");
73+
var route = $"http://localhost/api/v1/people/{person.Id}";
74+
75+
var request = new HttpRequestMessage(httpMethod, route);
76+
77+
var options = (JsonApiOptions) _factory.GetService<IJsonApiOptions>();
78+
options.RelativeLinks = false;
79+
80+
// Act
81+
var response = await _factory.Client.SendAsync(request);
82+
var responseString = await response.Content.ReadAsStringAsync();
83+
var document = JsonConvert.DeserializeObject<Document>(responseString);
84+
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{person.Id}";
85+
86+
// Assert
87+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
88+
Assert.Equal(expectedOwnerSelfLink, document.Links.Self);
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)