浏览代码

first server test

Nick Hildebrant 10 年之前
父节点
当前提交
f9fac60256

+ 30 - 4
test/ci_tests/01_basic/basic_spec.lua

@@ -1,7 +1,33 @@
-require("cURL")
+civet = require "test/ci_tests/civet"
+curl = require "cURL"
 
 describe("civetweb basic", function()
- it("should serve a simple get request", function()
-   assert.is_true(true)
- end)
+
+  setup(function()
+    civet.start()
+  end)
+
+  teardown(function()
+    civet.stop()
+  end)
+
+
+  it("should serve a simple get request", function()
+
+    local out = ""
+    function capture(str)
+      out = out .. str
+    end
+
+    curl.easy()
+      :setopt_url('http://localhost:' .. civet.port .. "/")
+      :setopt_writefunction(capture)
+      :perform()
+    :close()
+
+    assert.are.equal('Index of', string.match(out, 'Index of'))
+    assert.are.equal('01_basic_test_dir', string.match(out, '01_basic_test_dir'))
+    assert.are.equal('01_basic_test_file', string.match(out, '01_basic_test_file'))
+  end)
+
 end)

+ 0 - 0
test/ci_tests/01_basic/docroot/01_basic_test_dir/git_keep_empty_dir


+ 0 - 0
test/ci_tests/01_basic/docroot/01_basic_test_file


+ 36 - 0
test/ci_tests/civet.lua

@@ -0,0 +1,36 @@
+
+local civet = {}
+
+civet.port=12345
+
+function civet.start(docroot)
+  -- TODO: use a property
+  docroot = docroot or 'test/ci_tests/01_basic/docroot'
+  assert(io.popen('./civetweb'
+  .. " -listening_ports " .. civet.port
+  .. " -document_root " .. docroot
+  .. " > /dev/null 2>&1 &"
+  ))
+  -- wait until the server is listening (TODO: Linux only)
+  while true do
+    local f = assert(io.popen('netstat -an | grep '
+      .. civet.port .. ' | grep -i listen'))
+    local out = f:read('*all')
+    if string.match(out, civet.port) then break end
+    f:close()
+  end
+end
+
+function civet.stop()
+  os.execute('killall civetweb')
+  -- wait until the server is listening (TODO: Linux only)
+  while true do
+    local f = assert(io.popen('netstat -an | grep '
+      .. civet.port .. ' | grep -i listen'))
+    local out = f:read('*all')
+    if not string.match(out, civet.port) then break end
+    f:close()
+  end
+end
+
+return civet