nanoserve 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36d89ee5855668e13996733662f4ef7c00a1cc56
4
- data.tar.gz: 06c20ca6aeeaf09554954832c1241f82da2fc9a9
3
+ metadata.gz: cd9fbbd494bd1b394456421ae52b56f1bf6e9bb6
4
+ data.tar.gz: e315b88e86cc00d00c07d34d7b01d33bc7b0a866
5
5
  SHA512:
6
- metadata.gz: 98b1070c92caf9a53c8372ee688b6436be393f70d360635429690bd4e054bca84942d494f336fc9abdd37bc9165120a235faa755fd59e54968ecc71b95b11733
7
- data.tar.gz: edaf2f744b271f6134561cd42ee9f841dd8a2059d0734b48e18900fa449e136e9689e66cfd17daf4ae91b7887177c74f53b71bbfb82ebaf1e6bc2df1efdcfbaf
6
+ metadata.gz: a4cc3d2af66545e95edffcefa7a7c1109d92a8f11bb3c3a85642421d128826a47854cbdbda91bff1e9379f816b330175c2f45937875a34efb4af81cb8049a28c
7
+ data.tar.gz: afc409468d9b08e2bb210878a9b8aae9be2f9dcebc77341767d68b955f8f54b647e6019491f330535a078382e4f9e9c10ff2208a0725acde7a153bdf880c7838
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nanoserve (0.1.0)
4
+ nanoserve (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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
- Hash[*@uri.query.split('&').map { |kv| kv.split('=') }.flatten]
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)
@@ -29,7 +29,7 @@ class TestNanoServe < MiniTest::Test
29
29
  assert_equal(uuid, buf)
30
30
  end
31
31
 
32
- def test_http_responder
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nanoserve
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loic Nageleisen