Skip to content

Commit 7631e9e

Browse files
committedNov 1, 2015
Cleaner URL handing (WIP)
1 parent 8c9a64f commit 7631e9e

File tree

3 files changed

+85
-23
lines changed

3 files changed

+85
-23
lines changed
 

‎src/client.coffee

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ class CancellationToken
3434
# HTTP Client for connecting to etcd servers
3535
class Client
3636

37-
constructor: (@hosts, @sslopts) ->
37+
constructor: (@hosts, @options, @sslopts) ->
3838
@syncmsg = {}
3939

4040
execute: (method, options, callback) =>
41-
opt = _.defaults (_.clone options), defaultRequestOptions, { method: method }
41+
opt = _.defaults (_.clone options), @options, defaultRequestOptions, { method: method }
4242
opt.clientOptions = _.defaults opt.clientOptions, defaultClientOptions
4343

4444
servers = _.shuffle @hosts
@@ -59,7 +59,7 @@ class Client
5959
# Multiserver (cluster) executer
6060
_multiserverHelper: (servers, options, token, callback) =>
6161
host = _.first(servers)
62-
options.url = "#{options.serverprotocol}://#{host}#{options.path}"
62+
options.url = "#{host}#{options.path}"
6363

6464
return if token.isAborted() # Aborted by user?
6565

‎src/index.coffee

+26-13
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,30 @@ _ = require 'underscore'
22
Watcher = require './watcher'
33
Client = require './client'
44
HttpsAgent = (require 'https').Agent
5+
URL = require 'url-parse'
56

67
# Etcd client for etcd protocol version 2
78
class Etcd
89

910
# Constructor, set etcd host and port.
1011
# For https: provide {ca, crt, key} as sslopts.
11-
constructor: (host = '127.0.0.1', port = null, sslopts = null, client = null) ->
12+
# constructor: (host = '127.0.0.1', port = null, sslopts = null, client = null) ->
1213

13-
if _.isArray(host)
14-
@hosts = host
15-
@sslopts = port
16-
@client = sslopts
17-
else
18-
port ?= 4001
19-
@hosts = ["#{host}:#{port}"]
20-
@sslopts = sslopts
21-
@client = client
14+
# if _.isArray(host)
15+
# @hosts = host
16+
# @sslopts = port
17+
# @client = sslopts
18+
# else
19+
# port ?= 4001
20+
# @hosts = ["#{host}:#{port}"]
21+
# @sslopts = sslopts
22+
# @client = client
2223

23-
@client ?= new Client(@hosts, @sslopts)
24+
# @client ?= new Client(@hosts, @sslopts)
2425

26+
constructor: (hosts = "127.0.0.1:4001", options = {}) ->
27+
@hosts = @_cleanHostList hosts
28+
@client = new Client(@hosts, options, null)
2529

2630
# Set key to value
2731
# Usage:
@@ -230,15 +234,15 @@ class Etcd
230234

231235
# Prepare request options
232236
_prepareOpts: (path, apiVersion = "/v2", value = null, allOpts = {}) ->
233-
serverprotocol = if @sslopts? then "https" else "http"
237+
# serverprotocol = if @sslopts? then "https" else "http"
234238

235239
queryString = _.omit allOpts, 'maxRetries', 'synchronous'
236240

237241
clientOptions = _.pick allOpts, 'maxRetries'
238242

239243
opt = {
240244
path: "#{apiVersion}/#{path}"
241-
serverprotocol: serverprotocol
245+
# serverprotocol: serverprotocol
242246
json: true
243247
qs: queryString
244248
clientOptions: clientOptions
@@ -255,5 +259,14 @@ class Etcd
255259
else
256260
[options, callback]
257261

262+
# Make sure hosts is a list, make sure all have protocol added
263+
# defaults to http
264+
_cleanHostList: (hosts) ->
265+
hostlist = if _.isArray(hosts) then hosts else [hosts]
266+
hostlist.map (host) ->
267+
url = new URL(host)
268+
url.set 'protocol', 'http:' if url.protocol is ''
269+
url.href
270+
258271

259272
exports = module.exports = Etcd

‎test/index.coffee

+56-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ process.env.RUNNING_UNIT_TESTS = true
1010
getNock = (host = 'http://127.0.0.1:4001') ->
1111
nock host
1212

13+
14+
beforeEach () ->
15+
nock.cleanAll()
16+
1317
# Tests for utility functions
1418

1519
describe 'Utility', ->
@@ -28,6 +32,57 @@ describe 'Utility', ->
2832
path: '/v2/keypath/key'
2933
}
3034

35+
36+
describe 'Connecting', ->
37+
38+
mock = (host = 'http://127.0.0.1:4001/') ->
39+
nock(host)
40+
.get('/v2/keys/key')
41+
.reply(200, '{"action":"GET","key":"/key","value":"value","index":1}')
42+
43+
it 'should support empty constructor (localhost:4001)', (done) ->
44+
etcd = new Etcd
45+
m = mock()
46+
etcd.get 'key', (err, val) ->
47+
m.isDone().should.be.true
48+
done err, val
49+
50+
it 'should support string as connect host', (done) ->
51+
etcd = new Etcd "testhost.com:4009"
52+
m = mock("http://testhost.com:4009")
53+
etcd.get 'key', (err, val) ->
54+
m.isDone().should.be.true
55+
done err, val
56+
57+
it 'should support string prefixed by http:// as host', (done) ->
58+
etcd = new Etcd "http://testhost.com:4009"
59+
m = mock("http://testhost.com:4009")
60+
etcd.get 'key', (err, val) ->
61+
m.isDone().should.be.true
62+
done err, val
63+
64+
it 'should support string postfixed by / as host', (done) ->
65+
etcd = new Etcd "http://testhost.com:4009/"
66+
m = mock("http://testhost.com:4009")
67+
etcd.get 'key', (err, val) ->
68+
m.isDone().should.be.true
69+
done err, val
70+
71+
it 'should support array of strings as host', (done) ->
72+
etcd = new Etcd ["http://testhost.com:4009"]
73+
m = mock("http://testhost.com:4009")
74+
etcd.get 'key', (err, val) ->
75+
m.isDone().should.be.true
76+
done err, val
77+
78+
it 'should support https strings', (done) ->
79+
etcd = new Etcd ["https://testhost.com:1000"]
80+
m = mock("https://testhost.com:1000")
81+
etcd.get 'key', (err, val) ->
82+
m.isDone().should.be.true
83+
done err, val
84+
85+
3186
describe 'Basic functions', ->
3287

3388
etcd = new Etcd
@@ -308,13 +363,7 @@ describe 'Multiserver/Cluster support', ->
308363

309364
it 'should accept list of servers in constructor', ->
310365
etcd = new Etcd ['localhost:4001', 'localhost:4002']
311-
etcd.getHosts().should.eql ['localhost:4001', 'localhost:4002']
312-
313-
314-
it 'should accept host and port in constructor', ->
315-
etcd = new Etcd 'localhost', 4001
316-
etcd.getHosts().should.eql ['localhost:4001']
317-
366+
etcd.getHosts().should.eql ['http://localhost:4001', 'http://localhost:4002']
318367

319368
it 'should try next server in list on http error', (done) ->
320369
path = '/v2/keys/foo'

0 commit comments

Comments
 (0)
Please sign in to comment.