tikh-klarlack 0.0.8 → 0.0.9

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.
data/lib/klarlack.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'socket'
2
+ require 'digest'
2
3
  require 'varnish/socket_factory'
3
4
  require 'varnish/client'
4
5
 
@@ -127,7 +127,6 @@ module Varnish
127
127
  #
128
128
  # 1.
129
129
  # .purge :url, <regexp>
130
- # .purge :hash, <regexp>
131
130
  #
132
131
  # <regexp> is a string containing a varnish compatible regexp
133
132
  #
@@ -149,7 +148,7 @@ module Varnish
149
148
  #
150
149
  def purge(*args)
151
150
  c = 'purge'
152
- c << ".#{args.shift}" if [:url, :hash, :list].include?(args.first)
151
+ c << ".#{args.shift}" if [:url, :list].include?(args.first)
153
152
  response = cmd(c, *args)
154
153
  case c
155
154
  when 'purge.list'
@@ -249,6 +248,10 @@ module Varnish
249
248
  content = conn_read(length.to_i + 1) # +1 = \n
250
249
  if status.to_i == 107
251
250
  @conn.write "auth #{auth_response(content[0,32])}\n"
251
+ status, length = conn_gets.split # <status> <content_length>\n
252
+ content = conn_read(length.to_i + 1) # +1 = \n
253
+ content.chomp!
254
+ raise CommandFailed, "Command #{name} returned with status #{status}: #{content}" if status.to_i != 200
252
255
  end
253
256
  end
254
257
 
@@ -289,7 +292,6 @@ module Varnish
289
292
  ctx << File.read(secret_file)
290
293
  ctx << challenge
291
294
  ctx << "\n"
292
- puts "#{ctx.to_s}"
293
295
  ctx.to_s
294
296
  end
295
297
 
@@ -1,7 +1,7 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
3
  describe Varnish::Client do
4
-
4
+
5
5
  before(:each) do
6
6
  @varnish = Varnish::Client.new "127.0.0.1:6082"
7
7
  end
@@ -22,7 +22,7 @@ describe Varnish::Client do
22
22
  end
23
23
 
24
24
  it 'should use timeouts when sending commands' do
25
- Varnish::SocketFactory::Timer.should_receive(:timeout).and_return("200 0")
25
+ Varnish::SocketFactory::Timer.should_receive(:timeout).exactly(2).times.and_return("200 0")
26
26
  @varnish.timeout = 10
27
27
  @varnish.ping
28
28
  end
@@ -80,7 +80,6 @@ describe Varnish::Client do
80
80
 
81
81
  it '#purge should allow purging by url, hash and custom fields' do
82
82
  @varnish.purge(:url, '^/articles/.*').should be_true
83
- @varnish.purge(:hash, 12345).should be_true
84
83
  @varnish.purge("req.http.host", "~", "www.example.com").should be_true
85
84
  @varnish.purge("req.http.host ~ www.example.com").should be_true
86
85
  end
@@ -119,23 +118,13 @@ describe Varnish::Client do
119
118
 
120
119
  end
121
120
 
122
- describe '(regression)' do
123
-
124
- it '#purge.hash with regex containing backslashes should be escaped properly' do
125
- test_regex = '/home\?x=1'
126
- @varnish.purge :hash, test_regex
127
- list = @varnish.purge :list
128
- list.flatten.join.should include(test_regex)
129
- end
130
-
131
- end
132
121
 
133
122
  describe '(broken connection)' do
134
123
 
135
124
  before(:each) do
136
125
  fake_connection!(:connected => true, :return => "200 1\n")
137
126
  end
138
-
127
+
139
128
  it 'should fail with a Varnish::Error when the connection does return nil for gets' do
140
129
  @conn.stub!(:gets).and_return(nil)
141
130
  lambda { @varnish.ping }.should raise_error(Varnish::BrokenConnection)
@@ -147,9 +136,9 @@ describe Varnish::Client do
147
136
  end
148
137
 
149
138
  end
150
-
139
+
151
140
  describe '(daemon lifecycle)' do
152
-
141
+
153
142
  it '#start, #stop, #running?, #stopped? should bahave as advertised' do
154
143
  ensure_stopped # issues #stop
155
144
  @varnish.stopped?.should be_true
@@ -158,41 +147,59 @@ describe Varnish::Client do
158
147
  @varnish.stopped?.should be_false
159
148
  @varnish.running?.should be_true
160
149
  end
161
-
150
+
162
151
  it 'starting an already started daemon should raise an error' do
163
152
  ensure_started
164
153
  lambda { @varnish.start }.should raise_error(Varnish::CommandFailed)
165
154
  end
166
-
155
+
167
156
  it 'stopping an already stopped daemon should raise an error' do
168
157
  ensure_stopped
169
158
  lambda { @varnish.stop }.should raise_error(Varnish::CommandFailed)
170
159
  end
171
-
160
+
161
+ end
162
+
163
+ describe '(authentication)' do
164
+ before(:each) do
165
+ fake_connection!(:connected => false, :return => ["107 59\n", "xbhewxtfvjtxivucjwlajklgguylsehr\nAuthentication required.\n"])
166
+ @varnish.secret_file = "path/to/secret"
167
+ end
168
+
169
+ it 'send response on challenge request' do
170
+ File.should_receive(:read).with("path/to/secret").and_return("secret\n")
171
+ Varnish::SocketFactory.should_receive(:tcp_socket).and_return(@conn)
172
+ @conn.should_receive(:write).with("auth df7fd4ad1b16ffd6cc6af0730530a462ee2a365cc19a1d83cf3dc1bd1d5cd547\n")
173
+ @varnish.send(:connect)
174
+ end
172
175
  end
173
-
176
+
174
177
  def ensure_started
175
178
  @varnish.start if @varnish.stopped?
176
179
  while(!@varnish.running?) do sleep 0.1 end
177
180
  end
178
-
181
+
179
182
  def ensure_stopped
180
183
  @varnish.stop if @varnish.running?
181
184
  while(!@varnish.stopped?) do sleep 0.1 end
182
185
  end
183
-
186
+
184
187
  class FakeConn
185
188
  attr_accessor :retval
186
- def read(*args) @retval end
187
- def gets(*args) @retval end
189
+ def read(*args) @retval.shift end
190
+ def gets(*args) @retval.shift end
188
191
  def write(str) str.to_s.size end
189
192
  end
190
193
 
191
194
  def fake_connection!(opts = {})
192
195
  @conn = FakeConn.new
193
- @conn.retval = opts[:return] || ''
196
+ @conn.retval = wrap(opts[:return] || '')
194
197
  @varnish.stub!(:connected?).and_return(opts[:connected] || false)
195
198
  @varnish.instance_variable_set('@conn', @conn)
196
199
  end
197
200
 
201
+ def wrap(value)
202
+ value.is_a?(Array) ? value : [value]
203
+ end
204
+
198
205
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'spec'
1
+ require 'rspec'
2
2
 
3
3
  $LOAD_PATH.unshift(File.dirname(__FILE__))
4
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tikh-klarlack
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Max Sch\xC3\xB6fmann"
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-11-17 00:00:00 +03:00
19
+ date: 2010-11-24 00:00:00 +03:00
20
20
  default_executable:
21
21
  dependencies: []
22
22