|
| 1 | +using JsonApiDotNetCore.Builders; |
| 2 | +using JsonApiDotNetCore.Configuration; |
| 3 | +using JsonApiDotNetCore.Services; |
| 4 | +using Microsoft.AspNetCore.Http; |
| 5 | +using Moq; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace UnitTests |
| 9 | +{ |
| 10 | + public class LinkBuilder_Tests |
| 11 | + { |
| 12 | + [Theory] |
| 13 | + [InlineData("http", "localhost", "/api/v1/articles", false, "http://localhost/api/v1")] |
| 14 | + [InlineData("https", "localhost", "/api/v1/articles", false, "https://localhost/api/v1")] |
| 15 | + [InlineData("http", "example.com", "/api/v1/articles", false, "http://example.com/api/v1")] |
| 16 | + [InlineData("https", "example.com", "/api/v1/articles", false, "https://example.com/api/v1")] |
| 17 | + [InlineData("https", "example.com", "/articles", false, "https://example.com")] |
| 18 | + [InlineData("https", "example.com", "/articles", true, "")] |
| 19 | + [InlineData("https", "example.com", "/api/v1/articles", true, "/api/v1")] |
| 20 | + public void GetBasePath_Returns_Path_Before_Resource(string scheme, |
| 21 | + string host, string path, bool isRelative, string expectedPath) |
| 22 | + { |
| 23 | + // arrange |
| 24 | + const string resource = "articles"; |
| 25 | + var jsonApiContextMock = new Mock<IJsonApiContext>(); |
| 26 | + jsonApiContextMock.Setup(m => m.Options).Returns(new JsonApiOptions |
| 27 | + { |
| 28 | + RelativeLinks = isRelative |
| 29 | + }); |
| 30 | + |
| 31 | + var requestMock = new Mock<HttpRequest>(); |
| 32 | + requestMock.Setup(m => m.Scheme).Returns(scheme); |
| 33 | + requestMock.Setup(m => m.Host).Returns(new HostString(host)); |
| 34 | + requestMock.Setup(m => m.Path).Returns(new PathString(path)); |
| 35 | + |
| 36 | + var contextMock = new Mock<HttpContext>(); |
| 37 | + contextMock.Setup(m => m.Request).Returns(requestMock.Object); |
| 38 | + |
| 39 | + var linkBuilder = new LinkBuilder(jsonApiContextMock.Object); |
| 40 | + |
| 41 | + // act |
| 42 | + var basePath = linkBuilder.GetBasePath(contextMock.Object, resource); |
| 43 | + |
| 44 | + // assert |
| 45 | + Assert.Equal(expectedPath, basePath); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments