-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
FileBatcherTests.cs
134 lines (111 loc) · 6.3 KB
/
FileBatcherTests.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Smidge.CompositeFiles;
using Smidge.Models;
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.FileProviders;
using Xunit;
using Smidge.Hashing;
using Smidge.Cache;
namespace Smidge.Tests
{
public class FileBatcherTests
{
[Fact]
public void Get_Composite_File_Collection_For_Url_Generation_No_Duplicates()
{
var websiteInfo = new Mock<IWebsiteInfo>();
websiteInfo.Setup(x => x.GetBasePath()).Returns(string.Empty);
websiteInfo.Setup(x => x.GetBaseUrl()).Returns(new Uri("http://test.com"));
var urlHelper = new RequestHelper(websiteInfo.Object);
var fileProvider = new Mock<IFileProvider>();
var cacheProvider = new Mock<ICacheFileSystem>();
var fileProviderFilter = new DefaultFileProviderFilter();
var hasher = Mock.Of<IHasher>();
var fileSystemHelper = new SmidgeFileSystem(fileProvider.Object, fileProviderFilter, cacheProvider.Object, Mock.Of<IWebsiteInfo>());
var batcher = new FileBatcher(fileSystemHelper, urlHelper, hasher);
var file = new Mock<IFileInfo>();
file.Setup(a => a.IsDirectory).Returns(false);
file.SetupAllProperties();
fileProvider.Setup(x => x.GetFileInfo(It.IsAny<string>())).Returns(file.Object);
var result = batcher.GetCompositeFileCollectionForUrlGeneration(new IWebFile[] {
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test.js"),
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test.js"),
Mock.Of<IWebFile>(f => f.FilePath == "hello/world.js")
});
Assert.Equal(2, result.First().Files.Count());
}
[Fact]
public void Get_Composite_File_Collection_For_Url_Generation()
{
var websiteInfo = new Mock<IWebsiteInfo>();
websiteInfo.Setup(x => x.GetBasePath()).Returns(string.Empty);
websiteInfo.Setup(x => x.GetBaseUrl()).Returns(new Uri("http://test.com"));
var urlHelper = new RequestHelper(websiteInfo.Object);
var fileProvider = new Mock<IFileProvider>();
var cacheProvider = new Mock<ICacheFileSystem>();
var fileProviderFilter = new DefaultFileProviderFilter();
var hasher = Mock.Of<IHasher>();
var fileSystemHelper = new SmidgeFileSystem(fileProvider.Object, fileProviderFilter, cacheProvider.Object, Mock.Of<IWebsiteInfo>());
var batcher = new FileBatcher(fileSystemHelper, urlHelper, hasher);
var file = new Mock<IFileInfo>();
file.Setup(a => a.IsDirectory).Returns(false);
file.SetupAllProperties();
fileProvider.Setup(x => x.GetFileInfo(It.IsAny<string>())).Returns(file.Object);
//test a mix start/ending with external
var result = batcher.GetCompositeFileCollectionForUrlGeneration(new IWebFile[] {
Mock.Of<IWebFile>(f => f.FilePath == "//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"),
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test.js"),
Mock.Of<IWebFile>(f => f.FilePath == "hello/world.js"),
Mock.Of<IWebFile>(f => f.FilePath == "//cdnjs.cloudflare.com/ajax/libs/test.min.js"),
Mock.Of<IWebFile>(f => f.FilePath == "http://cdnjs.cloudflare.com/ajax/libs/test2.min.js"),
});
Assert.Equal(4, result.Count());
//start/end with internal
result = batcher.GetCompositeFileCollectionForUrlGeneration(new IWebFile[] {
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test.js"),
Mock.Of<IWebFile>(f => f.FilePath == "hello/world.js"),
Mock.Of<IWebFile>(f => f.FilePath == "//cdnjs.cloudflare.com/ajax/libs/test.min.js"),
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test2.js"),
Mock.Of<IWebFile>(f => f.FilePath == "hello/world2.js"),
});
Assert.Equal(3, result.Count());
//all internal
result = batcher.GetCompositeFileCollectionForUrlGeneration(new IWebFile[] {
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test.js"),
Mock.Of<IWebFile>(f => f.FilePath == "hello/world.js"),
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test2.js"),
Mock.Of<IWebFile>(f => f.FilePath == "hello/world2.js"),
});
Assert.Single(result);
//start internal/end external
result = batcher.GetCompositeFileCollectionForUrlGeneration(new IWebFile[] {
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test.js"),
Mock.Of<IWebFile>(f => f.FilePath == "hello/world.js"),
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test2.js"),
Mock.Of<IWebFile>(f => f.FilePath == "//cdnjs.cloudflare.com/ajax/libs/test.min.js"),
});
Assert.Equal(2, result.Count());
//start external/end internal
result = batcher.GetCompositeFileCollectionForUrlGeneration(new IWebFile[] {
Mock.Of<IWebFile>(f => f.FilePath == "//cdnjs.cloudflare.com/ajax/libs/test.min.js"),
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test.js"),
Mock.Of<IWebFile>(f => f.FilePath == "hello/world.js"),
Mock.Of<IWebFile>(f => f.FilePath == "~/test/test2.js"),
});
Assert.Equal(2, result.Count());
//all external
result = batcher.GetCompositeFileCollectionForUrlGeneration(new IWebFile[] {
Mock.Of<IWebFile>(f => f.FilePath == "//cdnjs.cloudflare.com/ajax/libs/test.min.js"),
Mock.Of<IWebFile>(f => f.FilePath == "//cdnjs.cloudflare.com/ajax/libs/test2.min.js"),
Mock.Of<IWebFile>(f => f.FilePath == "//cdnjs.cloudflare.com/ajax/libs/test3.min.js")
});
Assert.Equal(3, result.Count());
}
}
}