mirror of
https://github.com/lloeki/nanoserve.git
synced 2025-12-06 11:14:40 +01:00
Compare commits
5 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2edc861a06 | |||
| 8a5c956a24 | |||
| 66a2a20663 | |||
| dd5001cb7c | |||
| dc8739c692 |
3 changed files with 86 additions and 4 deletions
|
|
@ -98,8 +98,44 @@ module NanoServe
|
||||||
@body = +''.encode('ASCII-8BIT')
|
@body = +''.encode('ASCII-8BIT')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def host
|
||||||
|
@headers['host']
|
||||||
|
end
|
||||||
|
|
||||||
|
def path
|
||||||
|
@uri.path
|
||||||
|
end
|
||||||
|
|
||||||
|
def query_array
|
||||||
|
URI.decode_www_form(@uri.query || '')
|
||||||
|
end
|
||||||
|
|
||||||
|
def form_array
|
||||||
|
form? ? URI.decode_www_form(body) : []
|
||||||
|
end
|
||||||
|
|
||||||
|
def query
|
||||||
|
Hash[*query_array.flatten]
|
||||||
|
end
|
||||||
|
|
||||||
|
def form
|
||||||
|
Hash[*form_array.flatten]
|
||||||
|
end
|
||||||
|
|
||||||
def params
|
def params
|
||||||
Hash[*@uri.query.split('&').map { |kv| kv.split('=') }.flatten]
|
query.merge(form)
|
||||||
|
end
|
||||||
|
|
||||||
|
def form?
|
||||||
|
content_type == 'application/x-www-form-urlencoded'
|
||||||
|
end
|
||||||
|
|
||||||
|
def body
|
||||||
|
@body
|
||||||
|
end
|
||||||
|
|
||||||
|
def [](key)
|
||||||
|
@headers[key.downcase]
|
||||||
end
|
end
|
||||||
|
|
||||||
def <<(line)
|
def <<(line)
|
||||||
|
|
@ -124,6 +160,10 @@ module NanoServe
|
||||||
@headers.key?('content-length')
|
@headers.key?('content-length')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def content_type
|
||||||
|
@headers['content-type']
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
REQ_RE = %r{(?<method>[A-Z]+)\s+(?<path>\S+)\s+(?<version>HTTP/\d+.\d+)$}
|
REQ_RE = %r{(?<method>[A-Z]+)\s+(?<path>\S+)\s+(?<version>HTTP/\d+.\d+)$}
|
||||||
|
|
@ -139,7 +179,7 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_method(str)
|
def parse_method(str)
|
||||||
str
|
str.upcase
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_path(str)
|
def parse_path(str)
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = 'nanoserve'
|
s.name = 'nanoserve'
|
||||||
s.version = '0.2.0'
|
s.version = '0.3.0'
|
||||||
s.licenses = ['3BSD']
|
s.licenses = ['3BSD']
|
||||||
s.summary = 'Listen to one-shot connections'
|
s.summary = 'Listen to one-shot connections'
|
||||||
s.authors = ['Loic Nageleisen']
|
s.authors = ['Loic Nageleisen']
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class TestNanoServe < MiniTest::Test
|
||||||
assert_equal(uuid, buf)
|
assert_equal(uuid, buf)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_http_responder
|
def test_http_responder_get
|
||||||
uuid = SecureRandom.uuid.encode('UTF-8')
|
uuid = SecureRandom.uuid.encode('UTF-8')
|
||||||
uri = URI('http://localhost:2000')
|
uri = URI('http://localhost:2000')
|
||||||
|
|
||||||
|
|
@ -56,4 +56,46 @@ class TestNanoServe < MiniTest::Test
|
||||||
|
|
||||||
assert_equal(uuid, req.first.params['uuid'])
|
assert_equal(uuid, req.first.params['uuid'])
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue