-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPagedCollectionResultTest.cs
More file actions
109 lines (99 loc) · 4.02 KB
/
Copy pathPagedCollectionResultTest.cs
File metadata and controls
109 lines (99 loc) · 4.02 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
namespace WebLinking.Integration.AspNetCore.Tests.UnitTests.Mvc
{
using System;
using System.Threading.Tasks;
using AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Primitives;
using Moq;
using Xunit;
public class PagedCollectionResultTest
{
private readonly Mock<IServiceProvider> _serviceProviderMock =
new Mock<IServiceProvider>();
private readonly Mock<IActionResultExecutor<ObjectResult>>
_actionResultExecutorMock =
new Mock<IActionResultExecutor<ObjectResult>>();
private readonly Mock<IPagedCollection<object>> _pagedCollectionMock =
new Mock<IPagedCollection<object>>();
private readonly Mock<IHttpResponseFeature> _httpResponseFeature =
new Mock<IHttpResponseFeature>();
private readonly Mock<IHeaderDictionary> _headerDictionaryMock =
new Mock<IHeaderDictionary>();
public PagedCollectionResultTest()
{
_serviceProviderMock
.Setup(x => x.GetService(It.IsAny<Type>()))
.Returns(_actionResultExecutorMock.Object);
_httpResponseFeature
.SetupGet(x => x.Headers)
.Returns(_headerDictionaryMock.Object);
}
[Fact]
public void Constructor_Throws_When_PagedCollection_Is_Null()
{
Assert.Throws<ArgumentNullException>(
() => new PagedCollectionResult<IPagedCollection<object>>(
null));
}
[Fact]
public async Task ExecuteResultAsync_Throws_When_Context_Is_Null()
{
var pagedCollectionResult =
new PagedCollectionResult<object>(_pagedCollectionMock.Object);
await Assert.ThrowsAsync<ArgumentNullException>(
() => pagedCollectionResult.ExecuteResultAsync(null));
}
[Theory]
[InlineData(
true,
0)]
[InlineData(
false,
1)]
public async Task
ExecuteResultAsync_Adds_WebLinks_Only_If_Response_Has_Not_Started(
bool responseHasStarted,
int numLinkAddedToHeaders)
{
_httpResponseFeature
.SetupGet(x => x.HasStarted)
.Returns(responseHasStarted);
var pagedCollectionResult =
new PagedCollectionResult<object>(_pagedCollectionMock.Object);
var actionContext = new ActionContext(
CreateDefaultHttpContext(),
new RouteData(),
new ActionDescriptor());
await pagedCollectionResult.ExecuteResultAsync(actionContext);
_headerDictionaryMock.Verify(
x => x.Add(
It.Is<string>(y => y == "Link"),
It.IsAny<StringValues>()),
Times.Exactly(numLinkAddedToHeaders));
_actionResultExecutorMock.Verify(
x => x.ExecuteAsync(
It.Is<ActionContext>(y => y == actionContext),
It.Is<ObjectResult>(z => z == pagedCollectionResult)));
}
private DefaultHttpContext CreateDefaultHttpContext()
{
var httpContext = new DefaultHttpContext();
httpContext.Request.Host = new HostString(
"localhost",
80);
httpContext.Request.PathBase = new PathString("/pathbase");
httpContext.Request.Path = new PathString("/path");
httpContext.Request.QueryString = new QueryString("?key=value");
httpContext.Request.Scheme = "http";
httpContext.RequestServices = _serviceProviderMock.Object;
httpContext.Features.Set(_httpResponseFeature.Object);
return httpContext;
}
}
}