zold 0.20.4 → 0.21.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
  SHA256:
3
- metadata.gz: 8f86c74b0e0ba785befac0d7dfe8064e26ed50da6d573f291a7ee31fefc5dac7
4
- data.tar.gz: 45e481a2b598866032c3fcc487bd7935f05615e7af517a8d76d998ae879bb1a2
3
+ metadata.gz: '08d854cecafd7b1690e4d00d3f10ab81e93c1a6ddc80691b7bd706a34e3914d6'
4
+ data.tar.gz: e8d569ab7b6d48f7293e98b55bb2fe52be904dc83a9039dde605be1a306692c8
5
5
  SHA512:
6
- metadata.gz: 74ec4dbe1a0e12603e374a9fe15d8228e4435c81d4d808e8155e4a8c4c91e42bec94a9d69b9bf1437fa3cadb2ed6fc9c04d5e84117a5c15e17d67540000864e4
7
- data.tar.gz: 72d5ebdcbf091de7dd2b0d52df27390b2a0f9ddd6f3da8ef9e9dcca3eaab4b8435edc4fa01baced1199eec1edc7dd115d14543013367ed972c675d1c083ac83c
6
+ metadata.gz: 916db3539dfeb1a9307cb291192f10c30e2b10e566d2c222951b83bf0a1ff3db97488af147c926b924288abb899d2ac8b98845963f85ee2f1684293aa92b2091
7
+ data.tar.gz: a874b08ca61533b78daec42dac0437195c1ab10360c83bdfe5469b893a04526e744d5e3ecbcb951b9e662fece172788e40d44097124afbe0f9b1fcf338d1968e
data/bin/zold CHANGED
@@ -281,3 +281,4 @@ else
281
281
  log.debug("Successfully finished in #{Zold::Age.new(start)}")
282
282
  end
283
283
 
284
+ abort # We need this in case some threads are still alive
@@ -139,17 +139,15 @@ run 'zold remote update' or use --tolerate-quorum=1"
139
139
  return 0
140
140
  end
141
141
  uri = "/wallet/#{id}"
142
- size = r.http(uri + '/size').get
143
- r.assert_code(200, size)
144
- res = r.http(uri).get(timeout: 2 + size.body.to_i * 0.01 / 1024)
145
- r.assert_code(200, res)
146
- json = JsonPage.new(res.body, uri).to_hash
142
+ head = r.http(uri).get
143
+ r.assert_code(200, head)
144
+ json = JsonPage.new(head.body, uri).to_hash
147
145
  score = Score.parse_json(json['score'])
148
146
  r.assert_valid_score(score)
149
147
  r.assert_score_ownership(score)
150
148
  r.assert_score_strength(score) unless opts['ignore-score-weakness']
151
149
  Tempfile.open(['', Wallet::EXT]) do |f|
152
- IO.write(f, json['body'])
150
+ r.http(uri + '.bin').get_file(f)
153
151
  wallet = Wallet.new(f.path)
154
152
  wallet.refurbish
155
153
  if wallet.protocol != Zold::PROTOCOL
@@ -105,7 +105,7 @@ Available options:"
105
105
  end
106
106
  unless opts['quiet-if-missed']
107
107
  if done.value.zero?
108
- raise "No nodes out of #{nodes} accepted the wallet #{id}; run 'zold remote update' and try again"
108
+ raise "No nodes out of #{nodes.value} accepted the wallet #{id}; run 'zold remote update' and try again"
109
109
  end
110
110
  if masters.value.zero? && !opts['tolerate-edges']
111
111
  raise EdgesOnly, "There are only edge nodes, run 'zold remote update' or use --tolerate-edges"
@@ -125,12 +125,14 @@ out of #{nodes.value} in #{Age.new(start)}, total score for #{id} is #{total.val
125
125
  return 0
126
126
  end
127
127
  start = Time.now
128
- content = @wallets.acq(id) do |wallet|
129
- raise "The wallet #{id} is absent" unless wallet.exists?
130
- IO.read(wallet.path)
131
- end
132
128
  uri = "/wallet/#{id}"
133
- response = r.http(uri).put(content, timeout: 2 + content.length * 0.01 / 1024)
129
+ response = Tempfile.open do |f|
130
+ @wallets.acq(id) do |wallet|
131
+ raise "The wallet #{id} is absent" unless wallet.exists?
132
+ FileUtils.copy_file(wallet.path, f.path)
133
+ end
134
+ r.http(uri).put(f)
135
+ end
134
136
  @wallets.acq(id) do |wallet|
135
137
  if response.status == 304
136
138
  @log.info("#{r}: same version of #{wallet.mnemo} there, in #{Age.new(start, limit: 0.5)}")
data/lib/zold/http.rb CHANGED
@@ -32,6 +32,44 @@ require_relative 'version'
32
32
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
33
33
  # License:: MIT
34
34
  module Zold
35
+ # Some clients waits for status method in response
36
+ class HttpResponse < SimpleDelegator
37
+ def status
38
+ code.zero? ? 599 : code
39
+ end
40
+
41
+ def status_line
42
+ status_message || ''
43
+ end
44
+
45
+ def to_s
46
+ "#{status}: #{status_line}\n#{body}"
47
+ end
48
+ end
49
+
50
+ # The error, if connection fails
51
+ class HttpError < HttpResponse
52
+ def initialize(ex)
53
+ @ex = ex
54
+ end
55
+
56
+ def body
57
+ Backtrace.new(@ex).to_s
58
+ end
59
+
60
+ def status
61
+ 599
62
+ end
63
+
64
+ def status_line
65
+ @ex.message || ''
66
+ end
67
+
68
+ def headers
69
+ {}
70
+ end
71
+ end
72
+
35
73
  # Http page
36
74
  class Http
37
75
  # HTTP header we add to each HTTP request, in order to inform
@@ -68,7 +106,7 @@ module Zold
68
106
  end
69
107
 
70
108
  def get(timeout: READ_TIMEOUT)
71
- Response.new(
109
+ HttpResponse.new(
72
110
  Typhoeus::Request.get(
73
111
  @uri,
74
112
  accept_encoding: 'gzip',
@@ -78,64 +116,48 @@ module Zold
78
116
  )
79
117
  )
80
118
  rescue StandardError => e
81
- Error.new(e)
119
+ HttpError.new(e)
120
+ end
121
+
122
+ def get_file(file)
123
+ File.open(file, 'w') do |f|
124
+ request = Typhoeus::Request.new(
125
+ @uri,
126
+ accept_encoding: 'gzip',
127
+ headers: headers,
128
+ connecttimeout: CONNECT_TIMEOUT
129
+ )
130
+ request.on_body do |chunk|
131
+ f.write(chunk)
132
+ end
133
+ request.run
134
+ response = new HttpResponse(request)
135
+ raise "Invalid response code #{response.status}" unless response.status == 200
136
+ response
137
+ end
138
+ rescue StandardError => e
139
+ HttpError.new(e)
82
140
  end
83
141
 
84
- def put(body, timeout: READ_TIMEOUT)
85
- Response.new(
142
+ def put(file)
143
+ HttpResponse.new(
86
144
  Typhoeus::Request.put(
87
145
  @uri,
88
146
  accept_encoding: 'gzip',
89
- body: body,
90
- headers: headers.merge('Content-Type': 'text/plain'),
147
+ body: IO.read(file),
148
+ headers: headers.merge(
149
+ 'Content-Type': 'text/plain'
150
+ ),
91
151
  connecttimeout: CONNECT_TIMEOUT,
92
- timeout: timeout
152
+ timeout: 2 + File.size(file) * 0.01 / 1024
93
153
  )
94
154
  )
95
155
  rescue StandardError => e
96
- Error.new(e)
156
+ HttpError.new(e)
97
157
  end
98
158
 
99
159
  private
100
160
 
101
- # Some clients waits for status method in response
102
- class Response < SimpleDelegator
103
- def status
104
- code.zero? ? 599 : code
105
- end
106
-
107
- def status_line
108
- status_message || ''
109
- end
110
-
111
- def to_s
112
- "#{status}: #{status_line}\n#{body}"
113
- end
114
- end
115
-
116
- # The error, if connection fails
117
- class Error < Response
118
- def initialize(ex)
119
- @ex = ex
120
- end
121
-
122
- def body
123
- Backtrace.new(@ex).to_s
124
- end
125
-
126
- def status
127
- 599
128
- end
129
-
130
- def status_line
131
- @ex.message || ''
132
- end
133
-
134
- def headers
135
- {}
136
- end
137
- end
138
-
139
161
  def headers
140
162
  headers = {
141
163
  'User-Agent': "Zold #{VERSION}",
@@ -249,26 +249,7 @@ this is not a normal behavior, you may want to report a bug to our GitHub reposi
249
249
  size: wallet.size,
250
250
  digest: wallet.digest,
251
251
  copies: Copies.new(File.join(settings.copies, wallet.id)).all.count,
252
- balance: wallet.balance.to_i,
253
- body: IO.read(wallet.path)
254
- )
255
- end
256
- end
257
-
258
- get %r{/wallet/(?<id>[A-Fa-f0-9]{16}).json} do
259
- fetch('application/json') do |wallet|
260
- pretty(
261
- version: settings.opts['expose-version'],
262
- alias: settings.node_alias,
263
- protocol: settings.protocol,
264
- id: wallet.id.to_s,
265
- score: score.to_h,
266
- wallets: total_wallets,
267
- key: wallet.key.to_pub,
268
- mtime: wallet.mtime.utc.iso8601,
269
- digest: wallet.digest,
270
- balance: wallet.balance.to_i,
271
- txns: wallet.txns.count
252
+ balance: wallet.balance.to_i
272
253
  )
273
254
  end
274
255
  end
@@ -339,7 +320,7 @@ this is not a normal behavior, you may want to report a bug to our GitHub reposi
339
320
  end
340
321
 
341
322
  get %r{/wallet/(?<id>[A-Fa-f0-9]{16})\.bin} do
342
- fetch { |w| IO.read(w.path) }
323
+ fetch { |w| send_file(w.path) }
343
324
  end
344
325
 
345
326
  get %r{/wallet/(?<id>[A-Fa-f0-9]{16})/copies} do
data/lib/zold/version.rb CHANGED
@@ -25,7 +25,7 @@
25
25
  # Copyright:: Copyright (c) 2018 Yegor Bugayenko
26
26
  # License:: MIT
27
27
  module Zold
28
- VERSION = '0.20.4'
28
+ VERSION = '0.21.0'
29
29
  PROTOCOL = 2
30
30
  REPO = 'zold-io/zold'
31
31
  end
@@ -44,17 +44,17 @@ class TestFetch < Zold::Test
44
44
  def test_fetches_wallet
45
45
  FakeHome.new(log: test_log).run do |home|
46
46
  wallet = home.create_wallet
47
- stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}/size")
48
- .to_return(status: 200, body: wallet.size.to_s)
49
47
  stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}").to_return(
50
48
  status: 200,
51
49
  body: {
52
50
  'score': Zold::Score::ZERO.to_h,
53
- 'body': IO.read(wallet.path),
51
+ 'size': 10_000,
54
52
  'mtime': Time.now.utc.iso8601
55
53
  }.to_json
56
54
  )
57
- stub_request(:get, "http://localhost:81/wallet/#{wallet.id}/size").to_return(status: 404)
55
+ stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}.bin")
56
+ .to_return(status: 200, body: IO.read(wallet.path))
57
+ stub_request(:get, "http://localhost:81/wallet/#{wallet.id}").to_return(status: 404)
58
58
  remotes = home.remotes
59
59
  remotes.add('localhost', 4096)
60
60
  remotes.add('localhost', 81)
@@ -72,27 +72,27 @@ class TestFetch < Zold::Test
72
72
  log = TestLogger.new(test_log)
73
73
  FakeHome.new(log: log).run do |home|
74
74
  wallet_a = home.create_wallet
75
- stub_request(:get, "http://localhost:4096/wallet/#{wallet_a.id}/size")
76
- .to_return(status: 200, body: wallet_a.size.to_s)
77
75
  stub_request(:get, "http://localhost:4096/wallet/#{wallet_a.id}").to_return(
78
76
  status: 200,
79
77
  body: {
80
78
  'score': Zold::Score::ZERO.to_h,
81
- 'body': IO.read(wallet_a.path),
79
+ 'size': 10_000,
82
80
  'mtime': Time.now.utc.iso8601
83
81
  }.to_json
84
82
  )
83
+ stub_request(:get, "http://localhost:4096/wallet/#{wallet_a.id}.bin")
84
+ .to_return(status: 200, body: IO.read(wallet_a.path))
85
85
  wallet_b = home.create_wallet
86
- stub_request(:get, "http://localhost:4096/wallet/#{wallet_b.id}/size")
87
- .to_return(status: 200, body: wallet_b.size.to_s)
88
86
  stub_request(:get, "http://localhost:4096/wallet/#{wallet_b.id}").to_return(
89
87
  status: 200,
90
88
  body: {
91
89
  'score': Zold::Score::ZERO.to_h,
92
- 'body': IO.read(wallet_b.path),
90
+ 'size': 10_000,
93
91
  'mtime': Time.now.utc.iso8601
94
92
  }.to_json
95
93
  )
94
+ stub_request(:get, "http://localhost:4096/wallet/#{wallet_b.id}.bin")
95
+ .to_return(status: 200, body: IO.read(wallet_b.path))
96
96
  remotes = home.remotes
97
97
  remotes.add('localhost', 4096)
98
98
  copies_a = home.copies(wallet_a)
@@ -115,16 +115,16 @@ class TestFetch < Zold::Test
115
115
  def test_fails_when_only_edge_nodes
116
116
  FakeHome.new(log: test_log).run do |home|
117
117
  wallet = home.create_wallet
118
- stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}/size")
119
- .to_return(status: 200, body: wallet.size.to_s)
120
118
  stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}").to_return(
121
119
  status: 200,
122
120
  body: {
123
121
  'score': Zold::Score::ZERO.to_h,
124
- 'body': IO.read(wallet.path),
122
+ 'size': 10_000,
125
123
  'mtime': Time.now.utc.iso8601
126
124
  }.to_json
127
125
  )
126
+ stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}.bin")
127
+ .to_return(status: 200, body: IO.read(wallet.path))
128
128
  remotes = home.remotes
129
129
  remotes.add('localhost', 4096)
130
130
  copies = home.copies(wallet)
@@ -139,16 +139,16 @@ class TestFetch < Zold::Test
139
139
  def test_fails_when_only_one_node
140
140
  FakeHome.new(log: test_log).run do |home|
141
141
  wallet = home.create_wallet
142
- stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}/size")
143
- .to_return(status: 200, body: wallet.size.to_s)
144
142
  stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}").to_return(
145
143
  status: 200,
146
144
  body: {
147
145
  'score': Zold::Score::ZERO.to_h,
148
- 'body': IO.read(wallet.path),
146
+ 'size': 10_000,
149
147
  'mtime': Time.now.utc.iso8601
150
148
  }.to_json
151
149
  )
150
+ stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}.bin")
151
+ .to_return(status: 200, body: IO.read(wallet.path))
152
152
  remotes = home.remotes
153
153
  remotes.add('localhost', 4096)
154
154
  copies = home.copies(wallet)
@@ -57,9 +57,10 @@ class TestPay < Zold::Test
57
57
  remotes = home.remotes
58
58
  remotes.add('localhost', 4096)
59
59
  json = home.create_wallet_json
60
- id = Zold::JsonPage.new(json).to_hash['id']
61
- stub_request(:get, "http://localhost:4096/wallet/#{id}/size").to_return(status: 200, body: '10000')
60
+ hash = Zold::JsonPage.new(json).to_hash
61
+ id = hash['id']
62
62
  stub_request(:get, "http://localhost:4096/wallet/#{id}").to_return(status: 200, body: json)
63
+ stub_request(:get, "http://localhost:4096/wallet/#{id}.bin").to_return(status: 200, body: hash['body'])
63
64
  home.wallets.acq(Zold::Id.new(id)) { |w| File.delete(w.path) }
64
65
  source = home.create_wallet
65
66
  amount = Zold::Amount.new(zld: 14.95)
@@ -38,9 +38,10 @@ class TestPull < Zold::Test
38
38
  remotes = home.remotes
39
39
  remotes.add('localhost', 4096)
40
40
  json = home.create_wallet_json
41
- id = Zold::JsonPage.new(json).to_hash['id']
42
- stub_request(:get, "http://localhost:4096/wallet/#{id}/size").to_return(status: 200, body: '10000')
41
+ hash = Zold::JsonPage.new(json).to_hash
42
+ id = hash['id']
43
43
  stub_request(:get, "http://localhost:4096/wallet/#{id}").to_return(status: 200, body: json)
44
+ stub_request(:get, "http://localhost:4096/wallet/#{id}.bin").to_return(status: 200, body: hash['body'])
44
45
  Zold::Pull.new(wallets: home.wallets, remotes: remotes, copies: home.copies.root.to_s, log: test_log).run(
45
46
  ['--ignore-this-stupid-option', 'pull', id.to_s, '--tolerate-edges', '--tolerate-quorum=1']
46
47
  )
@@ -55,9 +56,10 @@ class TestPull < Zold::Test
55
56
  remotes = home.remotes
56
57
  remotes.add('localhost', 4096)
57
58
  json = home.create_wallet_json
58
- id = Zold::JsonPage.new(json).to_hash['id']
59
- stub_request(:get, "http://localhost:4096/wallet/#{id}/size").to_return(status: 200, body: '10000')
59
+ hash = Zold::JsonPage.new(json).to_hash
60
+ id = hash['id']
60
61
  stub_request(:get, "http://localhost:4096/wallet/#{id}").to_return(status: 200, body: json)
62
+ stub_request(:get, "http://localhost:4096/wallet/#{id}.bin").to_return(status: 200, body: hash['body'])
61
63
  assert_raises Zold::Fetch::EdgesOnly do
62
64
  Zold::Pull.new(wallets: home.wallets, remotes: remotes, copies: home.copies.root.to_s, log: test_log).run(
63
65
  ['--ignore-this-stupid-option', 'pull', id.to_s]
data/test/fake_home.rb CHANGED
@@ -81,6 +81,7 @@ class FakeHome
81
81
  protocol: Zold::PROTOCOL,
82
82
  id: wallet.id.to_s,
83
83
  score: score.to_h,
84
+ size: wallet.size,
84
85
  wallets: 1,
85
86
  mtime: wallet.mtime.utc.iso8601,
86
87
  digest: wallet.digest,
@@ -93,7 +93,6 @@ class FrontTest < Zold::Test
93
93
  '/this-is-absent',
94
94
  '/wallet/ffffeeeeddddcccc',
95
95
  '/wallet/ffffeeeeddddcccc.bin',
96
- '/wallet/ffffeeeeddddcccc.json',
97
96
  '/wallet/ffffeeeeddddcccc.txt',
98
97
  '/wallet/ffffeeeeddddcccc/balance',
99
98
  '/wallet/ffffeeeeddddcccc/key',
@@ -147,12 +146,11 @@ class FrontTest < Zold::Test
147
146
  FakeNode.new(log: test_log).run(['--ignore-score-weakness', '--standalone']) do |port|
148
147
  wallet = home.create_wallet(txns: 2)
149
148
  base = "http://localhost:#{port}"
150
- response = Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(IO.read(wallet.path))
149
+ response = Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(wallet.path)
151
150
  assert_equal(200, response.status, response.body)
152
151
  assert_equal_wait(200) { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").get.status }
153
152
  [
154
153
  "/wallet/#{wallet.id}.txt",
155
- "/wallet/#{wallet.id}.json",
156
154
  "/wallet/#{wallet.id}/balance",
157
155
  "/wallet/#{wallet.id}/key",
158
156
  "/wallet/#{wallet.id}/mtime",
@@ -177,7 +175,7 @@ class FrontTest < Zold::Test
177
175
  FakeHome.new(log: test_log).run do |home|
178
176
  wallet = home.create_wallet
179
177
  base = "http://localhost:#{port}"
180
- Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(IO.read(wallet.path))
178
+ Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(wallet.path)
181
179
  assert_equal_wait(200) { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").get.status }
182
180
  threads = []
183
181
  mutex = Mutex.new
@@ -200,12 +198,11 @@ class FrontTest < Zold::Test
200
198
  base = "http://localhost:#{port}"
201
199
  assert_equal(
202
200
  200,
203
- Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(IO.read(wallet.path)).status
201
+ Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(wallet.path).status
204
202
  )
205
203
  assert_equal_wait(200) { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").get.status }
206
204
  3.times do
207
- r = Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}")
208
- .put(IO.read(wallet.path))
205
+ r = Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(wallet.path)
209
206
  assert_equal(304, r.status, r.body)
210
207
  end
211
208
  end
@@ -218,7 +215,7 @@ class FrontTest < Zold::Test
218
215
  FakeHome.new(log: test_log).run do |home|
219
216
  Threads.new(5).assert do
220
217
  wallet = home.create_wallet
221
- Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(IO.read(wallet.path))
218
+ Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(wallet.path)
222
219
  assert_equal_wait(200) { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").get.status }
223
220
  end
224
221
  end
@@ -350,12 +347,12 @@ class FrontTest < Zold::Test
350
347
  FakeHome.new(log: test_log).run do |home|
351
348
  wallet = home.create_wallet(Zold::Id::ROOT)
352
349
  base = "http://localhost:#{port}"
353
- Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(IO.read(wallet.path))
350
+ Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(wallet.path)
354
351
  assert_equal_wait(200) { Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").get.status }
355
352
  cycles = 50
356
353
  cycles.times do
357
354
  wallet.sub(Zold::Amount.new(zents: 10), "NOPREFIX@#{Zold::Id.new}", key)
358
- Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(IO.read(wallet.path))
355
+ Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").put(wallet.path)
359
356
  assert_equal(200, Zold::Http.new(uri: "#{base}/wallet/#{wallet.id}").get.status)
360
357
  end
361
358
  assert_equal_wait(-10 * cycles) do
data/test/test_http.rb CHANGED
@@ -98,7 +98,7 @@ class TestHttp < Zold::Test
98
98
  Zold::VerboseThread.new(test_log).run do
99
99
  server = TCPServer.new(port)
100
100
  client = server.accept
101
- client.puts("HTTP/1.1 200 OK\nContent-Length: 4\n\n")
101
+ client.puts("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\n")
102
102
  sleep 1
103
103
  client.puts('Good')
104
104
  client.close
@@ -139,7 +139,10 @@ class TestHttp < Zold::Test
139
139
  socket.close
140
140
  end
141
141
  end
142
- res = Zold::Http.new(uri: "http://127.0.0.1:#{port}/").put('how are you?')
142
+ res = Tempfile.open do |f|
143
+ IO.write(f, 'How are you?')
144
+ Zold::Http.new(uri: "http://127.0.0.1:#{port}/").put(f)
145
+ end
143
146
  assert_equal(200, res.status, res)
144
147
  assert(body.include?('Content-Length: 12'), body)
145
148
  assert(body.include?('Content-Type: text/plain'))
@@ -157,4 +160,63 @@ class TestHttp < Zold::Test
157
160
  res = Zold::Http.new(uri: 'http://some-host-3/').get
158
161
  assert_equal(200, res.status, res)
159
162
  end
163
+
164
+ def test_uploads_file
165
+ WebMock.allow_net_connect!
166
+ RandomPort::Pool::SINGLETON.acquire do |port|
167
+ thread = Thread.start do
168
+ Zold::VerboseThread.new(test_log).run do
169
+ server = TCPServer.new(port)
170
+ socket = server.accept
171
+ body = ''
172
+ stops = 0
173
+ loop do
174
+ part = socket.read_nonblock(5, exception: false)
175
+ if part == :wait_readable
176
+ break if stops > 5
177
+ stops += 1
178
+ sleep 0.001
179
+ else
180
+ body += part
181
+ stops = 0
182
+ end
183
+ end
184
+ socket.close_read
185
+ socket.print("HTTP/1.1 200 OK\nContent-Length: #{body.length}\n\n#{body}")
186
+ socket.close_write
187
+ end
188
+ end
189
+ content = "how are you\nmy friend"
190
+ res = Tempfile.open do |f|
191
+ IO.write(f, content)
192
+ Zold::Http.new(uri: "http://localhost:#{port}/").put(f)
193
+ end
194
+ assert_equal(200, res.status, res)
195
+ assert(res.body.include?(content), res)
196
+ thread.kill
197
+ thread.join
198
+ end
199
+ end
200
+
201
+ def test_downloads_file
202
+ WebMock.allow_net_connect!
203
+ RandomPort::Pool::SINGLETON.acquire do |port|
204
+ content = "how are you\nmy friend" * 1000
205
+ thread = Thread.start do
206
+ Zold::VerboseThread.new(test_log).run do
207
+ server = TCPServer.new(port)
208
+ socket = server.accept
209
+ socket.print("HTTP/1.1 200 OK\nContent-Length: #{content.length}\n\n#{content}")
210
+ socket.close_write
211
+ end
212
+ end
213
+ body = Tempfile.open do |f|
214
+ Zold::Http.new(uri: "http://localhost:#{port}/").get_file(f)
215
+ IO.read(f)
216
+ end
217
+ assert(body.include?(content), body)
218
+ thread.kill
219
+ thread.join
220
+ end
221
+ end
160
222
  end
@@ -37,7 +37,7 @@ class TestHungryWallets < Zold::Test
37
37
  def test_pulls_wallet
38
38
  FakeHome.new(log: test_log).run do |home|
39
39
  id = Zold::Id.new
40
- get = stub_request(:get, "http://localhost:4096/wallet/#{id}/size").to_return(status: 404)
40
+ get = stub_request(:get, "http://localhost:4096/wallet/#{id}").to_return(status: 404)
41
41
  remotes = home.remotes
42
42
  remotes.add('localhost', 4096)
43
43
  pool = Zold::ThreadPool.new('test', log: test_log)
data/zold.gemspec CHANGED
@@ -69,6 +69,7 @@ and suggests a different architecture for digital wallet maintenance.'
69
69
  s.add_runtime_dependency 'get_process_mem', '~>0.2'
70
70
  s.add_runtime_dependency 'json', '2.1.0'
71
71
  s.add_runtime_dependency 'memory_profiler', '0.9.12'
72
+ s.add_runtime_dependency 'mimic', '0.4.2'
72
73
  s.add_runtime_dependency 'openssl', '2.1.2'
73
74
  s.add_runtime_dependency 'rainbow', '3.0.0'
74
75
  s.add_runtime_dependency 'rake', '12.3.1' # has to stay here for Heroku
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.4
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-22 00:00:00.000000000 Z
11
+ date: 2018-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - '='
123
123
  - !ruby/object:Gem::Version
124
124
  version: 0.9.12
125
+ - !ruby/object:Gem::Dependency
126
+ name: mimic
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '='
130
+ - !ruby/object:Gem::Version
131
+ version: 0.4.2
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '='
137
+ - !ruby/object:Gem::Version
138
+ version: 0.4.2
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: openssl
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -692,7 +706,7 @@ licenses:
692
706
  - MIT
693
707
  metadata: {}
694
708
  post_install_message: |-
695
- Thanks for installing Zold 0.20.4!
709
+ Thanks for installing Zold 0.21.0!
696
710
  Study our White Paper: https://papers.zold.io/wp.pdf
697
711
  Read our blog posts: https://blog.zold.io
698
712
  Try online wallet at: https://wts.zold.io