|
87 | 87 |
|
88 | 88 | end |
89 | 89 |
|
| 90 | +local _ENV = TEST_CASE'form' if ENABLE then |
| 91 | + |
| 92 | +local post |
| 93 | + |
| 94 | +function teardown() |
| 95 | + if post then post:free() end |
| 96 | + post = nil |
| 97 | +end |
| 98 | + |
| 99 | +function test_content_01() |
| 100 | + post = assert(scurl.form{name01 = 'value01'}) |
| 101 | + local data = assert_string(post:get()) |
| 102 | + assert_match("\r\n\r\nvalue01\r\n", data) |
| 103 | + assert_match('name="name01"', data) |
| 104 | +end |
| 105 | + |
| 106 | +function test_content_02() |
| 107 | + post = assert(scurl.form{name02 = {'value02', type = "text/plain"}}) |
| 108 | + local data = assert_string(post:get()) |
| 109 | + assert_match("\r\n\r\nvalue02\r\n", data) |
| 110 | + assert_match('name="name02"', data) |
| 111 | + assert_match('Content%-Type: text/plain\r\n', data) |
| 112 | +end |
| 113 | + |
| 114 | +function test_content_03() |
| 115 | + post = assert(scurl.form{name03 = {content = 'value03', headers = {"Content-Encoding: gzip"}}}) |
| 116 | + local data = assert_string(post:get()) |
| 117 | + assert_match("\r\n\r\nvalue03\r\n", data) |
| 118 | + assert_match('name="name03"', data) |
| 119 | + assert_match('Content%-Encoding: gzip\r\n', data) |
| 120 | +end |
| 121 | + |
| 122 | +function test_content_04() |
| 123 | + post = assert(scurl.form{name04 = {'value04', type = "text/plain", headers = {"Content-Encoding: gzip"}}}) |
| 124 | + local data = assert_string(post:get()) |
| 125 | + assert_match("\r\n\r\nvalue04\r\n", data) |
| 126 | + assert_match('name="name04"', data) |
| 127 | + assert_match('Content%-Encoding: gzip\r\n', data) |
| 128 | + assert_match('Content%-Type: text/plain\r\n', data) |
| 129 | +end |
| 130 | + |
| 131 | +function test_buffer_01() |
| 132 | + post = assert(scurl.form{name01 = { |
| 133 | + name = 'file01', |
| 134 | + data = 'value01', |
| 135 | + }}) |
| 136 | + |
| 137 | + local data = assert_string(post:get()) |
| 138 | + assert_match("\r\n\r\nvalue01\r\n", data) |
| 139 | + assert_match('name="name01"', data) |
| 140 | + assert_match('filename="file01"', data) |
| 141 | + assert_match('Content%-Type: application/octet%-stream\r\n', data) |
| 142 | +end |
| 143 | + |
| 144 | +function test_buffer_02() |
| 145 | + post = assert(scurl.form{name02 = { |
| 146 | + name = 'file02', |
| 147 | + data = 'value02', |
| 148 | + type = "text/plain", |
| 149 | + }}) |
| 150 | + |
| 151 | + local data = assert_string(post:get()) |
| 152 | + assert_match("\r\n\r\nvalue02\r\n", data) |
| 153 | + assert_match('name="name02"', data) |
| 154 | + assert_match('filename="file02"', data) |
| 155 | + assert_match('Content%-Type: text/plain\r\n', data) |
| 156 | + assert_not_match('Content%-Type: application/octet%-stream\r\n', data) |
| 157 | +end |
| 158 | + |
| 159 | +function test_buffer_03() |
| 160 | + post = assert(scurl.form{name03 = { |
| 161 | + name = 'file03', |
| 162 | + data = 'value03', |
| 163 | + headers = {"Content-Encoding: gzip"}, |
| 164 | + }}) |
| 165 | + local data = assert_string(post:get()) |
| 166 | + assert_match("\r\n\r\nvalue03\r\n", data) |
| 167 | + assert_match('name="name03"', data) |
| 168 | + assert_match('filename="file03"', data) |
| 169 | + assert_match('Content%-Type: application/octet%-stream\r\n', data) |
| 170 | + assert_match('Content%-Encoding: gzip\r\n', data) |
| 171 | +end |
| 172 | + |
| 173 | +function test_buffer_04() |
| 174 | + post = assert(scurl.form{name04 = { |
| 175 | + name = 'file04', |
| 176 | + data = 'value04', |
| 177 | + type = "text/plain", |
| 178 | + headers = {"Content-Encoding: gzip"}, |
| 179 | + }}) |
| 180 | + |
| 181 | + local data = assert_string(post:get()) |
| 182 | + assert_match("\r\n\r\nvalue04\r\n", data) |
| 183 | + assert_match('name="name04"', data) |
| 184 | + assert_match('filename="file04"', data) |
| 185 | + assert_match('Content%-Type: text/plain\r\n', data) |
| 186 | + assert_not_match('Content%-Type: application/octet%-stream\r\n', data) |
| 187 | + assert_match('Content%-Encoding: gzip\r\n', data) |
| 188 | +end |
| 189 | + |
| 190 | +function test_stream_01() |
| 191 | + post = assert(scurl.form{name01 = { |
| 192 | + stream = function() end, |
| 193 | + length = 128, |
| 194 | + }}) |
| 195 | + local data = assert_string(post:get()) |
| 196 | + assert_match('name="name01"', data) |
| 197 | + assert_not_match('filename', data) |
| 198 | +end |
| 199 | + |
| 200 | +function test_stream_02() |
| 201 | + post = assert(scurl.form{name02 = { |
| 202 | + name = 'file02', |
| 203 | + stream = function() end, |
| 204 | + length = 128, |
| 205 | + }}) |
| 206 | + local data = assert_string(post:get()) |
| 207 | + assert_match('name="name02"', data) |
| 208 | + assert_match('filename="file02"', data) |
| 209 | +end |
| 210 | + |
| 211 | +function test_stream_03() |
| 212 | + post = assert(scurl.form{name03 = { |
| 213 | + name = 'file03', |
| 214 | + stream = function() end, |
| 215 | + length = 128, |
| 216 | + type = 'text/plain', |
| 217 | + }}) |
| 218 | + |
| 219 | + local data = assert_string(post:get()) |
| 220 | + assert_match('name="name03"', data) |
| 221 | + assert_match('filename="file03"', data) |
| 222 | + assert_match('Content%-Type: text/plain\r\n', data) |
| 223 | +end |
| 224 | + |
| 225 | +function test_stream_04() |
| 226 | + post = assert(scurl.form{name04 = { |
| 227 | + name = 'file04', |
| 228 | + stream = function() end, |
| 229 | + length = 128, |
| 230 | + type = 'text/plain', |
| 231 | + headers = {"Content-Encoding: gzip"}, |
| 232 | + }}) |
| 233 | + local data = assert_string(post:get()) |
| 234 | + assert_match('name="name04"', data) |
| 235 | + assert_match('filename="file04"', data) |
| 236 | + assert_match('Content%-Type: text/plain\r\n', data) |
| 237 | + assert_match('Content%-Encoding: gzip\r\n', data) |
| 238 | +end |
| 239 | + |
| 240 | +function test_stream_05() |
| 241 | + post = assert(scurl.form{name05 = { |
| 242 | + stream = { |
| 243 | + length = function() return 128 end; |
| 244 | + read = function() end; |
| 245 | + } |
| 246 | + }}) |
| 247 | + local data = assert_string(post:get()) |
| 248 | + assert_match('name="name05"', data) |
| 249 | + assert_not_match('filename', data) |
| 250 | +end |
| 251 | + |
| 252 | +function test_error() |
| 253 | + assert_error(function() post = scurl.form{name = {content = 1}} end) |
| 254 | + assert_error(function() post = scurl.form{name = {1}} end) |
| 255 | + assert_error(function() post = scurl.form{name = {data = {}}} end) |
| 256 | + assert_error(function() post = scurl.form{name = {file = true}} end) |
| 257 | + assert_error(function() post = scurl.form{name = {stream = function() end}} end) |
| 258 | + assert_error(function() post = scurl.form{name = {stream = {}}} end) |
| 259 | + assert_error(function() post = scurl.form{name = {stream = { |
| 260 | + read=function()end;length=function()end |
| 261 | + }}}end) |
| 262 | + assert_error(function() post = scurl.form{name = {stream = { |
| 263 | + read=function()end;length=function() return "123" end |
| 264 | + }}}end) |
| 265 | + assert_error(function() post = scurl.form{name = {stream = { |
| 266 | + read=function()end;length=function() return "hello" end |
| 267 | + }}}end) |
| 268 | +end |
| 269 | + |
| 270 | +function test_ignore_unknown() |
| 271 | + post = assert(scurl.form{ |
| 272 | + name01 = {}, |
| 273 | + name02 = {name = "helo"}, |
| 274 | + }) |
| 275 | + local data = assert_string(post:get()) |
| 276 | + assert_not_match('name="name01"', data) |
| 277 | + assert_not_match('name="name02"', data) |
| 278 | +end |
| 279 | + |
| 280 | +function test_empty() |
| 281 | + post = assert(scurl.form{}) |
| 282 | +end |
| 283 | + |
| 284 | +end |
90 | 285 |
|
91 | 286 | if not HAS_RUNNER then lunit.run() end |
0 commit comments