示例#1
0
int main(int argc, char *argv[])
{
  QCoreApplication app(argc, argv);

  HttpServer* svr = HttpServer::getInstance();
  svr->initialize();

  auto helloworld = svr->createAction([](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    json["response"] = "Hello World!";
  });

  helloworld->registerRoute(HttpMethod::GET, "/helloworld");

  auto echobody = svr->createAction([](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    json["response"] = data.getRequest().getJson();
  });

  echobody->registerRoute(HttpMethod::POST, "/echobody");

  svr->startServer();
  return app.exec();
}
示例#2
0
void MonkeyTest::initTestCase()
{
  HttpServer* httpSvr = TestUtils::setUp(this);

  QVERIFY(httpSvr != nullptr);
  QVERIFY(httpSvr->initialize() == true);

  auto action = httpSvr->createAction([](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    json["response"] = "C++ FTW";
  });
  action->registerRoute(HttpMethod::GET, "something");

  httpSvr->startServer("127.0.0.1", 8080);
  QTest::qWait(1000);
}
示例#3
0
void QttpTest::initTestCase()
{
  HttpServer* httpSvr = TestUtils::setUp(this);

  QVERIFY(httpSvr != nullptr);
  QVERIFY(httpSvr->initialize() == true);

  auto action = httpSvr->createAction("", [](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    json["response"] = "C++ FTW";
  });

  httpSvr->addProcessor<SampleProcessor>();

  action = httpSvr->createAction("echo", [](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    auto& query = data.getRequest().getQuery();
    json["response"] = "C++ FTW " + query.queryItemValue("id");
  });

  auto result = httpSvr->registerRoute("get", "", "/echo/:id");
  QVERIFY(result == true);

  result = httpSvr->registerRoute("get", "echo", "/echo/:id/data");
  QVERIFY(result == true);

  action = httpSvr->createAction("echobody", [](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    json["response"] = data.getRequest().getJson();
  });

  result = httpSvr->registerRoute("post", "echobody", "/echobody");
  QVERIFY(result == true);

  // Uses the action interface.
  QVERIFY(httpSvr->addAction<SampleActionWithHttpMethods>().get() != nullptr);

  result = httpSvr->registerRoute("get", "sampleWithHttpMethods", "/http");
  QVERIFY(result == true);

  result = httpSvr->registerRoute("post", "sampleWithHttpMethods", "/http");
  QVERIFY(result == true);

  result = httpSvr->registerRoute("put", "sampleWithHttpMethods", "/http");
  QVERIFY(result == true);

  result = httpSvr->registerRoute("delete", "sampleWithHttpMethods", "/http");
  QVERIFY(result == true);

  // Uses the action interface.
  QString param = "param";
  QVERIFY((httpSvr->addAction<ActionWithParameter, QString>(param)).get() != nullptr);

  result = httpSvr->registerRoute("get", "sampleWithParameter", "/sampleWithParameter");
  QVERIFY(result == true);

  // Uses the action interface.
  QVERIFY((httpSvr->addAction<SampleAction>()).get() != nullptr);

  result = httpSvr->registerRoute("get", "sample", "/sample");
  QVERIFY(result == true);

  result = httpSvr->registerRoute("get", "sample", "/sample2");
  QVERIFY(result == true);

  // Uses a raw std::function based callback.
  action = httpSvr->createAction("test", [](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    json["response"] = "Test C++ FTW";

    // NOTE: This terminates early so we should not expect any post-processing.
    data.getResponse().finish();
  });
  QVERIFY((action.get() != nullptr));

  result = httpSvr->registerRoute("get", "test", "/test");
  QVERIFY(result == true);

  result = httpSvr->registerRoute("get", "test", "/test2");
  QVERIFY(result == true);

  action = httpSvr->createAction("terminates", [](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    json["response"] = "Test C++ FTW";
    // NOTE: This terminates early so we should not expect any post-processing.
    data.getResponse().terminate();
  });
  QVERIFY((action.get() != nullptr));

  result = httpSvr->registerRoute("get", "terminates", "/terminates");
  QVERIFY(result == true);

  action = httpSvr->createAction("regex", [](HttpData& data) {
    QJsonObject& json = data.getResponse().getJson();
    QString name = data.getRequest().getJson()["name"].toString();
    json["response"] = name;
  });

  result = httpSvr->registerRoute(qttp::GET, "regex", "/regex/:name([A-Za-z]+)");
  QVERIFY(result == true);

  httpSvr->startServer("127.0.0.1", 8080);
  QTest::qWait(1000);
}