2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . IO ;
7
+ using System . Linq ;
6
8
using System . Net ;
7
9
using System . Threading . Tasks ;
8
10
using Microsoft . AspNetCore . Builder ;
@@ -29,6 +31,24 @@ public async Task ReturnsNotFoundWithoutWwwroot()
29
31
Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
30
32
}
31
33
34
+ public async Task FoundFile_LastModifiedTrimsSeconds ( )
35
+ {
36
+ using ( var fileProvider = new PhysicalFileProvider ( Directory . GetCurrentDirectory ( ) ) )
37
+ {
38
+ var server = StaticFilesTestServer . Create ( app => app . UseStaticFiles ( new StaticFileOptions
39
+ {
40
+ FileProvider = fileProvider
41
+ } ) ) ;
42
+ var fileInfo = fileProvider . GetFileInfo ( "TestDocument.txt" ) ;
43
+ var response = await server . CreateRequest ( "TestDocument.txt" ) . GetAsync ( ) ;
44
+
45
+ var last = fileInfo . LastModified ;
46
+ var trimed = new DateTimeOffset ( last . Year , last . Month , last . Day , last . Hour , last . Minute , last . Second , last . Offset ) . ToUniversalTime ( ) ;
47
+
48
+ Assert . Equal ( response . Content . Headers . LastModified . Value , trimed ) ;
49
+ }
50
+ }
51
+
32
52
[ Fact ]
33
53
public async Task NullArguments ( )
34
54
{
@@ -45,30 +65,7 @@ public async Task NullArguments()
45
65
}
46
66
47
67
[ Theory ]
48
- [ InlineData ( "" , @"." , "/missing.file" ) ]
49
- [ InlineData ( "/subdir" , @"." , "/subdir/missing.file" ) ]
50
- [ InlineData ( "/missing.file" , @"./" , "/missing.file" ) ]
51
- [ InlineData ( "" , @"./" , "/xunit.xml" ) ]
52
- public async Task NoMatch_PassesThrough ( string baseUrl , string baseDir , string requestUrl )
53
- {
54
- using ( var fileProvider = new PhysicalFileProvider ( Path . Combine ( Directory . GetCurrentDirectory ( ) , baseDir ) ) )
55
- {
56
- var server = StaticFilesTestServer . Create ( app => app . UseStaticFiles ( new StaticFileOptions
57
- {
58
- RequestPath = new PathString ( baseUrl ) ,
59
- FileProvider = fileProvider
60
- } ) ) ;
61
- var response = await server . CreateRequest ( requestUrl ) . GetAsync ( ) ;
62
- Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
63
- }
64
- }
65
-
66
- [ Theory ]
67
- [ InlineData ( "" , @"." , "/TestDocument.txt" ) ]
68
- [ InlineData ( "/somedir" , @"." , "/somedir/TestDocument.txt" ) ]
69
- [ InlineData ( "/SomeDir" , @"." , "/soMediR/TestDocument.txt" ) ]
70
- [ InlineData ( "" , @"SubFolder" , "/ranges.txt" ) ]
71
- [ InlineData ( "/somedir" , @"SubFolder" , "/somedir/ranges.txt" ) ]
68
+ [ MemberData ( nameof ( ExistingFiles ) ) ]
72
69
public async Task FoundFile_Served_All ( string baseUrl , string baseDir , string requestUrl )
73
70
{
74
71
await FoundFile_Served ( baseUrl , baseDir , requestUrl ) ;
@@ -95,22 +92,27 @@ public async Task FoundFile_Served(string baseUrl, string baseDir, string reques
95
92
RequestPath = new PathString ( baseUrl ) ,
96
93
FileProvider = fileProvider
97
94
} ) ) ;
95
+ var fileInfo = fileProvider . GetFileInfo ( Path . GetFileName ( requestUrl ) ) ;
98
96
var response = await server . CreateRequest ( requestUrl ) . GetAsync ( ) ;
97
+ var responseContent = await response . Content . ReadAsByteArrayAsync ( ) ;
99
98
100
99
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
101
100
Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
102
- Assert . True ( response . Content . Headers . ContentLength > 0 ) ;
103
- Assert . Equal ( response . Content . Headers . ContentLength , ( await response . Content . ReadAsByteArrayAsync ( ) ) . Length ) ;
101
+ Assert . True ( response . Content . Headers . ContentLength == fileInfo . Length ) ;
102
+ Assert . Equal ( response . Content . Headers . ContentLength , responseContent . Length ) ;
103
+
104
+ using ( var stream = fileInfo . CreateReadStream ( ) )
105
+ {
106
+ var fileContents = new byte [ stream . Length ] ;
107
+ stream . Read ( fileContents , 0 , ( int ) stream . Length ) ;
108
+ Assert . True ( responseContent . SequenceEqual ( fileContents ) ) ;
109
+ }
104
110
}
105
111
}
106
112
107
113
[ Theory ]
108
- [ InlineData ( "" , @"." , "/TestDocument.txt" ) ]
109
- [ InlineData ( "/somedir" , @"." , "/somedir/TestDocument.txt" ) ]
110
- [ InlineData ( "/SomeDir" , @"." , "/soMediR/TestDocument.txt" ) ]
111
- [ InlineData ( "" , @"SubFolder" , "/ranges.txt" ) ]
112
- [ InlineData ( "/somedir" , @"SubFolder" , "/somedir/ranges.txt" ) ]
113
- public async Task PostFile_PassesThrough ( string baseUrl , string baseDir , string requestUrl )
114
+ [ MemberData ( nameof ( ExistingFiles ) ) ]
115
+ public async Task HeadFile_HeadersButNotBodyServed ( string baseUrl , string baseDir , string requestUrl )
114
116
{
115
117
using ( var fileProvider = new PhysicalFileProvider ( Path . Combine ( Directory . GetCurrentDirectory ( ) , baseDir ) ) )
116
118
{
@@ -119,18 +121,57 @@ public async Task PostFile_PassesThrough(string baseUrl, string baseDir, string
119
121
RequestPath = new PathString ( baseUrl ) ,
120
122
FileProvider = fileProvider
121
123
} ) ) ;
122
- var response = await server . CreateRequest ( requestUrl ) . PostAsync ( ) ;
123
- Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
124
+ var fileInfo = fileProvider . GetFileInfo ( Path . GetFileName ( requestUrl ) ) ;
125
+ var response = await server . CreateRequest ( requestUrl ) . SendAsync ( "HEAD" ) ;
126
+
127
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
128
+ Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
129
+ Assert . True ( response . Content . Headers . ContentLength == fileInfo . Length ) ;
130
+ Assert . Equal ( 0 , ( await response . Content . ReadAsByteArrayAsync ( ) ) . Length ) ;
124
131
}
125
132
}
126
133
127
134
[ Theory ]
128
- [ InlineData ( "" , @"." , "/TestDocument.txt" ) ]
129
- [ InlineData ( "/somedir" , @"." , "/somedir/TestDocument.txt" ) ]
130
- [ InlineData ( "/SomeDir" , @"." , "/soMediR/TestDocument.txt" ) ]
131
- [ InlineData ( "" , @"SubFolder" , "/ranges.txt" ) ]
132
- [ InlineData ( "/somedir" , @"SubFolder" , "/somedir/ranges.txt" ) ]
133
- public async Task HeadFile_HeadersButNotBodyServed ( string baseUrl , string baseDir , string requestUrl )
135
+ [ MemberData ( nameof ( MissingFiles ) ) ]
136
+ public async Task Get_NoMatch_PassesThrough ( string baseUrl , string baseDir , string requestUrl ) =>
137
+ await PassesThrough ( "GET" , baseUrl , baseDir , requestUrl ) ;
138
+
139
+ [ Theory ]
140
+ [ MemberData ( nameof ( MissingFiles ) ) ]
141
+ public async Task Head_NoMatch_PassesThrough ( string baseUrl , string baseDir , string requestUrl ) =>
142
+ await PassesThrough ( "HEAD" , baseUrl , baseDir , requestUrl ) ;
143
+
144
+ [ Theory ]
145
+ [ MemberData ( nameof ( MissingFiles ) ) ]
146
+ public async Task Unknown_NoMatch_PassesThrough ( string baseUrl , string baseDir , string requestUrl ) =>
147
+ await PassesThrough ( "VERB" , baseUrl , baseDir , requestUrl ) ;
148
+
149
+ [ Theory ]
150
+ [ MemberData ( nameof ( ExistingFiles ) ) ]
151
+ public async Task Options_Match_PassesThrough ( string baseUrl , string baseDir , string requestUrl ) =>
152
+ await PassesThrough ( "OPTIONS" , baseUrl , baseDir , requestUrl ) ;
153
+
154
+ [ Theory ]
155
+ [ MemberData ( nameof ( ExistingFiles ) ) ]
156
+ public async Task Trace_Match_PassesThrough ( string baseUrl , string baseDir , string requestUrl ) =>
157
+ await PassesThrough ( "TRACE" , baseUrl , baseDir , requestUrl ) ;
158
+
159
+ [ Theory ]
160
+ [ MemberData ( nameof ( ExistingFiles ) ) ]
161
+ public async Task Post_Match_PassesThrough ( string baseUrl , string baseDir , string requestUrl ) =>
162
+ await PassesThrough ( "POST" , baseUrl , baseDir , requestUrl ) ;
163
+
164
+ [ Theory ]
165
+ [ MemberData ( nameof ( ExistingFiles ) ) ]
166
+ public async Task Put_Match_PassesThrough ( string baseUrl , string baseDir , string requestUrl ) =>
167
+ await PassesThrough ( "PUT" , baseUrl , baseDir , requestUrl ) ;
168
+
169
+ [ Theory ]
170
+ [ MemberData ( nameof ( ExistingFiles ) ) ]
171
+ public async Task Unknown_Match_PassesThrough ( string baseUrl , string baseDir , string requestUrl ) =>
172
+ await PassesThrough ( "VERB" , baseUrl , baseDir , requestUrl ) ;
173
+
174
+ public async Task PassesThrough ( string method , string baseUrl , string baseDir , string requestUrl )
134
175
{
135
176
using ( var fileProvider = new PhysicalFileProvider ( Path . Combine ( Directory . GetCurrentDirectory ( ) , baseDir ) ) )
136
177
{
@@ -139,13 +180,28 @@ public async Task HeadFile_HeadersButNotBodyServed(string baseUrl, string baseDi
139
180
RequestPath = new PathString ( baseUrl ) ,
140
181
FileProvider = fileProvider
141
182
} ) ) ;
142
- var response = await server . CreateRequest ( requestUrl ) . SendAsync ( "HEAD" ) ;
143
-
144
- Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
145
- Assert . Equal ( "text/plain" , response . Content . Headers . ContentType . ToString ( ) ) ;
146
- Assert . True ( response . Content . Headers . ContentLength > 0 ) ;
147
- Assert . Equal ( 0 , ( await response . Content . ReadAsByteArrayAsync ( ) ) . Length ) ;
183
+ var response = await server . CreateRequest ( requestUrl ) . SendAsync ( method ) ;
184
+ Assert . Null ( response . Content . Headers . LastModified ) ;
185
+ Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
148
186
}
149
187
}
188
+
189
+ public static IEnumerable < string [ ] > MissingFiles => new [ ]
190
+ {
191
+ new [ ] { "" , @"." , "/missing.file" } ,
192
+ new [ ] { "/subdir" , @"." , "/subdir/missing.file" } ,
193
+ new [ ] { "/missing.file" , @"./" , "/missing.file" } ,
194
+ new [ ] { "" , @"./" , "/xunit.xml" }
195
+ } ;
196
+
197
+ public static IEnumerable < string [ ] > ExistingFiles => new [ ]
198
+ {
199
+ new [ ] { "" , @"." , "/TestDocument.txt" } ,
200
+ new [ ] { "/somedir" , @"." , "/somedir/TestDocument.txt" } ,
201
+ new [ ] { "/SomeDir" , @"." , "/soMediR/TestDocument.txt" } ,
202
+ new [ ] { "" , @"SubFolder" , "/ranges.txt" } ,
203
+ new [ ] { "/somedir" , @"SubFolder" , "/somedir/ranges.txt" } ,
204
+ new [ ] { "" , @"SubFolder" , "/Empty.txt" }
205
+ } ;
150
206
}
151
207
}
0 commit comments