Compare commits

..

No commits in common. "master" and "v0.2.0" have entirely different histories.

3 changed files with 4 additions and 86 deletions

View file

@ -98,44 +98,8 @@ module NanoServe
@body = +''.encode('ASCII-8BIT')
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
query.merge(form)
end
def form?
content_type == 'application/x-www-form-urlencoded'
end
def body
@body
end
def [](key)
@headers[key.downcase]
Hash[*@uri.query.split('&').map { |kv| kv.split('=') }.flatten]
end
def <<(line)
@ -160,10 +124,6 @@ module NanoServe
@headers.key?('content-length')
end
def content_type
@headers['content-type']
end
private
REQ_RE = %r{(?<method>[A-Z]+)\s+(?<path>\S+)\s+(?<version>HTTP/\d+.\d+)$}
@ -179,7 +139,7 @@ module NanoServe
end
def parse_method(str)
str.upcase
str
end
def parse_path(str)

View file

@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = 'nanoserve'
s.version = '0.3.0'
s.version = '0.2.0'
s.licenses = ['3BSD']
s.summary = 'Listen to one-shot connections'
s.authors = ['Loic Nageleisen']

View file

@ -29,7 +29,7 @@ class TestNanoServe < MiniTest::Test
assert_equal(uuid, buf)
end
def test_http_responder_get
def test_http_responder
uuid = SecureRandom.uuid.encode('UTF-8')
uri = URI('http://localhost:2000')
@ -56,46 +56,4 @@ 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