Browse Source

#712 Add Conan test package

Signed-off-by: Uilian Ries <uilianries@gmail.com>
Uilian Ries 6 years ago
parent
commit
e56d02a9a3

+ 2 - 0
.gitignore

@@ -267,3 +267,5 @@ requests.db
 ci/lua
 
 
+# Conan test cache
+conan/test_package/build

+ 9 - 0
conan/test_package/CMakeLists.txt

@@ -0,0 +1,9 @@
+cmake_minimum_required(VERSION 2.8.11)
+project(test_package CXX)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+add_executable(${PROJECT_NAME} test_package.cpp)
+target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
+set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)

+ 27 - 0
conan/test_package/conanfile.py

@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import os
+import subprocess
+import requests
+import time
+from conans import ConanFile, CMake, tools, RunEnvironment
+
+
+class TestPackageConan(ConanFile):
+    settings = "os", "compiler", "build_type", "arch"
+    generators = "cmake"
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def test(self):
+        assert os.path.isfile(os.path.join(self.deps_cpp_info["civetweb"].rootpath, "licenses", "LICENSE.md"))
+        bin_path = os.path.join(os.getcwd(), "bin", "test_package")
+        with tools.environment_append(RunEnvironment(self).vars):
+            process = subprocess.Popen([bin_path], shell=True)
+            time.sleep(2)
+            response = requests.get("http://localhost:8080/example")
+            assert response.ok
+            process.kill()

+ 93 - 0
conan/test_package/test_package.cpp

@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2018 the CivetWeb developers
+ * MIT License
+ */
+
+/* Simple demo of a REST callback. */
+#ifdef _WIN32
+#include <windows.h>
+#else
+#include <unistd.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "civetweb.h"
+
+#define PORT "8080"
+#define HOST_INFO "http://localhost:8080"
+#define EXAMPLE_URI "/example"
+
+int exitNow = 0;
+
+static int
+ExampleGET(struct mg_connection* conn)
+{
+    mg_send_http_ok(conn, "text/plain", 10);
+    exitNow = 1;
+	return 200;
+}
+
+static int
+ExampleHandler(struct mg_connection *conn, void *cbdata)
+{
+	const struct mg_request_info *ri = mg_get_request_info(conn);
+	(void)cbdata; /* currently unused */
+
+	if (0 == strcmp(ri->request_method, "GET")) {
+		return ExampleGET(conn);
+	}
+	return 405;
+}
+
+int
+log_message(const struct mg_connection *conn, const char *message)
+{
+	puts(message);
+	return 1;
+}
+
+int
+main(int argc, char *argv[])
+{
+	struct mg_callbacks callbacks;
+	struct mg_context *ctx;
+	int err = 0;
+
+	mg_init_library(0);
+
+	/* Callback will print error messages to console */
+	memset(&callbacks, 0, sizeof(callbacks));
+	callbacks.log_message = log_message;
+
+	/* Start CivetWeb web server */
+	ctx = mg_start(&callbacks, 0, NULL);
+
+	/* Check return value: */
+	if (ctx == NULL) {
+		fprintf(stderr, "Cannot start CivetWeb - mg_start failed.\n");
+		return EXIT_FAILURE;
+	}
+
+	/* Add handler EXAMPLE_URI, to explain the example */
+	mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0);
+
+	/* Show sone info */
+	printf("Start example: %s%s\n", HOST_INFO, EXAMPLE_URI);
+
+
+	/* Wait until the server should be closed */
+	while (!exitNow) {
+#ifdef _WIN32
+		Sleep(1000);
+#else
+		sleep(1);
+#endif
+	}
+
+	/* Stop the server */
+	mg_stop(ctx);
+	return EXIT_SUCCESS;
+}