forked from ravendb/ravendb
/
IISClientTest.cs
109 lines (92 loc) · 3.35 KB
/
IISClientTest.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
using System;
using System.IO;
using System.Net;
using System.Threading;
using Raven.Client;
using Raven.Client.Document;
using Raven.Database.Extensions;
using Raven.Tests.Util;
using Xunit;
namespace Raven.Tests
{
public class IISClientTest
{
const string HostName = "RavenIISTest";
const int Port = 80;
const string ApplicationPool = "RavenIISTest";
protected const string WebDirectory = @".\RavenIISTestWeb\";
protected const string DbDirectory = @".\RavenIISTestDb\";
public IISClientTest()
{
IISConfig.RemoveByApplicationPool(ApplicationPool);
IOExtensions.DeleteDirectory(WebDirectory);
IOExtensions.DeleteDirectory(DbDirectory);
}
public IDocumentStore GetDocumentStore()
{
if (!Directory.Exists(WebDirectory))
{
var fullPath = Path.GetFullPath(WebDirectory);
IOExtensions.CopyDirectory(GetRavenWebSource(), WebDirectory);
PSHostsFile.HostsFile.Set(HostName, "127.0.0.1");
IISConfig.CreateApplicationPool(ApplicationPool);
IISConfig.CreateSite(HostName, Port, ApplicationPool, fullPath);
VerifySiteIsAwake();
}
return new DocumentStore()
{
Url = GetUrl()
};
}
private void VerifySiteIsAwake()
{
var retriesLeft = 3;
do
{
try
{
Console.WriteLine();
Console.WriteLine("Downloading " + GetUrl());
Console.WriteLine();
var request = HttpWebRequest.Create(GetUrl()) as HttpWebRequest;
using (var response = request.GetResponse() as HttpWebResponse)
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
Assert.Contains("<title>Raven Studio", responseText);
}
}
}
catch (Exception)
{
if (--retriesLeft > 0)
{
Thread.Sleep(1000);
continue;
}
else
throw;
}
break;
} while (true);
}
private string GetUrl()
{
return string.Format("http://{0}:{1}/", HostName, Port);
}
string GetRavenWebSource()
{
foreach (var path in new[] { @".\..\..\..\Raven.Web", @".\_PublishedWebsites\Raven.Web" })
{
var fullPath = Path.GetFullPath(path);
if (Directory.Exists(fullPath) && Directory.Exists(Path.Combine(fullPath, "bin")))
{
return fullPath;
}
}
throw new FileNotFoundException("Could not find source directory for Raven.Web");
}
}
}