nanoserve 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/nanoserve.rb +42 -2
- data/test/test_nanoserve.rb +43 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd9fbbd494bd1b394456421ae52b56f1bf6e9bb6
|
4
|
+
data.tar.gz: e315b88e86cc00d00c07d34d7b01d33bc7b0a866
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4cc3d2af66545e95edffcefa7a7c1109d92a8f11bb3c3a85642421d128826a47854cbdbda91bff1e9379f816b330175c2f45937875a34efb4af81cb8049a28c
|
7
|
+
data.tar.gz: afc409468d9b08e2bb210878a9b8aae9be2f9dcebc77341767d68b955f8f54b647e6019491f330535a078382e4f9e9c10ff2208a0725acde7a153bdf880c7838
|
data/Gemfile.lock
CHANGED
data/lib/nanoserve.rb
CHANGED
@@ -98,8 +98,44 @@ module NanoServe
|
|
98
98
|
@body = +''.encode('ASCII-8BIT')
|
99
99
|
end
|
100
100
|
|
101
|
+
def host
|
102
|
+
@headers['host']
|
103
|
+
end
|
104
|
+
|
105
|
+
def path
|
106
|
+
@uri.path
|
107
|
+
end
|
108
|
+
|
109
|
+
def query_array
|
110
|
+
URI.decode_www_form(@uri.query || '')
|
111
|
+
end
|
112
|
+
|
113
|
+
def form_array
|
114
|
+
form? ? URI.decode_www_form(body) : []
|
115
|
+
end
|
116
|
+
|
117
|
+
def query
|
118
|
+
Hash[*query_array.flatten]
|
119
|
+
end
|
120
|
+
|
121
|
+
def form
|
122
|
+
Hash[*form_array.flatten]
|
123
|
+
end
|
124
|
+
|
101
125
|
def params
|
102
|
-
|
126
|
+
query.merge(form)
|
127
|
+
end
|
128
|
+
|
129
|
+
def form?
|
130
|
+
content_type == 'application/x-www-form-urlencoded'
|
131
|
+
end
|
132
|
+
|
133
|
+
def body
|
134
|
+
@body
|
135
|
+
end
|
136
|
+
|
137
|
+
def [](key)
|
138
|
+
@headers[key.downcase]
|
103
139
|
end
|
104
140
|
|
105
141
|
def <<(line)
|
@@ -124,6 +160,10 @@ module NanoServe
|
|
124
160
|
@headers.key?('content-length')
|
125
161
|
end
|
126
162
|
|
163
|
+
def content_type
|
164
|
+
@headers['content-type']
|
165
|
+
end
|
166
|
+
|
127
167
|
private
|
128
168
|
|
129
169
|
REQ_RE = %r{(?<method>[A-Z]+)\s+(?<path>\S+)\s+(?<version>HTTP/\d+.\d+)$}
|
@@ -139,7 +179,7 @@ module NanoServe
|
|
139
179
|
end
|
140
180
|
|
141
181
|
def parse_method(str)
|
142
|
-
str
|
182
|
+
str.upcase
|
143
183
|
end
|
144
184
|
|
145
185
|
def parse_path(str)
|
data/test/test_nanoserve.rb
CHANGED
@@ -29,7 +29,7 @@ class TestNanoServe < MiniTest::Test
|
|
29
29
|
assert_equal(uuid, buf)
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
32
|
+
def test_http_responder_get
|
33
33
|
uuid = SecureRandom.uuid.encode('UTF-8')
|
34
34
|
uri = URI('http://localhost:2000')
|
35
35
|
|
@@ -56,4 +56,46 @@ class TestNanoServe < MiniTest::Test
|
|
56
56
|
|
57
57
|
assert_equal(uuid, req.first.params['uuid'])
|
58
58
|
end
|
59
|
+
|
60
|
+
def test_http_responder_post
|
61
|
+
uuid = SecureRandom.uuid.encode('UTF-8')
|
62
|
+
uri = URI('http://localhost:2000')
|
63
|
+
|
64
|
+
r = NanoServe::HTTPResponder.new(uri.host, uri.port) do |res, req, y|
|
65
|
+
y << req
|
66
|
+
|
67
|
+
res.body = <<-EOS.gsub(/^ {8}/, '')
|
68
|
+
<html>
|
69
|
+
<head>
|
70
|
+
<title>An Example Page</title>
|
71
|
+
</head>
|
72
|
+
<body>
|
73
|
+
Hello World, this is a very simple HTML document.
|
74
|
+
</body>
|
75
|
+
</html>
|
76
|
+
EOS
|
77
|
+
end
|
78
|
+
|
79
|
+
req = r.start([]) do
|
80
|
+
Net::HTTP.post_form(
|
81
|
+
uri + "test?uuid=#{uuid}&p=query",
|
82
|
+
'p' => 'form',
|
83
|
+
'f' => 'foo',
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
r.stop
|
88
|
+
|
89
|
+
assert_equal(uuid, req.first.params['uuid'])
|
90
|
+
assert_equal(uuid, req.first.query['uuid'])
|
91
|
+
assert_nil(req.first.form['uuid'])
|
92
|
+
|
93
|
+
assert_equal('foo', req.first.params['f'])
|
94
|
+
assert_nil(req.first.query['f'])
|
95
|
+
assert_equal('foo', req.first.form['f'])
|
96
|
+
|
97
|
+
assert_equal('form', req.first.params['p'])
|
98
|
+
assert_equal('query', req.first.query['p'])
|
99
|
+
assert_equal('form', req.first.form['p'])
|
100
|
+
end
|
59
101
|
end
|