fileserver/lib/restbed-4.8/test/feature/source/http_client/fetch.cpp

131 lines
4.0 KiB
C++
Raw Normal View History

//System Includes
#include <map>
#include <thread>
#include <string>
#include <memory>
#include <ciso646>
#include <stdexcept>
#include <functional>
//Project Includes
#include <restbed>
//External Includes
#include <catch.hpp>
//System Namespaces
using std::thread;
using std::string;
using std::multimap;
using std::shared_ptr;
using std::make_shared;
using std::runtime_error;
using std::invalid_argument;
//Project Namespaces
using namespace restbed;
//External Namespaces
void get_handler( const shared_ptr< Session > session )
{
session->close( 200, "Hello, World!", { { "Content-Length", "13" }, { "Connection", "close" } } );
}
SCENARIO( "fetch response body", "[client]" )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resource" );
resource->set_method_handler( "GET", get_handler );
auto settings = make_shared< Settings >( );
settings->set_port( 1984 );
shared_ptr< thread > worker = nullptr;
Service service;
service.publish( resource );
service.set_ready_handler( [ &worker ]( Service & service )
{
worker = make_shared< thread >( [ &service ] ( )
{
GIVEN( "I publish a resource at '/resource' with a HTTP 'GET' method handler" )
{
WHEN( "I perform a HTTP 'GET' request and fetch the response body" )
{
auto request = make_shared< Request >( );
request->set_port( 1984 );
request->set_host( "localhost" );
request->set_method( "GET" );
request->set_path( "/resource" );
auto response = Http::sync( request );
THEN( "I should see a '200' (OK) status code" )
{
REQUIRE( 200 == response->get_status_code( ) );
}
AND_THEN( "I should see a response body of 'Hello, World!'" )
{
auto actual = Http::fetch( 13, response );
Bytes expectation { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' };
REQUIRE( actual == expectation );
}
multimap< string, string > headers = response->get_headers( );
AND_THEN( "I should see a 'Connection' header value of 'close'" )
{
auto header = headers.find( "Connection" );
REQUIRE( header not_eq headers.end( ) );
REQUIRE( "close" == headers.find( "Connection" )->second );
}
AND_THEN( "I should see a 'Content-Length' header value of '13'" )
{
auto header = headers.find( "Content-Length" );
REQUIRE( header not_eq headers.end( ) );
REQUIRE( "13" == headers.find( "Content-Length" )->second );
}
}
service.stop( );
}
} );
} );
service.start( settings );
worker->join( );
}
SCENARIO( "invoke fetch on default response", "[client]" )
{
GIVEN( "I create a default response instance" )
{
auto response = make_shared< Response >( );
WHEN( "I invoke fetch" )
{
THEN( "I should see an invalid_argument" )
{
REQUIRE_THROWS_AS( Http::fetch( 13, response ), invalid_argument );
}
}
}
}
SCENARIO( "invoke fetch on null response", "[client]" )
{
GIVEN( "I create a null response instance" )
{
WHEN( "I invoke fetch" )
{
THEN( "I should see an invalid_argument" )
{
REQUIRE_THROWS_AS( Http::fetch( 13, nullptr ), invalid_argument );
}
}
}
}