Add response builder to simplify the response build; refine the service interfaces.

This commit is contained in:
Chunting Gu
2019-06-21 17:41:02 +08:00
parent 4140b4f94c
commit 8a7f53313b
14 changed files with 316 additions and 207 deletions
+27 -29
View File
@@ -6,9 +6,9 @@
class MyService : public webcc::Service {
public:
void Handle(const webcc::RestRequest& request,
webcc::RestResponse* response) override {
response->status = webcc::Status::kOK;
webcc::ResponsePtr Handle(webcc::RequestPtr request,
const webcc::UrlArgs& args) override {
return webcc::ResponseBuilder{}.OK()();
}
};
@@ -17,22 +17,21 @@ public:
TEST(ServiceManagerTest, URL_RegexBasic) {
webcc::ServiceManager service_manager;
service_manager.AddService(std::make_shared<MyService>(),
"/instance/(\\d+)", true);
std::vector<std::string> matches;
service_manager.Add(std::make_shared<MyService>(), "/instance/(\\d+)", true);
std::string url = "/instance/12345";
webcc::ServicePtr service = service_manager.GetService(url, &matches);
webcc::UrlArgs args;
webcc::ServicePtr service = service_manager.Get(url, &args);
EXPECT_TRUE(!!service);
EXPECT_EQ(1, matches.size());
EXPECT_EQ("12345", matches[0]);
EXPECT_EQ(1, args.size());
EXPECT_EQ("12345", args[0]);
url = "/instance/abcde";
matches.clear();
service = service_manager.GetService(url, &matches);
args.clear();
service = service_manager.Get(url, &args);
EXPECT_FALSE(!!service);
}
@@ -40,25 +39,24 @@ TEST(ServiceManagerTest, URL_RegexBasic) {
TEST(RestServiceManagerTest, URL_RegexMultiple) {
webcc::ServiceManager service_manager;
service_manager.AddService(std::make_shared<MyService>(),
"/study/(\\d+)/series/(\\d+)/instance/(\\d+)",
true);
std::vector<std::string> matches;
service_manager.Add(std::make_shared<MyService>(),
"/study/(\\d+)/series/(\\d+)/instance/(\\d+)", true);
std::string url = "/study/1/series/2/instance/3";
webcc::ServicePtr service = service_manager.GetService(url, &matches);
webcc::UrlArgs args;
webcc::ServicePtr service = service_manager.Get(url, &args);
EXPECT_TRUE(!!service);
EXPECT_EQ(3, matches.size());
EXPECT_EQ("1", matches[0]);
EXPECT_EQ("2", matches[1]);
EXPECT_EQ("3", matches[2]);
EXPECT_EQ(3, args.size());
EXPECT_EQ("1", args[0]);
EXPECT_EQ("2", args[1]);
EXPECT_EQ("3", args[2]);
url = "/study/a/series/b/instance/c";
matches.clear();
service = service_manager.GetService(url, &matches);
args.clear();
service = service_manager.Get(url, &args);
EXPECT_FALSE(!!service);
}
@@ -66,13 +64,13 @@ TEST(RestServiceManagerTest, URL_RegexMultiple) {
TEST(RestServiceManagerTest, URL_NonRegex) {
webcc::ServiceManager service_manager;
service_manager.AddService(std::make_shared<MyService>(), "/instances",
false);
service_manager.Add(std::make_shared<MyService>(), "/instances", false);
std::vector<std::string> matches;
std::string url = "/instances";
webcc::ServicePtr service = service_manager.GetService(url, &matches);
webcc::UrlArgs args;
webcc::ServicePtr service = service_manager.Get(url, &args);
EXPECT_TRUE(!!service);
EXPECT_EQ(0, matches.size());
EXPECT_TRUE(args.empty());
}
+6 -6
View File
@@ -8,7 +8,7 @@ TEST(UrlTest, Basic) {
EXPECT_EQ("http", url.scheme());
EXPECT_EQ("example.com", url.host());
EXPECT_EQ("", url.port());
EXPECT_EQ("path", url.path());
EXPECT_EQ("/path", url.path());
EXPECT_EQ("", url.query());
}
@@ -28,7 +28,7 @@ TEST(UrlTest, NoPath2) {
EXPECT_EQ("http", url.scheme());
EXPECT_EQ("example.com", url.host());
EXPECT_EQ("", url.port());
EXPECT_EQ("", url.path());
EXPECT_EQ("/", url.path());
EXPECT_EQ("", url.query());
}
@@ -48,7 +48,7 @@ TEST(UrlTest, NoPath4) {
EXPECT_EQ("http", url.scheme());
EXPECT_EQ("example.com", url.host());
EXPECT_EQ("", url.port());
EXPECT_EQ("", url.path());
EXPECT_EQ("/", url.path());
EXPECT_EQ("key=value", url.query());
}
@@ -58,7 +58,7 @@ TEST(UrlTest, NoScheme) {
EXPECT_EQ("", url.scheme());
EXPECT_EQ("", url.host());
EXPECT_EQ("", url.port());
EXPECT_EQ("path/to", url.path());
EXPECT_EQ("/path/to", url.path());
EXPECT_EQ("", url.query());
}
@@ -68,7 +68,7 @@ TEST(UrlTest, NoScheme2) {
EXPECT_EQ("", url.scheme());
EXPECT_EQ("", url.host());
EXPECT_EQ("", url.port());
EXPECT_EQ("path/to", url.path());
EXPECT_EQ("/path/to", url.path());
EXPECT_EQ("key=value", url.query());
}
@@ -78,6 +78,6 @@ TEST(UrlTest, Full) {
EXPECT_EQ("https", url.scheme());
EXPECT_EQ("localhost", url.host());
EXPECT_EQ("3000", url.port());
EXPECT_EQ("path/to", url.path());
EXPECT_EQ("/path/to", url.path());
EXPECT_EQ("key=value", url.query());
}