示例#1
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);
}
示例#2
0
int main(int argc, char **argv)
{    
    int port = 0;
    int nCPU = 0;
    char* rootDir = 0;
    
    int c = 0;
    while ((c = getopt (argc, argv, "p:n:r:")) != -1) {
        switch(c) {
        case 'p':
            port = atoi(optarg);
            if(port < 1 || port > 65535) {
                std::cout << "Wrong port: " << port << std::endl;
                return 0;
            }
            break;
        case 'n':
            nCPU = atoi(optarg);
            if(nCPU < 1) {
                std::cout << "Wrong nCPU: " << nCPU << std::endl;
                return 0;
            }
            break;
        case 'r':
            rootDir = optarg;
            if(rootDir[strlen(rootDir) - 1] == '/') {
                rootDir[strlen(rootDir) - 1] = '\0';
            }
            break;
        }
    }
    
    HttpServer* httpServer = 0;
    try {
        HttpServer* httpServer = new HttpServer();
        httpServer->startServer(port, nCPU, rootDir);
    }
    catch(std::exception& err) {
        std::cout << err.what() << std::endl;
    }
    
    delete httpServer;
    return 0;
}
示例#3
0
int main(int argc, char **argv)
{
	if(argc == 1)
	{
		// sample code to create http server 
		HttpServer httpServer;
		int res = httpServer.startServer();
		if (res == -1)
		{
			return res;
		}
		
	}else if(argc == 3){
		// sample code to create http client
		string ip = argv[1];
		int port = stoi(string(argv[2]));
		HttpClient httpClient(ip, port);
		string response = httpClient.requestToHttpServer();
		cout << "Received string "<< response << endl; 
	}
	return 0;
}
示例#4
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();
}
示例#5
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);
}