sham_rack 1.3.5 → 1.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGUzNjMxMzE4N2M3NDA3ZWY2ZDQ5NWIzZDg2Mjg2ZjJhMmNiYWQxMA==
4
+ ZmU4NWE2MGVhYjA0ZDE5ODM1NjJiYTlhMzE1MTg5MjMyMTQ4YmExNg==
5
5
  data.tar.gz: !binary |-
6
- M2I4NDc5Njg5YTE0MzAwYjczYzZkYjA0ZWQ5OTExYjYxNTE5YzM5MQ==
6
+ ZmEwZDliNjk4MzIxNjdkYWNlZTQzY2ZhOWU2ZDdjMzg1NmZiODk4Yw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZGJmZTUwOGIwMDhkY2Q4MTY3NmFkMWNkYjk2MmNiMWRjMGZmZDk2MWQ3YWFj
10
- YzZlNzEwYzFhYmQ0M2NlM2MxYjViMzIwYTAwZWI4N2FlZjc3NjM3MjcwYzRi
11
- MjJiYTEyMWRhOTNiY2IyYzVlOGIyOTg1ZmJjMDg0NmZmY2RmNWU=
9
+ NDI2MTVkYzU3OWI2YTgwYzFkMWEzYzg0NWJlOTM3YzU4NjhmYzk0Mzc1NTUy
10
+ ZGNhMTY2OGQ1ODA2ODExY2U3OTE3M2VjZjI3MjE0ZjllZDQ5YTE4OGQyNDY3
11
+ YTEwZTM1OWRjZGZiZjMyM2U2NmVkYjFmN2I2NTA4NzNlNTUxOTc=
12
12
  data.tar.gz: !binary |-
13
- ZGQxNGYxNmFjNjViZTQzZThmOWQ5MWI4YTExZTg5N2YzMGJlMTcwZjViNTEy
14
- ZDVhNGQ2Y2QyODcyZTk4YThmMmEwNzAxNzM2ZmU4NzIxM2U3Yzg5ZDIzMTY4
15
- MTA2NmI1NDkyNTAyZmJjYzgzMjg5MGJkNmQyMGQwYTg0YWZlYzE=
13
+ ZmEyODY4YTVlNGRkNTNmMjE4ODNhZjlhNDUwODU0MjI3ZDFmZGYyYTcwNGJk
14
+ M2I2NTk0OGU1MDA2NTJiYWUyODczZGZjYTQ2ZjY2YjRmOGYwZDMzZmM4OTU1
15
+ MDljNjA2OTQ5NWFmNzQxMzAzZWYzZWEzZGZhZDMwZDA1NGI2ZGY=
@@ -53,9 +53,9 @@ Using it
53
53
  run MyApp.new
54
54
  end
55
55
 
56
- ### Any old app
56
+ ### Any old Rack app
57
57
 
58
- ShamRack.mount(my_google_stub, "google.com")
58
+ ShamRack.at("google.com").mount(my_google_stub)
59
59
 
60
60
  ### General-purpose stubbing
61
61
 
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ task "default" => "spec"
6
6
  require "rspec/core/rake_task"
7
7
 
8
8
  RSpec::Core::RakeTask.new do |t|
9
- t.rspec_opts = ["--colour", "--format", "nested"]
9
+ t.rspec_opts = ["--format", "nested"]
10
10
  end
11
11
 
12
12
  require 'bundler'
@@ -1,5 +1,5 @@
1
1
  require "sham_rack/net_http"
2
- require "sham_rack/registry"
2
+ require "sham_rack/registration"
3
3
  require "sham_rack/version"
4
4
 
5
5
  # ShamRack allows access to Rack applications using Net::Http, but without network traffic.
@@ -7,5 +7,5 @@ require "sham_rack/version"
7
7
  # For more detail, see http://github.com/mdub/sham_rack
8
8
  #
9
9
  module ShamRack
10
- extend ShamRack::Registry
10
+ extend ShamRack::Registration
11
11
  end
@@ -1,6 +1,5 @@
1
1
  require "net/http"
2
2
  require "rack"
3
- require "sham_rack/registry"
4
3
 
5
4
  class << Net::HTTP
6
5
 
@@ -1,5 +1,5 @@
1
1
  require "patron"
2
- require "sham_rack/registry"
2
+ require "sham_rack"
3
3
  require "uri"
4
4
 
5
5
  module Patron
@@ -0,0 +1,82 @@
1
+ module ShamRack
2
+
3
+ module Registration
4
+
5
+ ADDRESS_PATTERN = /^[a-z0-9-]+(\.[a-z0-9-]+)*$/i
6
+
7
+ def unmount_all
8
+ registry.clear
9
+ end
10
+
11
+ def at(address, port = nil, &app_block)
12
+ mount_point = mount_point_for(address, port)
13
+ if app_block
14
+ mount_point.mount(app_block)
15
+ else
16
+ mount_point
17
+ end
18
+ end
19
+
20
+ def application_for(address, port = nil)
21
+ mount_point_for(address, port).app
22
+ end
23
+
24
+ def mount(app, address, port = nil)
25
+ at(address, port).mount(app)
26
+ end
27
+
28
+ private
29
+
30
+ def mount_point_for(address, port)
31
+ registry[mount_key(address, port)]
32
+ end
33
+
34
+ def registry
35
+ @registry ||= Hash.new do |hash, key|
36
+ hash[key] = MountPoint.new
37
+ end
38
+ end
39
+
40
+ def mount_key(address, port)
41
+ unless address =~ ADDRESS_PATTERN
42
+ raise ArgumentError, "invalid address"
43
+ end
44
+ port ||= Net::HTTP.default_port
45
+ port = Integer(port)
46
+ [address, port]
47
+ end
48
+
49
+ end
50
+
51
+ class MountPoint
52
+
53
+ attr_reader :app
54
+
55
+ def mount(app)
56
+ @app = app
57
+ end
58
+
59
+ def unmount
60
+ @app = nil
61
+ end
62
+
63
+ def rackup(&block)
64
+ require "rack"
65
+ mount(Rack::Builder.new(&block).to_app)
66
+ end
67
+
68
+ def sinatra(&block)
69
+ require "sinatra/base"
70
+ sinatra_app = Class.new(Sinatra::Base)
71
+ sinatra_app.class_eval(&block)
72
+ mount(sinatra_app.new)
73
+ end
74
+
75
+ def stub
76
+ require "sham_rack/stub_web_service"
77
+ mount(StubWebService.new)
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -1,3 +1,3 @@
1
1
  module ShamRack
2
- VERSION = "1.3.5"
2
+ VERSION = "1.3.6"
3
3
  end
@@ -9,6 +9,16 @@ require "rack"
9
9
 
10
10
  describe ShamRack do
11
11
 
12
+ class NetHttpProhibited < StandardError; end
13
+
14
+ before do
15
+ any_instance_of(Net::HTTP) do |http|
16
+ stub(http).start do
17
+ raise NetHttpProhibited, "real network calls are not allowed"
18
+ end
19
+ end
20
+ end
21
+
12
22
  after(:each) do
13
23
  ShamRack.unmount_all
14
24
  end
@@ -56,31 +66,54 @@ describe ShamRack do
56
66
 
57
67
  end
58
68
 
59
- describe ".mount" do
69
+ describe ".at" do
70
+
71
+ context "with a block" do
72
+
73
+ it "mounts associated block as an app" do
74
+
75
+ ShamRack.at("simple.xyz") do |env|
76
+ ["200 OK", { "Content-type" => "text/plain" }, ["Easy, huh?"]]
77
+ end
78
+
79
+ open("http://simple.xyz").read.should == "Easy, huh?"
80
+
81
+ end
82
+
83
+ end
60
84
 
61
85
  context "with a URL" do
62
86
 
63
87
  it "raises an ArgumentError" do
64
88
  lambda do
65
- ShamRack.mount(GreetingApp.new, "http://www.greetings.com")
89
+ ShamRack.at("http://www.greetings.com")
66
90
  end.should raise_error(ArgumentError, "invalid address")
67
91
  end
68
92
 
69
93
  end
70
94
 
71
- end
95
+ describe "#mount" do
72
96
 
73
- describe ".at" do
97
+ it "mounts an app" do
74
98
 
75
- describe "with a block" do
99
+ ShamRack.at("hello.xyz").mount(GreetingApp.new)
76
100
 
77
- it "mounts associated block as an app" do
101
+ open("http://hello.xyz").read.should == "Hello, world"
78
102
 
79
- ShamRack.at("simple.xyz") do |env|
80
- ["200 OK", { "Content-type" => "text/plain" }, ["Easy, huh?"]]
81
- end
103
+ end
82
104
 
83
- open("http://simple.xyz").read.should == "Easy, huh?"
105
+ end
106
+
107
+ describe "#unmount" do
108
+
109
+ it "deregisters a mounted app" do
110
+
111
+ ShamRack.at("gone.xyz").mount(GreetingApp.new)
112
+ ShamRack.at("gone.xyz").unmount
113
+
114
+ lambda do
115
+ open("http://gone.xyz").read
116
+ end.should raise_error(NetHttpProhibited)
84
117
 
85
118
  end
86
119
 
@@ -143,6 +176,18 @@ describe ShamRack do
143
176
 
144
177
  end
145
178
 
179
+ describe ".mount" do
180
+
181
+ it "is deprecated, but still works" do
182
+
183
+ ShamRack.mount(GreetingApp.new, "hello.xyz")
184
+
185
+ open("http://hello.xyz").read.should == "Hello, world"
186
+
187
+ end
188
+
189
+ end
190
+
146
191
  describe "response" do
147
192
 
148
193
  before(:each) do
@@ -3,7 +3,7 @@ require "rspec"
3
3
  require "rr"
4
4
 
5
5
  RSpec.configure do |config|
6
- config.mock_with :rr
6
+ config.mock_with RR::Adapters::RSpec2
7
7
  end
8
8
 
9
9
  require "test_apps"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sham_rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.5
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-25 00:00:00.000000000 Z
11
+ date: 2013-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  prerelease: false
@@ -33,7 +33,7 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - lib/sham_rack/net_http.rb
35
35
  - lib/sham_rack/patron.rb
36
- - lib/sham_rack/registry.rb
36
+ - lib/sham_rack/registration.rb
37
37
  - lib/sham_rack/stub_web_service.rb
38
38
  - lib/sham_rack/version.rb
39
39
  - lib/sham_rack.rb
@@ -1,71 +0,0 @@
1
- module ShamRack
2
-
3
- module Registry
4
-
5
- ADDRESS_PATTERN = /^[a-z0-9-]+(\.[a-z0-9-]+)*$/i
6
-
7
- def mount(rack_app, address, port = nil)
8
- unless address =~ ADDRESS_PATTERN
9
- raise ArgumentError, "invalid address"
10
- end
11
- if port.nil?
12
- port = Net::HTTP.default_port
13
- else
14
- port = Integer(port)
15
- end
16
- registry[[address, port]] = rack_app
17
- end
18
-
19
- def unmount_all
20
- registry.clear
21
- end
22
-
23
- def at(address, port = nil, &block)
24
- if block
25
- mount(block, address, port)
26
- else
27
- Registrar.new(address, port)
28
- end
29
- end
30
-
31
- def application_for(address, port = nil)
32
- port ||= Net::HTTP.default_port
33
- registry[[address, port]]
34
- end
35
-
36
- private
37
-
38
- def registry
39
- @registry ||= {}
40
- end
41
-
42
- end
43
-
44
- class Registrar
45
-
46
- def initialize(address, port = nil)
47
- @address = address
48
- @port = port
49
- end
50
-
51
- def rackup(&block)
52
- require "rack"
53
- app = Rack::Builder.new(&block).to_app
54
- ShamRack.mount(app, @address, @port)
55
- end
56
-
57
- def sinatra(&block)
58
- require "sinatra/base"
59
- sinatra_app = Class.new(Sinatra::Base)
60
- sinatra_app.class_eval(&block)
61
- ShamRack.mount(sinatra_app.new, @address, @port)
62
- end
63
-
64
- def stub
65
- require "sham_rack/stub_web_service"
66
- ShamRack.mount(StubWebService.new, @address, @port)
67
- end
68
-
69
- end
70
-
71
- end