Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2a7c685

Browse files
hughbestephentoub
authored andcommitted
Cleanup tests by allowing HttpListenerFactory to have a custom (empty) path
1 parent 6087666 commit 2a7c685

File tree

2 files changed

+41
-173
lines changed

2 files changed

+41
-173
lines changed

src/System.Net.HttpListener/tests/HttpListenerFactory.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Net.Sockets;
88
using System.Text;
9+
using Xunit;
910

1011
namespace System.Net.Tests
1112
{
@@ -19,19 +20,18 @@ internal class HttpListenerFactory : IDisposable
1920
private readonly string _path;
2021
private readonly int _port;
2122

22-
internal HttpListenerFactory(string hostname = "localhost")
23+
internal HttpListenerFactory(string hostname = "localhost", string path = null)
2324
{
2425
// Find a URL prefix that is not in use on this machine *and* uses a port that's not in use.
2526
// Once we find this prefix, keep a listener on it for the duration of the process, so other processes
2627
// can't steal it.
27-
28-
Guid processGuid = Guid.NewGuid();
2928
_hostname = hostname;
30-
_path = processGuid.ToString("N");
29+
_path = path ?? Guid.NewGuid().ToString("N");
30+
string pathComponent = string.IsNullOrEmpty(_path) ? _path : $"{_path}/";
3131

3232
for (int port = 1025; port <= IPEndPoint.MaxPort; port++)
3333
{
34-
string prefix = $"http://{hostname}:{port}/{_path}/";
34+
string prefix = $"http://{hostname}:{port}/{pathComponent}";
3535

3636
var listener = new HttpListener();
3737
try
@@ -113,8 +113,10 @@ public static bool SupportsWildcards
113113
{
114114
try
115115
{
116-
new HttpListenerFactory("*");
117-
s_supportsWildcards = true;
116+
using (new HttpListenerFactory("*"))
117+
{
118+
s_supportsWildcards = true;
119+
}
118120
}
119121
catch (InvalidOperationException)
120122
{

src/System.Net.HttpListener/tests/HttpListenerPrefixCollectionTests.cs

Lines changed: 32 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -272,208 +272,74 @@ public static IEnumerable<object[]> Hosts_TestData()
272272
[MemberData(nameof(Hosts_TestData))]
273273
public void Add_SamePortDifferentPathDifferentListenerNotStarted_Works(string host)
274274
{
275-
var listener1 = new HttpListener();
276-
var listener2 = new HttpListener();
277-
278-
int? freePort = null;
279-
280-
// Try to find a port that is not being used.
281-
for (int port = 1025; port <= IPEndPoint.MaxPort; port++)
275+
using (var factory1 = new HttpListenerFactory(host, path: string.Empty))
282276
{
283-
string uriPrefix = $"http://{host}:{port}/";
284-
try
285-
{
286-
listener1.Prefixes.Add(uriPrefix);
287-
Assert.Equal(1, listener1.Prefixes.Count);
288-
listener1.Start();
289-
290-
freePort = port;
291-
break;
292-
}
293-
catch (HttpListenerException)
277+
HttpListener listener1 = factory1.GetListener();
278+
using (var listener2 = new HttpListener())
294279
{
295-
// This port is already in use. Skip it and find a port that's not being used.
296-
listener1.Close();
297-
listener1 = new HttpListener();
298-
}
299-
}
280+
string prefixWithSubPath = $"{factory1.ListeningUrl}sub_path/";
281+
listener2.Prefixes.Add(prefixWithSubPath);
282+
Assert.Equal(prefixWithSubPath, Assert.Single(listener2.Prefixes));
300283

301-
try
302-
{
303-
if (!freePort.HasValue)
304-
{
305-
throw new InvalidOperationException("Expected to have a port open on the machine.");
284+
listener2.Start();
285+
Assert.True(listener2.IsListening);
306286
}
307-
308-
listener2.Prefixes.Add($"http://{host}:{freePort}/sub_path/");
309-
Assert.Equal(1, listener2.Prefixes.Count);
310-
311-
listener2.Start();
312-
Assert.True(listener2.IsListening);
313-
}
314-
finally
315-
{
316-
listener1?.Close();
317-
listener2?.Close();
318287
}
319288
}
320289

321290
[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
322291
[MemberData(nameof(Hosts_TestData))]
323292
public void Add_SamePortDifferentPathDifferentListenerStarted_Works(string host)
324293
{
325-
var listener1 = new HttpListener();
326-
var listener2 = new HttpListener();
327-
328-
int? freePort1 = null;
329-
int? freePort2 = null;
330-
331-
// Try to find a port that is not being used.
332-
for (int port = 1025; port <= IPEndPoint.MaxPort; port++)
294+
using (var factory1 = new HttpListenerFactory(host, path: string.Empty))
295+
using (var factory2 = new HttpListenerFactory(host, path: string.Empty))
333296
{
334-
string uriPrefix = $"http://127.0.0.1:{port}/";
335-
try
336-
{
337-
if (!freePort1.HasValue)
338-
{
339-
listener1.Prefixes.Add(uriPrefix);
340-
Assert.Equal(1, listener1.Prefixes.Count);
341-
342-
listener1.Start();
343-
freePort1 = port;
344-
}
345-
else if (!freePort2.HasValue)
346-
{
347-
listener2.Prefixes.Add(uriPrefix);
348-
Assert.Equal(1, listener2.Prefixes.Count);
297+
HttpListener listener1 = factory1.GetListener();
298+
HttpListener listener2 = factory2.GetListener();
299+
string prefix1 = factory1.ListeningUrl;
300+
string prefix2 = factory2.ListeningUrl;
349301

350-
listener2.Start();
351-
freePort2 = port;
352-
353-
break;
354-
}
355-
}
356-
catch (HttpListenerException)
357-
{
358-
// This port is already in use. Skip it and find a port that's not being used.
359-
if (!freePort1.HasValue)
360-
{
361-
listener1.Close();
362-
listener1 = new HttpListener();
363-
}
364-
else if (!freePort2.HasValue)
365-
{
366-
listener2.Close();
367-
listener2 = new HttpListener();
368-
}
369-
}
370-
}
371-
372-
try
373-
{
374-
if (!freePort1.HasValue || !freePort2.HasValue)
375-
{
376-
throw new InvalidOperationException("Expected to have a port open on the machine.");
377-
}
378-
379-
listener1.Prefixes.Add($"http://127.0.0.1:{freePort2}/hola/");
380-
listener2.Prefixes.Add($"http://127.0.0.1:{freePort1}/hola/");
302+
listener1.Prefixes.Add($"{prefix2}hola/");
303+
listener2.Prefixes.Add($"{prefix1}hola/");
381304

382305
Assert.Equal(2, listener1.Prefixes.Count);
383306
Assert.Equal(2, listener2.Prefixes.Count);
384307

385308
// Conflicts with existing registration: listener2 has registered to listen to http://127.0.0.1:{freePort1}/...
386-
Assert.Throws<HttpListenerException>(() => listener1.Prefixes.Add($"http://127.0.0.1:{freePort1}/"));
387-
Assert.Throws<HttpListenerException>(() => listener1.Prefixes.Add($"http://127.0.0.1:{freePort1}/hola/"));
309+
Assert.Throws<HttpListenerException>(() => listener1.Prefixes.Add(prefix1));
310+
Assert.Throws<HttpListenerException>(() => listener1.Prefixes.Add($"{prefix1}hola/"));
388311

389312
// Conflicts with existing registration: listener1 has registered to listen to http://127.0.0.1:{freePort2}/...
390-
Assert.Throws<HttpListenerException>(() => listener2.Prefixes.Add($"http://127.0.0.1:{freePort2}/"));
391-
Assert.Throws<HttpListenerException>(() => listener2.Prefixes.Add($"http://127.0.0.1:{freePort2}/hola/"));
392-
}
393-
finally
394-
{
395-
listener1?.Close();
396-
listener2?.Close();
313+
Assert.Throws<HttpListenerException>(() => listener2.Prefixes.Add(prefix2));
314+
Assert.Throws<HttpListenerException>(() => listener2.Prefixes.Add($"{prefix2}hola/"));
397315
}
398316
}
399317

400318
[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotOneCoreUAP))]
401319
[MemberData(nameof(Hosts_TestData))]
402320
public void Add_SamePortDifferentPathMultipleStarted_Success(string host)
403321
{
404-
var listener1 = new HttpListener();
405-
var listener2 = new HttpListener();
406-
407-
int? freePort1 = null;
408-
int? freePort2 = null;
409-
410-
// Try to find a port that is not being used.
411-
for (int port = 1025; port <= IPEndPoint.MaxPort; port++)
322+
using (var factory1 = new HttpListenerFactory(host, path: string.Empty))
323+
using (var factory2 = new HttpListenerFactory(host, path: string.Empty))
412324
{
413-
string uriPrefix = $"http://{host}:{port}/";
414-
try
415-
{
416-
if (!freePort1.HasValue)
417-
{
418-
listener1.Prefixes.Add(uriPrefix);
419-
Assert.Equal(1, listener1.Prefixes.Count);
420-
421-
listener1.Start();
422-
freePort1 = port;
423-
}
424-
else if (!freePort2.HasValue)
425-
{
426-
listener2.Prefixes.Add(uriPrefix);
427-
Assert.Equal(1, listener2.Prefixes.Count);
325+
HttpListener listener1 = factory1.GetListener();
326+
HttpListener listener2 = factory2.GetListener();
327+
string prefix1 = factory1.ListeningUrl;
328+
string prefix2 = factory2.ListeningUrl;
428329

429-
listener2.Start();
430-
freePort2 = port;
431-
432-
break;
433-
}
434-
}
435-
catch (HttpListenerException)
436-
{
437-
// This port is already in use. Skip it and find a port that's not being used.
438-
if (!freePort1.HasValue)
439-
{
440-
listener1.Close();
441-
listener1 = new HttpListener();
442-
}
443-
else if (!freePort2.HasValue)
444-
{
445-
listener2.Close();
446-
listener2 = new HttpListener();
447-
}
448-
}
449-
}
450-
451-
try
452-
{
453-
if (!freePort1.HasValue || !freePort2.HasValue)
454-
{
455-
throw new InvalidOperationException("Expected to have a port open on the machine.");
456-
}
457-
458-
listener1.Prefixes.Add($"http://{host}:{freePort1}/hola/");
330+
listener1.Prefixes.Add($"{prefix1}hola/");
459331
Assert.Equal(2, listener1.Prefixes.Count);
460332

461-
listener2.Prefixes.Add($"http://{host}:{freePort2}/hola/");
333+
listener2.Prefixes.Add($"{prefix2}hola/");
462334
Assert.Equal(2, listener2.Prefixes.Count);
463335

464336
// Conflict: listenerX is already listening to $"http://127.0.0.1:{freePortX}/hola/".
465-
Assert.Throws<HttpListenerException>(() => listener1.Prefixes.Add($"http://{host}:{freePort1}/hola/"));
466-
Assert.Throws<HttpListenerException>(() => listener2.Prefixes.Add($"http://{host}:{freePort2}/hola/"));
337+
Assert.Throws<HttpListenerException>(() => listener1.Prefixes.Add($"{prefix1}hola/"));
338+
Assert.Throws<HttpListenerException>(() => listener2.Prefixes.Add($"{prefix2}hola/"));
467339

468340
// Conflict: listenerX is already listening to $"http://127.0.0.1:{freePortY}/hola/".
469-
Assert.Throws<HttpListenerException>(() => listener1.Prefixes.Add($"http://{host}:{freePort2}/hola/"));
470-
Assert.Throws<HttpListenerException>(() => listener2.Prefixes.Add($"http://{host}:{freePort1}/hola/"));
471-
472-
}
473-
finally
474-
{
475-
listener1?.Close();
476-
listener2?.Close();
341+
Assert.Throws<HttpListenerException>(() => listener1.Prefixes.Add($"{prefix2}hola/"));
342+
Assert.Throws<HttpListenerException>(() => listener2.Prefixes.Add($"{prefix2}hola/"));
477343
}
478344
}
479345

0 commit comments

Comments
 (0)