sinatra_fake_webservice 0.2.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.9.0
@@ -1,13 +1,22 @@
1
-
1
+ require 'net/http'
2
+ require 'uri'
2
3
 
3
4
  class SinatraWebService
4
5
 
5
6
  attr_accessor :host, :port
7
+ attr_accessor :current_thread
6
8
 
7
9
  class SinatraStem < Sinatra::Base
10
+
11
+ enable :methodoverride
12
+
8
13
  get '/' do
9
14
  "Hello! i am lindesy lohan!"
10
15
  end
16
+
17
+ post '/gimmie' do
18
+ "here ya go"
19
+ end
11
20
  end
12
21
 
13
22
 
@@ -17,13 +26,17 @@ class SinatraWebService
17
26
  end
18
27
 
19
28
  def running?
20
- false
29
+ self.current_thread.alive? rescue false
21
30
  end
22
31
 
23
32
  def run!
33
+ if Thread.list.size > 1
34
+ Thread.list.first.kill
35
+ end
36
+
24
37
  @port = find_free_port
25
38
 
26
- app = Thread.new("running_app") do
39
+ self.current_thread = Thread.new do
27
40
  SinatraStem.run! :post => @host, :port => @port.to_i
28
41
  sleep 1
29
42
  end
@@ -46,8 +59,32 @@ class SinatraWebService
46
59
  end
47
60
  end
48
61
 
62
+ def get_response(action)
63
+ res = Net::HTTP.start(self.host, self.port) do |http|
64
+ http.get(action)
65
+ end
66
+ end
67
+
68
+ def delete_response(action, data = "", headers = nil, dest = nil)
69
+ data = data.empty? ? "_method=delete" : data += "&_method=delete"
70
+ post_response(action, data, headers, dest)
71
+ end
72
+
73
+ def put_response(action, data = "", headers = nil, dest = nil)
74
+ data = data.empty? ? "_method=put" : data += "&_method=put"
75
+ post_response(action, data, headers, dest)
76
+ end
77
+
78
+ def post_response(action, data, headers = nil, dest = nil)
79
+ res = Net::HTTP.start(self.host, self.port) do |http|
80
+ http.post(action, data, headers, dest)
81
+ end
82
+ end
83
+
49
84
  def method_missing(method, *args, &block)
50
- SinatraStem.send(method, *args, &block) unless method.to_s == "proxy"
85
+ SinatraStem.instance_eval do |base|
86
+ route method.to_s.upcase, *args, &block
87
+ end
51
88
  end
52
89
 
53
90
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_fake_webservice}
8
- s.version = "0.2.0"
8
+ s.version = "0.9.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Elad Meidar"]
12
- s.date = %q{2010-02-18}
12
+ s.date = %q{2010-03-03}
13
13
  s.description = %q{FakeWeb allows you to fake a response from a specific url, this gem intends to give developers the option to allow several responses from the same url based on parameters (ex: WSDL)}
14
14
  s.email = %q{elad@nautilus6.com}
15
15
  s.extra_rdoc_files = [
@@ -23,9 +23,7 @@ Gem::Specification.new do |s|
23
23
  "README.rdoc",
24
24
  "Rakefile",
25
25
  "VERSION",
26
- "lib/sinatra_ext.rb",
27
26
  "lib/sinatra_webservice.rb",
28
- "lib/web_service.rb",
29
27
  "metal/sinatra_stem.rb",
30
28
  "sinatra.log",
31
29
  "sinatra_fake_webservice.gemspec",
data/test/helper.rb CHANGED
@@ -1,16 +1,15 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
- require 'net/http'
5
- require 'uri'
4
+
6
5
 
7
6
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
7
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'metal'))
9
8
  $LOAD_PATH.unshift(File.dirname(__FILE__))
10
9
  require 'sinatra/base'
11
- #require 'sinatra_ext'
10
+
12
11
  require 'sinatra_webservice'
13
- #require 'web_service'
12
+
14
13
 
15
14
  class Test::Unit::TestCase
16
15
  end
@@ -6,6 +6,23 @@ class TestSinatraFakeWebservice < Test::Unit::TestCase
6
6
 
7
7
  setup do
8
8
  @sinatra_app = SinatraWebService.new
9
+ @sinatra_app.run!
10
+
11
+ @sinatra_app.get '/payme' do
12
+ "OMG I GOT PAID"
13
+ end
14
+
15
+ @sinatra_app.post '/returnme' do
16
+ "#{params[:return_value]}"
17
+ end
18
+
19
+ @sinatra_app.delete '/killme' do
20
+ "argh! i is dead."
21
+ end
22
+
23
+ @sinatra_app.put '/gimmieitnow' do
24
+ "yay, i haz it."
25
+ end
9
26
  end
10
27
 
11
28
  should "have default host and port" do
@@ -15,21 +32,38 @@ class TestSinatraFakeWebservice < Test::Unit::TestCase
15
32
 
16
33
  context "with a registered GET service" do
17
34
 
18
- setup do
19
- @sinatra_app.get '/payme' do
20
- "OMG I GOT PAID"
21
- end
35
+ should "respond to GET '/payme' with 'OMG I GOT PAID" do
36
+ res = @sinatra_app.get_response('/payme')
37
+ assert_equal "OMG I GOT PAID",res.body
38
+ end
39
+ end
40
+
41
+ context "with a registered POST service" do
42
+
43
+ should "respond to POST '/returnme' with param :return_value with param value" do
44
+ res = @sinatra_app.post_response('/returnme',"return_value=2")
22
45
 
23
- @sinatra_app.run!
46
+ assert_equal "2", res.body
24
47
  end
48
+ end
49
+
50
+ context "with a registered DELETE service" do
25
51
 
26
- should "respond to '/payme' with 'OMG I GOT PAID" do
27
- res = Net::HTTP.start(@sinatra_app.host, @sinatra_app.port) do |http|
28
- http.get('/payme')
29
- end
30
-
31
- assert_equal "OMG I GOT PAID",res.body
52
+ should "respond to DELETE '/killme'" do
53
+ res = @sinatra_app.delete_response('/killme')
54
+
55
+ assert_equal "argh! i is dead.", res.body
32
56
  end
33
57
  end
58
+
59
+ context "with a registered PUT service" do
60
+
61
+ should "respond to put '/gimmieitnow'" do
62
+ res = @sinatra_app.put_response('/gimmieitnow')
63
+
64
+ assert_equal "yay, i haz it.", res.body
65
+ end
66
+ end
67
+
34
68
  end
35
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_fake_webservice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elad Meidar
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-18 00:00:00 -05:00
12
+ date: 2010-03-03 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -48,9 +48,7 @@ files:
48
48
  - README.rdoc
49
49
  - Rakefile
50
50
  - VERSION
51
- - lib/sinatra_ext.rb
52
51
  - lib/sinatra_webservice.rb
53
- - lib/web_service.rb
54
52
  - metal/sinatra_stem.rb
55
53
  - sinatra.log
56
54
  - sinatra_fake_webservice.gemspec
data/lib/sinatra_ext.rb DELETED
@@ -1,16 +0,0 @@
1
- # Disabling the capture of the Errno::EADDRINUSE exception in #run! so we can make WebService catch it.
2
- # class Sinatra::Base
3
- # def self.run!(options={})
4
- # attempts = 0
5
- # set options
6
- # handler = detect_rack_handler
7
- # handler_name = handler.name.gsub(/.*::/, '')
8
- # handler.run self, :Host => bind, :Port => port do |server|
9
- # trap(:INT) do
10
- # ## Use thins' hard #stop! if available, otherwise just #stop
11
- # server.respond_to?(:stop!) ? server.stop! : server.stop
12
- # end
13
- # set :running, true
14
- # end
15
- # end
16
- # end
data/lib/web_service.rb DELETED
@@ -1,60 +0,0 @@
1
- require 'net/http'
2
- class WebService
3
-
4
- class SinatraStem < Sinatra::Base
5
- get '/' do
6
- "Hello! i am lindesy lohan!"
7
- end
8
- end
9
-
10
- attr_accessor :port, :host, :name, :sinatra_app
11
-
12
-
13
- # Initialize a new face service
14
- def initialize(options = {})
15
- @name = options[:name] ||= "App"
16
- @port = options[:port] ||= "4567"
17
- @host = options[:host] ||= "localhost"
18
- @sinatra_app = Sinatra::Base.new
19
- end
20
-
21
- def proxy
22
- @sinatra_app
23
- end
24
-
25
- def run
26
-
27
- @port = find_free_port
28
-
29
- app = Thread.new("running_#{self.name}") do
30
- puts "\n == Starting webservice '#{@name}' on port #{@port}"
31
- proxy.run! :post => @host, :port => @port.to_i
32
- sleep 1
33
- end
34
- end
35
-
36
- def find_free_port
37
- found = false
38
- attempts = 0
39
- while !found and attempts < 10
40
- puts "\n== Trying port #{@port}"
41
- begin
42
- res = Net::HTTP.start(host,port) do |http|
43
- http.get('/')
44
- end
45
- attempts += 1
46
- @port = @port.succ
47
- rescue Errno::ECONNREFUSED
48
- return @port
49
- end
50
- end
51
- end
52
-
53
- def proxy
54
- @sinatra_app
55
- end
56
-
57
- def method_missing(method, *args, &block)
58
- proxy.send(method, *args, &block) unless method.to_s == "proxy"
59
- end
60
- end