Browse Source

add boilerplate for torrent resource

master
Jørgen Lien Sellæg 9 years ago
parent
commit
ff47d4c68a
  1. 32
      toREST/include/torrent.hpp
  2. 9
      toREST/src/main.cxx
  3. 25
      toREST/tests/torrent_test.cpp

32
toREST/include/torrent.hpp

@ -0,0 +1,32 @@
#ifndef _TR_TORRENT_HPP_
#define _TR_TORRENT_HPP_
#include <http.hpp>
namespace tr {
namespace session {
namespace torrents {
namespace id {
template <class torrent_session, class request, class response>
void get(torrent_session &session, response resp, request req) {
auto http_response = http::response();
const auto respond = [&](http::status status) {
const auto response_code = http::code(status);
http_response.set_body({{"code", response_code.first}, {"status", response_code.second}});
http_response.set_status(response_code.first);
*resp << http_response;
};
if (!session.is_valid()) {
return respond(http::service_unavailable);
}
}
template <class settings, class torrent_session, class request, class response>
void patch(settings opts, torrent_session &session, response resp, request req) {
}
}
}
}
}
#endif

9
toREST/src/main.cxx

@ -6,6 +6,7 @@
#include <config.hpp>
#include <server_http.hpp>
#include <session.hpp>
#include <torrent.hpp>
#include <torrents.hpp>
typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
@ -56,6 +57,14 @@ int main(int argc, char *argv[]) {
tr::session::torrents::del(session, resp, req);
};
http_server.resource["^/session/torrents/([0-9a-fA-F]){40}(\\?.*)?\\/?$"]["GET"] = [&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) {
tr::session::torrents::id::get(session, resp, req);
};
http_server.resource["^/session/torrents/([0-9a-fA-F]){40}(\\?.*)?\\/?$"]["PATCH"] = [&](std::shared_ptr<HttpServer::Response> resp, std::shared_ptr<HttpServer::Request> req) {
tr::session::torrents::id::patch(options, session, resp, req);
};
std::thread server_thread([&http_server]() {
http_server.start();
});

25
toREST/tests/torrent_test.cpp

@ -0,0 +1,25 @@
#include <ServerContext.hpp>
#include <SessionContext.hpp>
#include <TorrentContext.hpp>
#include <torrent.hpp>
SCENARIO("We are running a GET /session/torrents/id resource") {
auto torrent_session = TestSession();
auto response = std::make_shared<TestResponse>();
auto request = std::make_shared<TestRequest>();
GIVEN("the server is not working properly") {
WHEN("we recive a request") {
tr::session::torrents::id::get(torrent_session, response, request);
THEN("the server should reply with service unavailable") {
CommonResponse::service_unavailable(response);
}
}
}
GIVEN("the server is working properly") {
torrent_session.valid = true;
THEN("the server should reply with service unavailable") {
tr::session::torrents::id::get(torrent_session, response, request);
CommonResponse::ok(response, {});
}
}
}
Loading…
Cancel
Save