made test_http_connection use lighty as well (the test is more self contained)

This commit is contained in:
Arvid Norberg 2007-11-27 00:06:59 +00:00
parent 254e4ffce4
commit 8fc9f9e842
4 changed files with 54 additions and 29 deletions

View File

@ -1,4 +1,5 @@
#include <fstream>
#include <sstream>
#include "libtorrent/session.hpp"
#include "libtorrent/hasher.hpp"
@ -30,6 +31,27 @@ void test_sleep(int millisec)
boost::thread::sleep(xt);
}
void start_web_server(int port)
{
std::ofstream f("./lighty_config");
f << "server.modules = (\"mod_access\")\n"
"server.document-root = \"" << boost::filesystem::initial_path().string() << "\"\n"
"server.range-requests = \"enable\"\n"
"server.port = " << port << "\n"
"server.pid-file = \"./lighty" << port << ".pid\"\n";
f.close();
system("lighttpd -f lighty_config &");
test_sleep(1000);
}
void stop_web_server(int port)
{
std::stringstream cmd;
cmd << "kill `cat ./lighty" << port << ".pid`";
system(cmd.str().c_str());
}
using namespace libtorrent;
template <class T>

View File

@ -15,5 +15,8 @@ setup_transfer(libtorrent::session* ses1, libtorrent::session* ses2
, libtorrent::session* ses3, bool clear_files, bool use_metadata_transfer = true
, bool connect = true);
void start_web_server(int port);
void stop_web_server(int port);
#endif

View File

@ -2,6 +2,10 @@
#include "libtorrent/socket.hpp"
#include "libtorrent/connection_queue.hpp"
#include "libtorrent/http_connection.hpp"
#include "setup_transfer.hpp"
#include <fstream>
#include <boost/optional.hpp>
using namespace libtorrent;
@ -13,17 +17,17 @@ int handler_called = 0;
int data_size = 0;
int http_status = 0;
asio::error_code error_code;
char data_buffer[4000];
void print_http_header(http_parser const& p)
{
std::cerr << p.status_code() << " " << p.message() << std::endl;
std::cerr << " < " << p.status_code() << " " << p.message() << std::endl;
for (std::map<std::string, std::string>::const_iterator i
= p.headers().begin(), end(p.headers().end()); i != end; ++i)
{
std::cerr << i->first << ": " << i->second << std::endl;
std::cerr << " < " << i->first << ": " << i->second << std::endl;
}
}
void http_connect_handler(http_connection& c)
@ -40,7 +44,14 @@ void http_handler(asio::error_code const& ec, http_parser const& parser, char co
data_size = size;
error_code = ec;
if (parser.header_finished()) http_status = parser.status_code();
if (parser.header_finished())
{
http_status = parser.status_code();
if (http_status == 200)
{
TEST_CHECK(memcmp(data, data_buffer, size) == 0);
}
}
print_http_header(parser);
cq.close();
@ -55,7 +66,7 @@ void reset_globals()
error_code = asio::error_code();
}
void run_test(char const* url, int size, int status, int connected, asio::error_code const& ec)
void run_test(char const* url, int size, int status, int connected, boost::optional<asio::error_code> ec)
{
reset_globals();
@ -75,15 +86,22 @@ void run_test(char const* url, int size, int status, int connected, asio::error_
TEST_CHECK(connect_handler_called == connected);
TEST_CHECK(handler_called == 1);
TEST_CHECK(data_size == size || size == -1);
TEST_CHECK(error_code == ec);
TEST_CHECK(!ec || error_code == ec);
TEST_CHECK(http_status == status || status == -1);
}
int test_main()
{
run_test("http://127.0.0.1/disk_io.png", 17809, 200, 1, asio::error_code());
run_test("http://127.0.0.1/non-existing-file", -1, 404, 1, asio::error::eof);
run_test("http://non-existent-domain.se/non-existing-file", -1, -1, 0, asio::error::host_not_found);
typedef boost::optional<asio::error_code> err;
start_web_server(8001);
std::srand(std::time(0));
std::generate(data_buffer, data_buffer + sizeof(data_buffer), &std::rand);
std::ofstream("test_file").write(data_buffer, 3216);
run_test("http://127.0.0.1:8001/test_file", 3216, 200, 1, asio::error_code());
run_test("http://127.0.0.1:8001/non-existing-file", -1, 404, 1, err());
run_test("http://non-existent-domain.se/non-existing-file", -1, -1, 0, err(asio::error::host_not_found));
stop_web_server(8001);
std::remove("test_file");
return 0;
}

View File

@ -33,24 +33,6 @@ void add_files(
}
}
void start_web_server()
{
std::ofstream f("./lighty_config");
f << "server.modules = (\"mod_access\")\n"
"server.document-root = \"" << initial_path().string() << "\"\n"
"server.range-requests = \"enable\"\n"
"server.port = 8000\n"
"server.pid-file = \"./lighty.pid\"\n";
f.close();
system("lighttpd -f lighty_config &");
}
void stop_web_server()
{
system("kill `cat ./lighty.pid`");
}
void test_transfer()
{
using namespace libtorrent;
@ -72,7 +54,7 @@ void test_transfer()
add_files(*torrent_file, complete("."), "test_torrent");
start_web_server();
start_web_server(8000);
file_pool fp;
boost::scoped_ptr<storage_interface> s(default_storage_constructor(
@ -115,7 +97,7 @@ void test_transfer()
TEST_CHECK(th.is_seed());
remove_all("./test_torrent");
stop_web_server();
stop_web_server(8000);
}
int test_main()