// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Castle.MonoRail.TestSupport
{
using System;
using System.Configuration;
using System.IO;
using Cassini;
///
/// Manages a instance. This is useful
/// to start/stop a lightweight webserver to run acceptance tests.
///
public static class WebServer
{
private const string AppPathWeb = "web.physical.dir";
private static string virtualDir = "/";
private static int port = 88;
private static bool started;
private static Cassini.Server server;
///
/// Gets or sets the port to run the server. Defaults to 88.
///
/// The port.
public static int Port
{
get { return port; }
set { port = value; }
}
///
/// Gets or sets the virtual dir to be used by the server. Defaults to /
///
/// The virtual dir.
public static string VirtualDir
{
get { return virtualDir; }
set { virtualDir = value; }
}
///
/// Gets a value indicating whether this is started.
///
/// true if started; otherwise, false.
public static bool Started
{
get { return started; }
}
///
/// Starts the web server. The web project folder is going to be
/// extracted from the appSettings.webapp entry (from the configuration file)
///
/// If the path is relative, it is going to be converted to an absolute path.
///
///
public static void StartWebServer()
{
string webAppFromConfig = ConfigurationManager.AppSettings[AppPathWeb];
string webAppAbsPath = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, webAppFromConfig)).FullName;
StartWebServer(webAppAbsPath);
}
///
/// Starts the web server using the specified web project path. Note
/// that the path must be absolute.
///
/// The web application absolute path.
public static void StartWebServer(string webApplicationAbsolutePath)
{
if (!Directory.Exists(webApplicationAbsolutePath))
{
throw new ApplicationException("Cannot start web server as the path could not be found. " +
"Check if the following folder exists: " + webApplicationAbsolutePath);
}
server = new Server(88, VirtualDir, webApplicationAbsolutePath);
server.Start();
started = true;
}
///
/// Stops the web server.
///
public static void StopWebServer()
{
if (started)
{
server.Stop();
}
}
}
}