Test. Use pegasus server to dump mime content.

This commit is contained in:
Alexey Melnichuk 2017-10-18 14:11:04 +03:00
parent 8916f541d9
commit 5239176fc0
4 changed files with 134 additions and 8 deletions

View File

@ -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

18
test/server.lua Normal file
View File

@ -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

View File

@ -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()

View File

@ -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;
}