mirror of
https://github.com/lloeki/nanoserve.git
synced 2025-12-06 11:14:40 +01:00
Implement form-urlencoded support
Query and form return a Hash, folding duplicates with last-key-wins strategy. *_array methods provide the seldom-used duplicate-preserving counterparts. Keep query and form separate, but provide params, which handles both, with form having precedence over query.
This commit is contained in:
parent
66a2a20663
commit
8a5c956a24
2 changed files with 64 additions and 2 deletions
|
|
@ -29,7 +29,7 @@ class TestNanoServe < MiniTest::Test
|
|||
assert_equal(uuid, buf)
|
||||
end
|
||||
|
||||
def test_http_responder
|
||||
def test_http_responder_get
|
||||
uuid = SecureRandom.uuid.encode('UTF-8')
|
||||
uri = URI('http://localhost:2000')
|
||||
|
||||
|
|
@ -56,4 +56,46 @@ class TestNanoServe < MiniTest::Test
|
|||
|
||||
assert_equal(uuid, req.first.params['uuid'])
|
||||
end
|
||||
|
||||
def test_http_responder_post
|
||||
uuid = SecureRandom.uuid.encode('UTF-8')
|
||||
uri = URI('http://localhost:2000')
|
||||
|
||||
r = NanoServe::HTTPResponder.new(uri.host, uri.port) do |res, req, y|
|
||||
y << req
|
||||
|
||||
res.body = <<-EOS.gsub(/^ {8}/, '')
|
||||
<html>
|
||||
<head>
|
||||
<title>An Example Page</title>
|
||||
</head>
|
||||
<body>
|
||||
Hello World, this is a very simple HTML document.
|
||||
</body>
|
||||
</html>
|
||||
EOS
|
||||
end
|
||||
|
||||
req = r.start([]) do
|
||||
Net::HTTP.post_form(
|
||||
uri + "test?uuid=#{uuid}&p=query",
|
||||
'p' => 'form',
|
||||
'f' => 'foo',
|
||||
)
|
||||
end
|
||||
|
||||
r.stop
|
||||
|
||||
assert_equal(uuid, req.first.params['uuid'])
|
||||
assert_equal(uuid, req.first.query['uuid'])
|
||||
assert_nil(req.first.form['uuid'])
|
||||
|
||||
assert_equal('foo', req.first.params['f'])
|
||||
assert_nil(req.first.query['f'])
|
||||
assert_equal('foo', req.first.form['f'])
|
||||
|
||||
assert_equal('form', req.first.params['p'])
|
||||
assert_equal('query', req.first.query['p'])
|
||||
assert_equal('form', req.first.form['p'])
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue