6
6
using System . Collections . Generic ;
7
7
using System . Collections . Specialized ;
8
8
using System . IO ;
9
+ using System . Linq ;
9
10
using System . Net ;
10
11
using System . Net . Http ;
11
12
using System . Net . Http . Headers ;
13
+ using System . Threading ;
12
14
using System . Threading . Tasks ;
13
15
using System . Web . Http ;
14
16
using System . Web . Http . Dependencies ;
15
17
using NuGet . Server . App_Start ;
18
+ using NuGet . Server . Core . Infrastructure ;
16
19
using NuGet . Server . Core . Tests ;
17
20
using NuGet . Server . Core . Tests . Infrastructure ;
18
21
using Xunit ;
@@ -56,6 +59,72 @@ public async Task DropPackageThenReadPackages()
56
59
}
57
60
}
58
61
62
+ [ Fact ]
63
+ public async Task DownloadPackage ( )
64
+ {
65
+ // Arrange
66
+ using ( var tc = new TestContext ( ) )
67
+ {
68
+ // Act & Assert
69
+ // 1. Write a package to the drop folder.
70
+ var packagePath = Path . Combine ( tc . PackagesDirectory , "package.nupkg" ) ;
71
+ TestData . CopyResourceToPath ( TestData . PackageResource , packagePath ) ;
72
+ var expectedBytes = File . ReadAllBytes ( packagePath ) ;
73
+
74
+ // 2. Download the package.
75
+ using ( var request = new HttpRequestMessage (
76
+ HttpMethod . Get ,
77
+ $ "/nuget/Packages(Id='{ TestData . PackageId } ',Version='{ TestData . PackageVersionString } ')/Download") )
78
+ using ( var response = await tc . Client . SendAsync ( request ) )
79
+ {
80
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
81
+ var actualBytes = await response . Content . ReadAsByteArrayAsync ( ) ;
82
+
83
+ Assert . Equal ( "binary/octet-stream" , response . Content . Headers . ContentType . ToString ( ) ) ;
84
+ Assert . Equal ( expectedBytes , actualBytes ) ;
85
+ }
86
+ }
87
+ }
88
+
89
+ [ Fact ]
90
+ public async Task FilterOnFramework ( )
91
+ {
92
+ // Arrange
93
+ using ( var tc = new TestContext ( ) )
94
+ {
95
+ tc . Settings [ "enableFrameworkFiltering" ] = "true" ;
96
+
97
+ // Act & Assert
98
+ // 1. Write a package to the drop folder.
99
+ var packagePath = Path . Combine ( tc . PackagesDirectory , "package.nupkg" ) ;
100
+ TestData . CopyResourceToPath ( TestData . PackageResource , packagePath ) ;
101
+
102
+ // 2. Search for all packages supporting .NET Framework 4.6 (this should match the test package)
103
+ using ( var request = new HttpRequestMessage (
104
+ HttpMethod . Get ,
105
+ $ "/nuget/Search?targetFramework='net46'") )
106
+ using ( var response = await tc . Client . SendAsync ( request ) )
107
+ {
108
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
109
+ var content = await response . Content . ReadAsStringAsync ( ) ;
110
+
111
+ Assert . Contains ( TestData . PackageId , content ) ;
112
+ }
113
+
114
+ // 3. Search for all packages supporting .NET Framework 2.0 (this should match nothing)
115
+ using ( var request = new HttpRequestMessage (
116
+ HttpMethod . Get ,
117
+ $ "/nuget/Search?targetFramework='net20'") )
118
+ using ( var response = await tc . Client . SendAsync ( request ) )
119
+ {
120
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
121
+ var content = await response . Content . ReadAsStringAsync ( ) ;
122
+
123
+ Assert . DoesNotContain ( TestData . PackageId , content ) ;
124
+ }
125
+ }
126
+ }
127
+
59
128
[ Fact ]
60
129
public async Task PushPackageThenReadPackages ( )
61
130
{
@@ -122,6 +191,75 @@ public async Task CanQueryUsingProjection(string endpoint)
122
191
}
123
192
}
124
193
194
+ [ Fact ]
195
+ public async Task DoesNotWriteToNuGetScratch ( )
196
+ {
197
+ // Arrange
198
+ OptimizedZipPackage . PurgeCache ( ) ;
199
+ var expectedTempEntries = Directory
200
+ . GetFileSystemEntries ( Path . Combine ( Path . GetTempPath ( ) , "NuGetScratch" ) )
201
+ . OrderBy ( x => x )
202
+ . ToList ( ) ;
203
+
204
+ using ( var tc = new TestContext ( ) )
205
+ {
206
+ tc . Settings [ "enableFrameworkFiltering" ] = "true" ;
207
+ tc . Settings [ "allowOverrideExistingPackageOnPush" ] = "true" ;
208
+
209
+ string apiKey = "foobar" ;
210
+ tc . SetApiKey ( apiKey ) ;
211
+
212
+ // Act & Assert
213
+ // 1. Write a package to the drop folder.
214
+ var packagePath = Path . Combine ( tc . PackagesDirectory , "package.nupkg" ) ;
215
+ TestData . CopyResourceToPath ( TestData . PackageResource , packagePath ) ;
216
+
217
+ // 2. Search for packages.
218
+ using ( var request = new HttpRequestMessage (
219
+ HttpMethod . Get ,
220
+ $ "/nuget/Search?targetFramework='net46'") )
221
+ using ( var response = await tc . Client . SendAsync ( request ) )
222
+ {
223
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
224
+ }
225
+
226
+ // 3. Push the package.
227
+ var pushPath = Path . Combine ( tc . TemporaryDirectory , "package.nupkg" ) ;
228
+ TestData . CopyResourceToPath ( TestData . PackageResource , pushPath ) ;
229
+ using ( var request = new HttpRequestMessage ( HttpMethod . Put , "/nuget" )
230
+ {
231
+ Headers =
232
+ {
233
+ { "X-NUGET-APIKEY" , apiKey }
234
+ } ,
235
+ Content = tc . GetFileUploadContent ( pushPath ) ,
236
+ } )
237
+ {
238
+ using ( request )
239
+ using ( var response = await tc . Client . SendAsync ( request ) )
240
+ {
241
+ Assert . Equal ( HttpStatusCode . Created , response . StatusCode ) ;
242
+ }
243
+ }
244
+
245
+ // 4. Search for packages again.
246
+ using ( var request = new HttpRequestMessage (
247
+ HttpMethod . Get ,
248
+ $ "/nuget/Search?targetFramework='net46'") )
249
+ using ( var response = await tc . Client . SendAsync ( request ) )
250
+ {
251
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
252
+ }
253
+
254
+ // 6. Make sure we have not added more temp files.
255
+ var actualTempEntries = Directory
256
+ . GetFileSystemEntries ( Path . Combine ( Path . GetTempPath ( ) , "NuGetScratch" ) )
257
+ . OrderBy ( x => x )
258
+ . ToList ( ) ;
259
+ Assert . Equal ( expectedTempEntries , actualTempEntries ) ;
260
+ }
261
+ }
262
+
125
263
public static IEnumerable < object [ ] > EndpointsSupportingProjection
126
264
{
127
265
get
@@ -135,7 +273,6 @@ public static IEnumerable<object[]> EndpointsSupportingProjection
135
273
private sealed class TestContext : IDisposable
136
274
{
137
275
private readonly HttpServer _server ;
138
- private readonly DefaultServiceResolver _serviceResolver ;
139
276
private readonly HttpConfiguration _config ;
140
277
141
278
public TestContext ( )
@@ -149,11 +286,11 @@ public TestContext()
149
286
{ "apiKey" , string . Empty }
150
287
} ;
151
288
152
- _serviceResolver = new DefaultServiceResolver ( PackagesDirectory , Settings ) ;
289
+ ServiceResolver = new DefaultServiceResolver ( PackagesDirectory , Settings ) ;
153
290
154
291
_config = new HttpConfiguration ( ) ;
155
292
_config . IncludeErrorDetailPolicy = IncludeErrorDetailPolicy . Always ;
156
- _config . DependencyResolver = new DependencyResolverAdapter ( _serviceResolver ) ;
293
+ _config . DependencyResolver = new DependencyResolverAdapter ( ServiceResolver ) ;
157
294
158
295
NuGetODataConfig . Initialize ( _config , "TestablePackagesOData" ) ;
159
296
@@ -162,6 +299,7 @@ public TestContext()
162
299
Client . BaseAddress = new Uri ( "http://localhost/" ) ;
163
300
}
164
301
302
+ public DefaultServiceResolver ServiceResolver { get ; }
165
303
public TemporaryDirectory TemporaryDirectory { get ; }
166
304
public TemporaryDirectory PackagesDirectory { get ; }
167
305
public NameValueCollection Settings { get ; }
@@ -198,7 +336,7 @@ public void Dispose()
198
336
Client . Dispose ( ) ;
199
337
_server . Dispose ( ) ;
200
338
_config . Dispose ( ) ;
201
- _serviceResolver . Dispose ( ) ;
339
+ ServiceResolver . Dispose ( ) ;
202
340
PackagesDirectory . Dispose ( ) ;
203
341
TemporaryDirectory . Dispose ( ) ;
204
342
}
0 commit comments