1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.TestSupport
18 using System
.Configuration
;
23 /// Manages a <see cref="Cassini.Server"/> instance. This is useful
24 /// to start/stop a lightweight webserver to run acceptance tests.
26 public static class WebServer
28 private const string AppPathWeb
= "web.physical.dir";
30 private static string virtualDir
= "/";
31 private static int port
= 88;
32 private static bool started
;
33 private static Cassini
.Server server
;
36 /// Gets or sets the port to run the server. Defaults to 88.
38 /// <value>The port.</value>
39 public static int Port
46 /// Gets or sets the virtual dir to be used by the server. Defaults to <c>/</c>
48 /// <value>The virtual dir.</value>
49 public static string VirtualDir
51 get { return virtualDir; }
52 set { virtualDir = value; }
56 /// Gets a value indicating whether this <see cref="WebServer"/> is started.
58 /// <value><c>true</c> if started; otherwise, <c>false</c>.</value>
59 public static bool Started
61 get { return started; }
65 /// Starts the web server. The web project folder is going to be
66 /// extracted from the appSettings.webapp entry (from the configuration file)
68 /// If the path is relative, it is going to be converted to an absolute path.
71 public static void StartWebServer()
73 string webAppFromConfig
= ConfigurationManager
.AppSettings
[AppPathWeb
];
74 string webAppAbsPath
= new DirectoryInfo(Path
.Combine(AppDomain
.CurrentDomain
.BaseDirectory
, webAppFromConfig
)).FullName
;
76 StartWebServer(webAppAbsPath
);
80 /// Starts the web server using the specified web project path. Note
81 /// that the path must be absolute.
83 /// <param name="webApplicationAbsolutePath">The web application absolute path.</param>
84 public static void StartWebServer(string webApplicationAbsolutePath
)
86 if (!Directory
.Exists(webApplicationAbsolutePath
))
88 throw new ApplicationException("Cannot start web server as the path could not be found. " +
89 "Check if the following folder exists: " + webApplicationAbsolutePath
);
92 server
= new Server(88, VirtualDir
, webApplicationAbsolutePath
);
99 /// Stops the web server.
101 public static void StopWebServer()