From 5239176fc01aeac35e7e676e29d491c22e2e3038 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 18 Oct 2017 14:11:04 +0300 Subject: [PATCH] Test. Use pegasus server to dump mime content. --- appveyor.yml | 8 +++++ test/server.lua | 18 ++++++++++ test/test_mime.lua | 88 +++++++++++++++++++++++++++++++++++++++++----- test/utils.lua | 28 +++++++++++++++ 4 files changed, 134 insertions(+), 8 deletions(-) create mode 100644 test/server.lua diff --git a/appveyor.yml b/appveyor.yml index 08190a5..e4e867b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -39,6 +39,7 @@ install: hererocks c:\hererocks --%LUA% --target %HR_TARGET% -rlatest ) - call c:\hererocks\bin\activate + - luarocks show luarocks-fetch-gitrec >nul 2>&1 || luarocks install luarocks-fetch-gitrec before_build: # external deps @@ -57,6 +58,10 @@ before_test: - luarocks show dkjson >nul 2>&1 || luarocks install dkjson - luarocks show luafilesystem >nul 2>&1 || luarocks install luafilesystem - luarocks show lua-path >nul 2>&1 || luarocks install lua-path + - luarocks show pegasus >nul 2>&1 || luarocks install pegasus http.parser \ + --server=http://luarocks.org/manifests/moteus + - ps: $TestServer = Start-Process lua -ArgumentList test/server.lua -PassThru + - curl -s http://127.0.0.1:7090 test_script: - echo "Testing..." @@ -69,3 +74,6 @@ test_script: after_test: - cd %APPVEYOR_BUILD_FOLDER% - .appveyor\pack_artifact.bat lua-curl bin-rock + +on_finish: + - ps: Stop-Process -Id $TestServer.Id \ No newline at end of file diff --git a/test/server.lua b/test/server.lua new file mode 100644 index 0000000..e29bd6c --- /dev/null +++ b/test/server.lua @@ -0,0 +1,18 @@ +local function prequire(m) + local ok, err = pcall(require, m) + if not ok then return nil, err end + return err +end + +local uv = prequire "lluv" +local Pegasus = require (uv and "lluv.pegasus" or "pegasus") + +local server = Pegasus:new{host = '127.0.0.1', port = 7090} + +server:start(function(request, response) + response:statusCode(200) + response:addHeader('Content-Type', 'text/plain') + response:write('Hello from Pegasus') +end) + +if uv then uv.run() end diff --git a/test/test_mime.lua b/test/test_mime.lua index 602297c..1a535a4 100644 --- a/test/test_mime.lua +++ b/test/test_mime.lua @@ -13,16 +13,11 @@ local TEST_CASE = assert(lunit.TEST_CASE) local skip = lunit.skip or function() end local curl = require "lcurl" +local utils = require "utils" -local function weak_ptr(val) - return setmetatable({value = val}, {__mode = 'v'}) -end +local weak_ptr, gc_collect, dump_mime_ = utils.import('weak_ptr', 'gc_collect', 'dump_mime') -local function gc_collect(n) - for i = 1, (n or 10) do - collectgarbage("collect") - end -end +local dump_mime_url = 'http://127.0.0.1:7090' local function is_freed(c) return not not string.find(tostring(c), '%(freed%)') @@ -185,4 +180,81 @@ end end +local _ENV = TEST_CASE'mime basic' if not curl.OPT_MIMEPOST then +function test() skip("MIMI API supports since cURL 7.56.0") end +else + +local easy, mime + +local function dump_mime(mime) + return dump_mime_(easy, mime, dump_mime_url) +end + +function setup() + easy = curl.easy() + mime = easy:mime() +end + +function teardown() + if easy then easy:close() end + if mime then mime:free() end + easy, mime = nil +end + +function test_data() + mime:addpart():data('hello') + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) +end + +function test_data_type() + mime:addpart():data('hello', 'text/html') + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('Content%-Type:%s+text/html', info) +end + +function test_data_type_name() + mime:addpart():data('hello', 'text/html', 'test') + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('Content%-Type:%s+text/html', info) + assert_match('Content%-Disposition:.-name="test"', info) +end + +function test_data_name() + mime:addpart():data('hello', nil, 'test') + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('Content%-Disposition:.-name="test"', info) +end + +function test_data_headers() + mime:addpart():data('hello', { + 'X-Custom-Header: hello' + }) + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('X%-Custom%-Header:%s*hello', info) +end + +function test_unset_name() + mime:addpart():data('hello', 'text/html', 'test'):name(false) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_match('Content%-Type:%s+text/html', info) + assert_not_match('Content%-Disposition:.-name="test"', info) +end + +function test_unset_type() + mime:addpart():data('hello', 'text/html'):type(false) + + local info = assert_string(dump_mime(mime)) + assert_match('\r\n\r\nhello', info) + assert_not_match('Content%-Type:%s+text/html', info) +end + +end + RUN() diff --git a/test/utils.lua b/test/utils.lua index 798a105..6dce917 100644 --- a/test/utils.lua +++ b/test/utils.lua @@ -66,6 +66,33 @@ local function Stream(ch, n, m) return _stream:reset() end +local function easy_dump_mime(easy, mime, url) + local buffer = {} + + local function dump_mime(type, data) + if type == curl.INFO_DATA_OUT then + buffer[#buffer + 1] = data + end + end + + local ok, err = easy:setopt{ + url = url or "http://127.0.0.1:7090"; + customrequest = "GET"; + mimepost = mime; + verbose = true; + debugfunction = dump_mime; + writefunction = function()end; + } + + if not ok then return nil, err end + + ok, err = easy:perform() + + if not ok then return nil, err end + + return table.concat(buffer) +end + local utils = { weak_ptr = weak_ptr; gc_collect = gc_collect; @@ -73,6 +100,7 @@ local utils = { is_curl_eq = is_curl_eq; get_bin_by = get_bin_by; read_file = read_file; + dump_mime = easy_dump_mime; stream = stream; Stream = Stream; }