1
+ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2
+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . IO ;
7
+ using Microsoft . AspNet . FileSystems ;
8
+ using Microsoft . AspNet . Http ;
9
+ using Microsoft . AspNet . PipelineCore ;
10
+ using Microsoft . Framework . Expiration . Interfaces ;
11
+ using Microsoft . Framework . Logging ;
12
+ using Xunit ;
13
+
14
+ namespace Microsoft . AspNet . StaticFiles
15
+ {
16
+ public class StaticFileContextTest
17
+ {
18
+ [ Fact ]
19
+ public void LookupFileInfo_ReturnsFalse_IfFileDoesNotExist ( )
20
+ {
21
+ // Arrange
22
+ var options = new StaticFileOptions ( ) ;
23
+ options . FileSystem = new TestFileSystem ( ) ;
24
+ var context = new StaticFileContext ( new DefaultHttpContext ( ) , options , PathString . Empty , NullLogger . Instance ) ;
25
+
26
+ // Act
27
+ var validateResult = context . ValidatePath ( ) ;
28
+ var lookupResult = context . LookupFileInfo ( ) ;
29
+
30
+ // Assert
31
+ Assert . True ( validateResult ) ;
32
+ Assert . False ( lookupResult ) ;
33
+ }
34
+
35
+ [ Fact ]
36
+ public void LookupFileInfo_ReturnsTrue_IfFileExists ( )
37
+ {
38
+ // Arrange
39
+ var options = new StaticFileOptions ( ) ;
40
+ var fileSystem = new TestFileSystem ( ) ;
41
+ fileSystem . AddFile ( "/foo.txt" , new TestFileInfo
42
+ {
43
+ LastModified = new DateTimeOffset ( 2014 , 1 , 2 , 3 , 4 , 5 , TimeSpan . Zero )
44
+ } ) ;
45
+ options . FileSystem = fileSystem ;
46
+ var pathString = new PathString ( "/test" ) ;
47
+ var httpContext = new DefaultHttpContext ( ) ;
48
+ httpContext . Request . Path = new PathString ( "/test/foo.txt" ) ;
49
+ var context = new StaticFileContext ( httpContext , options , pathString , NullLogger . Instance ) ;
50
+
51
+ // Act
52
+ context . ValidatePath ( ) ;
53
+ var result = context . LookupFileInfo ( ) ;
54
+
55
+ // Assert
56
+ Assert . True ( result ) ;
57
+ }
58
+
59
+ private sealed class TestFileSystem : IFileSystem
60
+ {
61
+ private readonly Dictionary < string , IFileInfo > _files = new Dictionary < string , IFileInfo > ( StringComparer . Ordinal ) ;
62
+
63
+ public void AddFile ( string path , IFileInfo fileInfo )
64
+ {
65
+ _files [ path ] = fileInfo ;
66
+ }
67
+
68
+ public IDirectoryContents GetDirectoryContents ( string subpath )
69
+ {
70
+ return new NotFoundDirectoryContents ( ) ;
71
+ }
72
+
73
+ public IFileInfo GetFileInfo ( string subpath )
74
+ {
75
+ IFileInfo result ;
76
+ if ( _files . TryGetValue ( subpath , out result ) )
77
+ {
78
+ return result ;
79
+ }
80
+
81
+ return new NotFoundFileInfo ( subpath ) ;
82
+ }
83
+
84
+ public IExpirationTrigger Watch ( string filter )
85
+ {
86
+ throw new NotSupportedException ( ) ;
87
+ }
88
+ }
89
+
90
+ private sealed class TestFileInfo : IFileInfo
91
+ {
92
+ public bool Exists
93
+ {
94
+ get { return true ; }
95
+ }
96
+
97
+ public bool IsDirectory
98
+ {
99
+ get { return false ; }
100
+ }
101
+
102
+ public bool IsReadOnly
103
+ {
104
+ get { return false ; }
105
+ }
106
+
107
+ public DateTimeOffset LastModified { get ; set ; }
108
+
109
+ public long Length { get ; set ; }
110
+
111
+ public string Name { get ; set ; }
112
+
113
+ public string PhysicalPath { get ; set ; }
114
+
115
+ public Stream CreateReadStream ( )
116
+ {
117
+ throw new NotImplementedException ( ) ;
118
+ }
119
+
120
+ public void Delete ( )
121
+ {
122
+ throw new NotImplementedException ( ) ;
123
+ }
124
+
125
+ public void WriteContent ( byte [ ] content )
126
+ {
127
+ throw new NotImplementedException ( ) ;
128
+ }
129
+ }
130
+ }
131
+ }
0 commit comments