-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.cpp
99 lines (89 loc) · 2.65 KB
/
string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// Created by Rakesh on 02/04/2022.
//
#if __GNUC__ > 10 || defined _WIN32
#include <catch2/catch.hpp>
#else
#include <catch2/catch_test_macros.hpp>
#endif
#include <map>
#include <unordered_map>
#include "../src/router.hpp"
using namespace std::string_literals;
using namespace std::string_view_literals;
SCENARIO( "String parameters test suite" )
{
struct Request {} request;
using Params = std::unordered_map<std::string, std::string>;
GIVEN( "Router instantiated with string parameters" )
{
const auto method = "GET"sv;
spt::http::router::HttpRouter<const Request&, Params, Params> r;
r.add( method, "/device/sensor/", []( const Request&, auto args )
{
REQUIRE( args.size() == 0 );
return args;
} );
r.add( method, "/device/sensor/id/{id}", []( const Request&, auto args )
{
REQUIRE( args.size() == 1 );
return args;
} );
WHEN( "Testing /device/sensor/" )
{
auto url = "/device/sensor/"s;
auto resp = r.route( method, url, request );
REQUIRE( resp );
REQUIRE( resp->empty() );
CHECK( r.canRoute( method, url ) );
}
AND_WHEN( "Testing /device/sensor/id/6230f3069e7c9be9ff4b78a1")
{
auto url = "/device/sensor/id/6230f3069e7c9be9ff4b78a1"s;
auto resp = r.route( method, url, request );
REQUIRE( resp );
REQUIRE( resp->contains( "id"s ) );
REQUIRE( (*resp)["id"s] == "6230f3069e7c9be9ff4b78a1"s );
CHECK( r.canRoute( method, url ) );
}
}
}
#ifdef HAS_BOOST
SCENARIO( "map parameters test suite" )
{
struct Request {} request;
using Params = std::map<std::string, std::string>;
GIVEN( "Router instantiated with string parameters" )
{
const auto method = "GET"sv;
spt::http::router::HttpRouter<const Request&, Params, Params> r;
r.add( method, "/device/sensor/", []( const Request&, auto args )
{
REQUIRE( args.size() == 0 );
return args;
} );
r.add( method, "/device/sensor/id/{id}", []( const Request&, auto args )
{
REQUIRE( args.size() == 1 );
return args;
} );
WHEN( "Testing /device/sensor/" )
{
auto url = "/device/sensor/"s;
auto resp = r.route( method, url, request );
REQUIRE( resp );
REQUIRE( resp->empty() );
CHECK( r.canRoute( method, url ) );
}
AND_WHEN( "Testing /device/sensor/id/6230f3069e7c9be9ff4b78a1")
{
auto url = "/device/sensor/id/6230f3069e7c9be9ff4b78a1"s;
auto resp = r.route( method, url, request );
REQUIRE( resp );
REQUIRE( resp->contains( "id"s ) );
REQUIRE( (*resp)["id"s] == "6230f3069e7c9be9ff4b78a1"s );
CHECK( r.canRoute( method, url ) );
}
}
}
#endif