Skip to content

Commit 5239176

Browse files
committed
Test. Use pegasus server to dump mime content.
1 parent 8916f54 commit 5239176

4 files changed

Lines changed: 134 additions & 8 deletions

File tree

appveyor.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ install:
3939
hererocks c:\hererocks --%LUA% --target %HR_TARGET% -rlatest
4040
)
4141
- call c:\hererocks\bin\activate
42+
- luarocks show luarocks-fetch-gitrec >nul 2>&1 || luarocks install luarocks-fetch-gitrec
4243

4344
before_build:
4445
# external deps
@@ -57,6 +58,10 @@ before_test:
5758
- luarocks show dkjson >nul 2>&1 || luarocks install dkjson
5859
- luarocks show luafilesystem >nul 2>&1 || luarocks install luafilesystem
5960
- luarocks show lua-path >nul 2>&1 || luarocks install lua-path
61+
- luarocks show pegasus >nul 2>&1 || luarocks install pegasus http.parser \
62+
--server=http://luarocks.org/manifests/moteus
63+
- ps: $TestServer = Start-Process lua -ArgumentList test/server.lua -PassThru
64+
- curl -s http://127.0.0.1:7090
6065

6166
test_script:
6267
- echo "Testing..."
@@ -69,3 +74,6 @@ test_script:
6974
after_test:
7075
- cd %APPVEYOR_BUILD_FOLDER%
7176
- .appveyor\pack_artifact.bat lua-curl bin-rock
77+
78+
on_finish:
79+
- ps: Stop-Process -Id $TestServer.Id

test/server.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local function prequire(m)
2+
local ok, err = pcall(require, m)
3+
if not ok then return nil, err end
4+
return err
5+
end
6+
7+
local uv = prequire "lluv"
8+
local Pegasus = require (uv and "lluv.pegasus" or "pegasus")
9+
10+
local server = Pegasus:new{host = '127.0.0.1', port = 7090}
11+
12+
server:start(function(request, response)
13+
response:statusCode(200)
14+
response:addHeader('Content-Type', 'text/plain')
15+
response:write('Hello from Pegasus')
16+
end)
17+
18+
if uv then uv.run() end

test/test_mime.lua

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@ local TEST_CASE = assert(lunit.TEST_CASE)
1313
local skip = lunit.skip or function() end
1414

1515
local curl = require "lcurl"
16+
local utils = require "utils"
1617

17-
local function weak_ptr(val)
18-
return setmetatable({value = val}, {__mode = 'v'})
19-
end
18+
local weak_ptr, gc_collect, dump_mime_ = utils.import('weak_ptr', 'gc_collect', 'dump_mime')
2019

21-
local function gc_collect(n)
22-
for i = 1, (n or 10) do
23-
collectgarbage("collect")
24-
end
25-
end
20+
local dump_mime_url = 'http://127.0.0.1:7090'
2621

2722
local function is_freed(c)
2823
return not not string.find(tostring(c), '%(freed%)')
@@ -185,4 +180,81 @@ end
185180

186181
end
187182

183+
local _ENV = TEST_CASE'mime basic' if not curl.OPT_MIMEPOST then
184+
function test() skip("MIMI API supports since cURL 7.56.0") end
185+
else
186+
187+
local easy, mime
188+
189+
local function dump_mime(mime)
190+
return dump_mime_(easy, mime, dump_mime_url)
191+
end
192+
193+
function setup()
194+
easy = curl.easy()
195+
mime = easy:mime()
196+
end
197+
198+
function teardown()
199+
if easy then easy:close() end
200+
if mime then mime:free() end
201+
easy, mime = nil
202+
end
203+
204+
function test_data()
205+
mime:addpart():data('hello')
206+
local info = assert_string(dump_mime(mime))
207+
assert_match('\r\n\r\nhello', info)
208+
end
209+
210+
function test_data_type()
211+
mime:addpart():data('hello', 'text/html')
212+
local info = assert_string(dump_mime(mime))
213+
assert_match('\r\n\r\nhello', info)
214+
assert_match('Content%-Type:%s+text/html', info)
215+
end
216+
217+
function test_data_type_name()
218+
mime:addpart():data('hello', 'text/html', 'test')
219+
local info = assert_string(dump_mime(mime))
220+
assert_match('\r\n\r\nhello', info)
221+
assert_match('Content%-Type:%s+text/html', info)
222+
assert_match('Content%-Disposition:.-name="test"', info)
223+
end
224+
225+
function test_data_name()
226+
mime:addpart():data('hello', nil, 'test')
227+
local info = assert_string(dump_mime(mime))
228+
assert_match('\r\n\r\nhello', info)
229+
assert_match('Content%-Disposition:.-name="test"', info)
230+
end
231+
232+
function test_data_headers()
233+
mime:addpart():data('hello', {
234+
'X-Custom-Header: hello'
235+
})
236+
local info = assert_string(dump_mime(mime))
237+
assert_match('\r\n\r\nhello', info)
238+
assert_match('X%-Custom%-Header:%s*hello', info)
239+
end
240+
241+
function test_unset_name()
242+
mime:addpart():data('hello', 'text/html', 'test'):name(false)
243+
244+
local info = assert_string(dump_mime(mime))
245+
assert_match('\r\n\r\nhello', info)
246+
assert_match('Content%-Type:%s+text/html', info)
247+
assert_not_match('Content%-Disposition:.-name="test"', info)
248+
end
249+
250+
function test_unset_type()
251+
mime:addpart():data('hello', 'text/html'):type(false)
252+
253+
local info = assert_string(dump_mime(mime))
254+
assert_match('\r\n\r\nhello', info)
255+
assert_not_match('Content%-Type:%s+text/html', info)
256+
end
257+
258+
end
259+
188260
RUN()

test/utils.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,41 @@ local function Stream(ch, n, m)
6666
return _stream:reset()
6767
end
6868

69+
local function easy_dump_mime(easy, mime, url)
70+
local buffer = {}
71+
72+
local function dump_mime(type, data)
73+
if type == curl.INFO_DATA_OUT then
74+
buffer[#buffer + 1] = data
75+
end
76+
end
77+
78+
local ok, err = easy:setopt{
79+
url = url or "http://127.0.0.1:7090";
80+
customrequest = "GET";
81+
mimepost = mime;
82+
verbose = true;
83+
debugfunction = dump_mime;
84+
writefunction = function()end;
85+
}
86+
87+
if not ok then return nil, err end
88+
89+
ok, err = easy:perform()
90+
91+
if not ok then return nil, err end
92+
93+
return table.concat(buffer)
94+
end
95+
6996
local utils = {
7097
weak_ptr = weak_ptr;
7198
gc_collect = gc_collect;
7299
is_curl_ge = is_curl_ge;
73100
is_curl_eq = is_curl_eq;
74101
get_bin_by = get_bin_by;
75102
read_file = read_file;
103+
dump_mime = easy_dump_mime;
76104
stream = stream;
77105
Stream = Stream;
78106
}

0 commit comments

Comments
 (0)