ww 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ww/double/spy.rb CHANGED
@@ -13,6 +13,10 @@ module Ww
13
13
  end
14
14
  end
15
15
 
16
+ def spy_them_all!
17
+ before { spy! }
18
+ end
19
+
16
20
  def requests
17
21
  @requests ||= Store.new
18
22
  end
@@ -23,7 +27,7 @@ module Ww
23
27
 
24
28
  module InstanceMethods
25
29
  def spy!
26
- self.class.store(@request)
30
+ self.class.store(@request) if @spyed ^ (@spyed = true)
27
31
  end
28
32
  alias stump! spy!
29
33
  end
data/lib/ww/double.rb CHANGED
@@ -18,16 +18,14 @@ module Ww
18
18
  end
19
19
 
20
20
  def unbound_action(klass, mname, block)
21
- ret = nil
22
21
  klass.module_eval do
23
22
  begin
24
23
  define_method(mname, &block)
25
- ret = instance_method(mname)
24
+ instance_method(mname)
26
25
  ensure
27
26
  remove_method(mname) if instance_methods.include?(mname)
28
27
  end
29
28
  end
30
- return ret
31
29
  end
32
30
  module_function :unbound_action
33
31
  end
data/lib/ww/server.rb CHANGED
@@ -24,7 +24,7 @@ module Ww
24
24
  end
25
25
 
26
26
  def_delegators :current_app, *double_methods = %w[
27
- spy requests mock verify stub
27
+ spy spy_them_all! requests mock verify stub
28
28
  ]
29
29
 
30
30
  attr_reader :app, :port
@@ -37,6 +37,7 @@ module Ww
37
37
 
38
38
  def start_once
39
39
  @app.reset!
40
+ current_app.testing_thread = Thread.current
40
41
  start! unless running?
41
42
  end
42
43
 
@@ -60,10 +61,27 @@ module Ww
60
61
  private
61
62
  def run_with_picking_server_instance!
62
63
  q = Queue.new
63
- @thread = Thread.new { @handler.run(@app, :Port => @port) {|server| q << server } }
64
+ opt = handler_options(@handler)
65
+ @thread = Thread.new { @handler.run(@app, opt ) {|server| q << silence!(server) } }
64
66
  @server = q.pop
65
67
  end
66
68
 
69
+ def handler_options(handler)
70
+ opt = {:Port => @port}
71
+ if handler.name == "Rack::Handler::WEBrick"
72
+ l = WEBrick::Log.new("/dev/null")
73
+ opt.update(:Logger => l, :AccessLog => [l, WEBrick::AccessLog::COMMON_LOG_FORMAT])
74
+ end
75
+ return opt
76
+ end
77
+
78
+ def silence!(server)
79
+ case server.class.name
80
+ when "Thin::Server" then server.silent = true
81
+ end
82
+ return server
83
+ end
84
+
67
85
  def shutdown_http_server
68
86
  case @server.class.name
69
87
  when "WEBrick::HTTPServer" then @server.shutdown
@@ -6,6 +6,14 @@
6
6
  %head
7
7
  %title&= title
8
8
  %script{:src=>"http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"}
9
+ :javascript
10
+ $(function(){
11
+ $(".request .headers h3").click(function(){ $(this).next("table").toggle() });
12
+ $(".request .body h3").click(function(){ $(this).next("pre").toggle() });
13
+ $(".request .headers table").hide();
14
+ $(".request .body pre").hide();
15
+ });
16
+
9
17
  %style{:type=>"text/css"}
10
18
  :sass
11
19
  body
@@ -15,7 +23,7 @@
15
23
  h1
16
24
  font-size: 10px
17
25
 
18
- div.stump
26
+ .request
19
27
  font-size: 10px
20
28
  border: 1px solid silver
21
29
  padding: 0 1em
@@ -50,18 +58,18 @@
50
58
  font-weight: normal
51
59
  font-family: 'Courier', monospace
52
60
 
53
- .headers table, div.body pre
61
+ .headers table, .body pre
54
62
  border: 1px dashed green
55
63
  margin: 1em
56
64
  padding: 1em
57
65
 
58
- div.body pre
66
+ .body pre
59
67
  overflow: auto
60
68
  font-size: 12px
61
69
  %body
62
70
  %h1&= title
63
71
  - requests.each do |wreq|
64
- %div.stump{:id => wreq.object_id}
72
+ .request{:id => wreq.object_id}
65
73
  %h2
66
74
  %span.method&= wreq.request_method
67
75
  %span.path&= wreq.fullpath
@@ -69,27 +77,19 @@
69
77
  %span.time&= wreq.time.iso8601
70
78
  from
71
79
  %span.source&= wreq.ip
72
- %div
73
- .headers
74
- %h3 Headers
75
- %table
76
- - wreq.env.each do |k,v|
77
- - next if k =~ /\A[a-z]/
78
- %tr
79
- %th&= k
80
- %td&= v
80
+ .headers
81
+ %h3 Headers
82
+ %table
83
+ - wreq.env.each do |k,v|
84
+ - next if k =~ /\A[a-z]/
85
+ %tr
86
+ %th&= k
87
+ %td&= v
81
88
 
82
- - unless wreq.get?
83
- .body
84
- %h3
85
- Body
86
- %span.media_type= wreq.media_type
87
- %pre&= wreq.parsed_body.pretty_inspect
89
+ - unless wreq.get?
90
+ .body
91
+ %h3
92
+ Body
93
+ %span.media_type= wreq.media_type
94
+ %pre&= wreq.parsed_body.pretty_inspect
88
95
 
89
- :javascript
90
- jQuery(function(){
91
- $("div.stump .headers h3").click(function(){ $(this).next("table").toggle() });
92
- $("div.stump .body h3").click(function(){ $(this).next("pre").toggle() });
93
- $("div.stump .headers table").hide();
94
- $("div.stump .body pre").hide();
95
- });
data/lib/ww.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Ww
2
- Version = '0.3.0'
2
+ Version = '0.3.1'
3
3
 
4
4
  def to_app(*args, &block)
5
5
  $stderr.puts <<-WORNING
data/spec/spec_helper.rb CHANGED
@@ -2,3 +2,21 @@ require 'rubygems'
2
2
  require 'sinatra'
3
3
  $: << File.expand_path("../lib", File.dirname(__FILE__))
4
4
 
5
+ module WwSpec
6
+ module ExampleMethods
7
+ def servlet_defining_get_root
8
+ Ww::Servlet.base do
9
+ get("/") do
10
+ response.status = 200
11
+ response["Content-Type"] = "text/plain"
12
+ response.body = "Hello World"
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ Spec::Runner.configure do |config|
20
+ config.include WwSpec::ExampleMethods
21
+ end
22
+
@@ -0,0 +1,60 @@
1
+ require File.expand_path("../../spec_helper", File.dirname(__FILE__))
2
+ require 'ww/servlet'
3
+
4
+ describe Ww::Double::Mock, "with Servlet" do
5
+ before do
6
+ @server = servlet_defining_get_root
7
+ end
8
+
9
+ describe "mock(:get, '/', :verify => lambda" do
10
+ before do
11
+ v = Proc.new {|req, par| par["entity_id"].to_i == 1 && par["entity_value"] == "var" }
12
+
13
+ @server.mock( :get, '/', :verify => v) do
14
+ response.status = 200
15
+ response["Content-Type"] = "text/plain"
16
+ response.body = "Hi World"
17
+ end
18
+ @server.testing_thread = Thread.new{ sleep }
19
+ end
20
+
21
+ it do
22
+ @server.new.call( Rack::MockRequest.env_for("/", :method => "GET"))
23
+ @server.testing_thread.should_not be_alive
24
+ end
25
+
26
+ it do
27
+ @server.new.call( Rack::MockRequest.env_for("/?entity_id=1&entity_value=var", :method => "GET"))
28
+ @server.testing_thread.should be_alive
29
+ end
30
+ end
31
+
32
+ describe "mock(:get, '/')" do
33
+ before do
34
+ @server.mock(:get, '/') do
35
+ response.status = 200
36
+ response["Content-Type"] = "text/plain"
37
+ response.body = "Hi World"
38
+ end
39
+ end
40
+
41
+ describe "call" do
42
+ before do
43
+ app = @server.new
44
+ @response = app.call( Rack::MockRequest.env_for("/", :method => "GET"))
45
+ end
46
+
47
+ subject{ @response }
48
+
49
+ it { should == [200, {"Content-Type"=>"text/plain", "Content-Length"=>"8"}, ["Hi World"]] }
50
+ it {
51
+ expect{ @server.verify }.should_not raise_error Ww::Double::MockError
52
+ }
53
+ end
54
+
55
+ describe "don't call" do
56
+ it { expect{ @server.verify }.should raise_error Ww::Double::MockError }
57
+ end
58
+ end
59
+ end
60
+
@@ -0,0 +1,80 @@
1
+ require File.expand_path("../../spec_helper", File.dirname(__FILE__))
2
+ require 'ww/servlet'
3
+
4
+ describe Ww::Double, "with Servlet" do
5
+ before do
6
+ @server = servlet_defining_get_root
7
+ end
8
+
9
+ describe "spy(:get, '/')" do
10
+ before do
11
+ @server.spy(:get, '/') do
12
+ response.status = 200
13
+ response["Content-Type"] = "text/plain"
14
+ response.body = "Hi World"
15
+ end
16
+ end
17
+
18
+ describe "GET / リクエストの" do
19
+ before do
20
+ app = @server.new
21
+ @response = app.call( Rack::MockRequest.env_for("/", :method => "GET"))
22
+ end
23
+
24
+ subject{ @server.requests.first }
25
+
26
+ it { should be_a Rack::Request }
27
+ its(:request_method) { should == 'GET' }
28
+ its(:fullpath) { should == "/" }
29
+
30
+ it "bodyは空のIOであること" do
31
+ subject.body.rewind
32
+ subject.body.read.should == ""
33
+ end
34
+
35
+ it "レスポンスは想定どおりのものであること" do
36
+ @response.should ==
37
+ [200, {"Content-Type"=>"text/plain", "Content-Length"=>"8"}, ["Hi World"]]
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "spy(:get, '/') backword compat, old version spy! or spy(:get..) called stump!" do
43
+ before do
44
+ @server.get('/backword') do
45
+ stump!
46
+
47
+ response.status = 200
48
+ response["Content-Type"] = "text/plain"
49
+ response.body = "Hi World"
50
+ end
51
+
52
+ app = @server.new
53
+ @response = app.call( Rack::MockRequest.env_for("/backword", :method => "GET"))
54
+ end
55
+
56
+ subject{ @server.requests }
57
+ it { should_not be_empty }
58
+ it { @server.requests.first.should be_a Ww::Double::Spy::Request }
59
+ end
60
+
61
+ describe "spy_them_all! - extend spy feature to all actions" do
62
+ before do
63
+ @server.spy_them_all!
64
+ @app = @server.new
65
+ 3.times{ @app.call( Rack::MockRequest.env_for("/", :method => "GET")) }
66
+ end
67
+ subject{ @server }
68
+
69
+ it { should have(3).requests }
70
+
71
+ describe "do-not collect if already collected" do
72
+ before do
73
+ @server.spy(:get, '/spyed'){ "Hello" }
74
+ @app.call( Rack::MockRequest.env_for("/spyed", :method => "GET"))
75
+ end
76
+ it { should have(3 + 1).requests }
77
+ end
78
+ end
79
+ end
80
+
@@ -0,0 +1,59 @@
1
+ require File.expand_path("../../spec_helper", File.dirname(__FILE__))
2
+ require 'ww/servlet'
3
+
4
+ describe Ww::Double::Stub, "included to Servlet" do
5
+ before do
6
+ @server = servlet_defining_get_root
7
+ end
8
+
9
+ describe "stub(:get, '/dynamic_add')" do
10
+ before do
11
+ @server.stub(:get, '/dynamic_add') do
12
+ response.status = 200
13
+ response["Content-Type"] = "text/plain"
14
+ response.body = "Hi World"
15
+ end
16
+ end
17
+
18
+ subject{
19
+ @server.new.call( Rack::MockRequest.env_for("/dynamic_add", :method => "GET"))
20
+ }
21
+
22
+ it { should == [200, {"Content-Type"=>"text/plain", "Content-Length"=>"8"}, ["Hi World"]] }
23
+ end
24
+
25
+ describe "stub(:get, '/') # override" do
26
+ before do
27
+ @server.stub(:get, '/') do
28
+ response.status = 200
29
+ response["Content-Type"] = "text/plain"
30
+ response.body = "Hi World"
31
+ end
32
+ end
33
+
34
+ subject{
35
+ @server.new.call( Rack::MockRequest.env_for("/", :method => "GET"))
36
+ }
37
+
38
+ it { should == [200, {"Content-Type"=>"text/plain", "Content-Length"=>"8"}, ["Hi World"]] }
39
+ end
40
+
41
+ describe "stub(:get, '/') # re-define after app initialized" do
42
+ before do
43
+ @app = @server.new
44
+
45
+ @server.stub(:get, '/') do
46
+ response.status = 200
47
+ response["Content-Type"] = "text/plain"
48
+ response.body = "Hi! World"
49
+ end
50
+ end
51
+
52
+ subject{
53
+ @app.call( Rack::MockRequest.env_for("/", :method => "GET"))
54
+ }
55
+
56
+ it { should == [200, {"Content-Type"=>"text/plain", "Content-Length"=>"9"}, ["Hi! World"]] }
57
+ end
58
+ end
59
+
@@ -5,7 +5,7 @@ require 'json'
5
5
 
6
6
  describe Ww::Server do
7
7
  before do
8
- Ww::Server.handler = :mongrel # Mongrel is most silent.
8
+ Ww::Server.handler = :webrick
9
9
  Ww::Server[:spec] ||= Ww::Server.build_double(3080) do
10
10
  get("/goodnight") { "Good night" }
11
11
  spy(:get, "/hello") { "Hello world" }
@@ -69,5 +69,26 @@ describe Ww::Server do
69
69
  expect{ Ww::Server[:spec].verify }.should raise_error Ww::Double::MockError
70
70
  end
71
71
  end
72
+
73
+ describe "mocking with verifying expectation" do
74
+ before do
75
+ v = lambda {|req,par| par["key"] == "value" }
76
+ Ww::Server[:spec].mock(:get, "/goodnight", :verify => v) do
77
+ "OYASUMI-NASAI"
78
+ end
79
+ end
80
+
81
+ it "fail unless access there" do
82
+ expect{
83
+ URI("http://localhost:3080/goodnight").read
84
+ }.should raise_error Ww::Double::MockError
85
+ end
86
+
87
+ it "pass if access there" do
88
+ expect{
89
+ URI("http://localhost:3080/goodnight?key=value").read
90
+ }.should_not raise_error
91
+ end
92
+ end
72
93
  end
73
94
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ww
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - moro
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-17 00:00:00 +09:00
12
+ date: 2010-01-20 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -61,10 +61,15 @@ extensions: []
61
61
  extra_rdoc_files: []
62
62
 
63
63
  files:
64
+ - README.rdoc
65
+ - ChangeLog
66
+ - MIT-LICENSE
64
67
  - Rakefile
65
68
  - spec/spec_helper.rb
66
69
  - spec/ww/application_spec.rb
67
- - spec/ww/double_spec.rb
70
+ - spec/ww/double/mock_spec.rb
71
+ - spec/ww/double/spy_spec.rb
72
+ - spec/ww/double/stub_spec.rb
68
73
  - spec/ww/double_spy_request_spec.rb
69
74
  - spec/ww/server_integration_spec.rb
70
75
  - spec/ww/server_spec.rb
@@ -1,144 +0,0 @@
1
- require File.expand_path("../spec_helper", File.dirname(__FILE__))
2
- require 'ww/servlet'
3
-
4
- describe Ww::Double, "with Servlet" do
5
- before do
6
- @server = Ww::Servlet.base do
7
- get("/") do
8
- response.status = 200
9
- response["Content-Type"] = "text/plain"
10
- response.body = "Hello World"
11
- end
12
- end
13
- end
14
-
15
- describe "stub(:get, '/dynamic_add')" do
16
- before do
17
- @server.stub(:get, '/dynamic_add') do
18
- response.status = 200
19
- response["Content-Type"] = "text/plain"
20
- response.body = "Hi World"
21
- end
22
- end
23
-
24
- subject{
25
- @server.new.call( Rack::MockRequest.env_for("/dynamic_add", :method => "GET"))
26
- }
27
-
28
- it { should == [200, {"Content-Type"=>"text/plain", "Content-Length"=>"8"}, ["Hi World"]] }
29
- end
30
-
31
- describe "stub(:get, '/') # override" do
32
- before do
33
- @server.stub(:get, '/') do
34
- response.status = 200
35
- response["Content-Type"] = "text/plain"
36
- response.body = "Hi World"
37
- end
38
- end
39
-
40
- subject{
41
- @server.new.call( Rack::MockRequest.env_for("/", :method => "GET"))
42
- }
43
-
44
- it { should == [200, {"Content-Type"=>"text/plain", "Content-Length"=>"8"}, ["Hi World"]] }
45
- end
46
-
47
- describe "stub(:get, '/') # re-define after app initialized" do
48
- before do
49
- @app = @server.new
50
-
51
- @server.stub(:get, '/') do
52
- response.status = 200
53
- response["Content-Type"] = "text/plain"
54
- response.body = "Hi! World"
55
- end
56
- end
57
-
58
- subject{
59
- @app.call( Rack::MockRequest.env_for("/", :method => "GET"))
60
- }
61
-
62
- it { should == [200, {"Content-Type"=>"text/plain", "Content-Length"=>"9"}, ["Hi! World"]] }
63
- end
64
-
65
- describe "mock(:get, '/')" do
66
- before do
67
- @server.mock(:get, '/') do
68
- response.status = 200
69
- response["Content-Type"] = "text/plain"
70
- response.body = "Hi World"
71
- end
72
- end
73
-
74
- describe "call" do
75
- before do
76
- app = @server.new
77
- @response = app.call( Rack::MockRequest.env_for("/", :method => "GET"))
78
- end
79
-
80
- subject{ @response }
81
-
82
- it { should == [200, {"Content-Type"=>"text/plain", "Content-Length"=>"8"}, ["Hi World"]] }
83
- it {
84
- expect{ @server.verify }.should_not raise_error Ww::Double::MockError
85
- }
86
- end
87
-
88
- describe "don't call" do
89
- it { expect{ @server.verify }.should raise_error Ww::Double::MockError }
90
- end
91
- end
92
-
93
- describe "spy(:get, '/')" do
94
- before do
95
- @server.spy(:get, '/') do
96
- response.status = 200
97
- response["Content-Type"] = "text/plain"
98
- response.body = "Hi World"
99
- end
100
- end
101
-
102
- describe "GET / リクエストの" do
103
- before do
104
- app = @server.new
105
- @response = app.call( Rack::MockRequest.env_for("/", :method => "GET"))
106
- end
107
-
108
- subject{ @server.requests.first }
109
-
110
- it { should be_a Rack::Request }
111
- its(:request_method) { should == 'GET' }
112
- its(:fullpath) { should == "/" }
113
-
114
- it "bodyは空のIOであること" do
115
- subject.body.rewind
116
- subject.body.read.should == ""
117
- end
118
-
119
- it "レスポンスは想定どおりのものであること" do
120
- @response.should ==
121
- [200, {"Content-Type"=>"text/plain", "Content-Length"=>"8"}, ["Hi World"]]
122
- end
123
- end
124
- end
125
-
126
- describe "spy(:get, '/') backword compat" do
127
- before do
128
- @server.get('/backword') do
129
- stump!
130
- response.status = 200
131
- response["Content-Type"] = "text/plain"
132
- response.body = "Hi World"
133
- end
134
-
135
- app = @server.new
136
- @response = app.call( Rack::MockRequest.env_for("/backword", :method => "GET"))
137
- end
138
-
139
- subject{ @server.requests }
140
- it { should_not be_empty }
141
- it { @server.requests.first.should be_a Ww::Double::Spy::Request }
142
- end
143
- end
144
-