mdub-sham_rack 1.0.0 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ pkg
2
+ rdoc
@@ -0,0 +1,13 @@
1
+ ## 5-Jun-2009 [mdub@dogbiscuit.org]
2
+
3
+ * Add support for Net::HTTP.get_response.
4
+ * Pass back headers provided by Rack app in the HTTPResponse.
5
+
6
+ ## 3-Jun-2009 [mdub@dogbiscuit.org]
7
+
8
+ * Introduced ShamRack#at to simplify registration of apps.
9
+
10
+ ## 13-May-2009 [mdub@dogbiscuit.org]
11
+
12
+ * Added accessors on HTTP object for address, port and rack_app.
13
+ * Added accessors to imitate "net/https".
@@ -1,14 +1,14 @@
1
1
  ShamRack
2
2
  ========
3
3
 
4
- ShamRack plumbs Net:HTTP into [Rack][rack], providing infrastructure for testing arbitrary Rack-based apps using arbitrary HTTP clients, all from the comfort of your own Ruby VM.
4
+ ShamRack plumbs Net:HTTP into [Rack][rack].
5
5
 
6
6
  What's it for, again?
7
7
  ---------------------
8
8
 
9
- Well, you can _test your HTTP client code_, using ShamRack to fake out an external web-service ([Sinatra][sinatra] helps, here).
9
+ Well, you can _test your HTTP client code_, using ShamRack to stub out an external web-service. Think of it as [FakeWeb][fakeweb] on steriods.
10
10
 
11
- Or, you can `ShamRack.mount` your actual Sinatra/Rails/Merb/Rack app, which is handy if you want to _test access using an HTTP client library_ such as:
11
+ Or, you can _test your Rack application_ (or Sinatra, or Rails, or Merb) using arbitrary HTTP client libraries, to check interoperability. For instance, you could hit a local app using:
12
12
 
13
13
  * [`rest-client`][rest-client]
14
14
  * [`httparty`][httparty]
@@ -23,17 +23,20 @@ Installing it
23
23
  Using it
24
24
  --------
25
25
 
26
+ ### A simple inline application
27
+
26
28
  require 'sham_rack'
27
-
28
- rack_app = lambda { |env| ["200 OK", { "Content-type" => "text/plain" }, "Hello, world!"] }
29
- ShamRack.mount(rack_app, "www.example.com")
29
+
30
+ ShamRack.at("www.example.com") do |env|
31
+ ["200 OK", { "Content-type" => "text/plain" }, "Hello, world!"]
32
+ end
30
33
 
31
34
  require 'open-uri'
32
35
  open("http://www.example.com/").read #=> "Hello, world!"
33
36
 
34
37
  ### Sinatra integration
35
38
 
36
- ShamRack.sinatra("sinatra.xyz") do
39
+ ShamRack.at("sinatra.xyz").sinatra do
37
40
  get "/hello/:subject" do
38
41
  "Hello, #{params[:subject]}"
39
42
  end
@@ -43,12 +46,16 @@ Using it
43
46
 
44
47
  ### Rackup support
45
48
 
46
- ShamRack.rackup("rackup.xyz") do
49
+ ShamRack.at("rackup.xyz").rackup do
47
50
  use Some::Middleware
48
51
  use Some::Other::Middleware
49
52
  run MyApp.new
50
53
  end
51
54
 
55
+ ### Any old app
56
+
57
+ ShamRack.mount(my_google_stub, "google.com")
58
+
52
59
  What's the catch?
53
60
  -----------------
54
61
 
@@ -59,6 +66,7 @@ Thanks to
59
66
  ---------
60
67
 
61
68
  * Blaine Cook for [FakeWeb][fakeweb], which was an inspiration for ShamRack.
69
+ * Perryn Fowler for his efforts plumbing Net::HTTP into ActionController::TestProcess.
62
70
  * Christian Neukirchen et al for the chewy goodness that is [Rack][rack].
63
71
 
64
72
  [rack]: http://rack.rubyforge.org/
data/Rakefile CHANGED
@@ -1,3 +1,6 @@
1
+ require "rubygems"
2
+ require "rake"
3
+
1
4
  require "spec/rake/spectask"
2
5
 
3
6
  task "default" => "spec"
@@ -7,16 +10,31 @@ Spec::Rake::SpecTask.new do |t|
7
10
  t.spec_files = FileList['spec/**/*_spec.rb']
8
11
  end
9
12
 
10
- begin
11
- require 'jeweler'
12
- Jeweler::Tasks.new do |gemspec|
13
- gemspec.name = "sham_rack"
14
- gemspec.summary = "Net::HTTP-to-Rack plumbing"
15
- gemspec.email = "mdub@dogbiscuit.org"
16
- gemspec.homepage = "http://github.com/mdub/sham_rack"
17
- gemspec.description = "ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP testing."
18
- gemspec.authors = ["Mike Williams"]
13
+ require "jeweler"
14
+
15
+ Jeweler::Tasks.new do |g|
16
+ g.name = "sham_rack"
17
+ g.summary = "Net::HTTP-to-Rack plumbing"
18
+ g.email = "mdub@dogbiscuit.org"
19
+ g.homepage = "http://github.com/mdub/sham_rack"
20
+ g.description = "ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP testing."
21
+ g.authors = ["Mike Williams"]
22
+ g.rubyforge_project = "shamrack"
23
+ end
24
+
25
+ Jeweler::RubyforgeTasks.new
26
+
27
+ require 'rake/rdoctask'
28
+ Rake::RDocTask.new do |rdoc|
29
+ if File.exist?('VERSION.yml')
30
+ config = YAML.load(File.read('VERSION.yml'))
31
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
32
+ else
33
+ version = ""
19
34
  end
20
- rescue LoadError
21
- puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
35
+
36
+ rdoc.rdoc_dir = 'rdoc'
37
+ rdoc.title = "ShamRack #{version}"
38
+ rdoc.main = "ShamRack"
39
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
40
  end
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 0
3
2
  :major: 1
4
- :minor: 0
3
+ :minor: 1
4
+ :patch: 2
@@ -1 +1,7 @@
1
+ # ShamRack allows access to Rack applications using Net::Http, but without network traffic.
2
+ #
3
+ # For more detail, see http://github.com/mdub/sham_rack
4
+ #
5
+ module ShamRack; end
6
+
1
7
  require "sham_rack/core_ext/net/http"
@@ -8,13 +8,16 @@ module ShamRack
8
8
  @port = port
9
9
  @rack_app = rack_app
10
10
  end
11
+
12
+ attr_reader :address, :port, :rack_app
11
13
 
14
+ attr_accessor :read_timeout, :open_timeout
15
+ attr_accessor :use_ssl,:key, :cert, :ca_file, :ca_path, :verify_mode, :verify_callback, :verify_depth, :cert_store
16
+
12
17
  def start
13
18
  yield self
14
19
  end
15
-
16
- attr_accessor :use_ssl, :verify_mode, :read_timeout, :open_timeout
17
-
20
+
18
21
  def request(req, body = nil)
19
22
  env = default_env
20
23
  env.merge!(path_env(req.path))
@@ -26,6 +29,22 @@ module ShamRack
26
29
  return response
27
30
  end
28
31
 
32
+ def request_get(path, initheader = nil, &block)
33
+ request Net::HTTP::Get.new(path, initheader), &block
34
+ end
35
+
36
+ def request_head(path, initheader = nil, &block)
37
+ request Net::HTTP::Head.new(path, initheader), &block
38
+ end
39
+
40
+ def request_post(path, data, initheader = nil, &block)
41
+ request Net::HTTP::Post.new(path, initheader), data, &block
42
+ end
43
+
44
+ def request_put(path, data, initheader = nil, &block)
45
+ request Net::HTTP::Put.new(path, initheader), data, &block
46
+ end
47
+
29
48
  private
30
49
 
31
50
  def default_env
@@ -81,6 +100,9 @@ module ShamRack
81
100
  response = Net::HTTPResponse.send(:response_class, code).new("Sham", code, message)
82
101
  response.instance_variable_set(:@body, assemble_body(body))
83
102
  response.instance_variable_set(:@read, true)
103
+ headers.each do |k,v|
104
+ response.add_field(k, v)
105
+ end
84
106
  response.extend ShamRack::ResponseExtensions
85
107
  return response
86
108
  end
@@ -7,25 +7,17 @@ module ShamRack
7
7
  registry[[address, port]] = rack_app
8
8
  end
9
9
 
10
- def rackup(address, port = nil, &block)
11
- app = Rack::Builder.new(&block).to_app
12
- mount(app, address, port)
13
- end
14
-
15
- def lambda(address, port = nil, &block)
16
- mount(block, address, port)
17
- end
18
-
19
- def sinatra(address, port = nil, &block)
20
- require "sinatra/base"
21
- sinatra_app = Class.new(Sinatra::Base)
22
- sinatra_app.class_eval(&block)
23
- mount(sinatra_app.new, address, port)
24
- end
25
-
26
10
  def unmount_all
27
11
  registry.clear
28
12
  end
13
+
14
+ def at(address, port = nil, &block)
15
+ if block
16
+ mount(block, address, port)
17
+ else
18
+ Registrar.new(address, port)
19
+ end
20
+ end
29
21
 
30
22
  def application_for(address, port = nil)
31
23
  port ||= Net::HTTP.default_port
@@ -42,4 +34,26 @@ module ShamRack
42
34
 
43
35
  extend Registry
44
36
 
37
+ class Registrar
38
+
39
+ def initialize(address, port = nil)
40
+ @address = address
41
+ @port = port
42
+ end
43
+
44
+ def rackup(&block)
45
+ require "rack"
46
+ app = Rack::Builder.new(&block).to_app
47
+ ShamRack.mount(app, @address, @port)
48
+ end
49
+
50
+ def sinatra(&block)
51
+ require "sinatra/base"
52
+ sinatra_app = Class.new(Sinatra::Base)
53
+ sinatra_app.class_eval(&block)
54
+ ShamRack.mount(sinatra_app.new, @address, @port)
55
+ end
56
+
57
+ end
58
+
45
59
  end
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{sham_rack}
5
+ s.version = "1.1.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Mike Williams"]
9
+ s.date = %q{2009-06-05}
10
+ s.description = %q{ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP testing.}
11
+ s.email = %q{mdub@dogbiscuit.org}
12
+ s.extra_rdoc_files = [
13
+ "README.markdown"
14
+ ]
15
+ s.files = [
16
+ ".gitignore",
17
+ "CHANGES.markdown",
18
+ "README.markdown",
19
+ "Rakefile",
20
+ "VERSION.yml",
21
+ "lib/sham_rack.rb",
22
+ "lib/sham_rack/core_ext/net/http.rb",
23
+ "lib/sham_rack/http.rb",
24
+ "lib/sham_rack/registry.rb",
25
+ "sham_rack.gemspec",
26
+ "spec/sham_rack_spec.rb",
27
+ "spec/spec_helper.rb"
28
+ ]
29
+ s.has_rdoc = true
30
+ s.homepage = %q{http://github.com/mdub/sham_rack}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubyforge_project = %q{shamrack}
34
+ s.rubygems_version = %q{1.3.2}
35
+ s.summary = %q{Net::HTTP-to-Rack plumbing}
36
+ s.test_files = [
37
+ "spec/sham_rack_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
@@ -75,6 +75,11 @@ describe ShamRack do
75
75
  response.body.should == "Hello, world"
76
76
  end
77
77
 
78
+ it "can be accessed using Net::HTTP#get_response" do
79
+ response = Net::HTTP.get_response(URI.parse("http://www.test.xyz/"))
80
+ response.body.should == "Hello, world"
81
+ end
82
+
78
83
  it "can be accessed using open-uri" do
79
84
  response = open("http://www.test.xyz")
80
85
  response.status.should == ["200", "OK"]
@@ -89,47 +94,90 @@ describe ShamRack do
89
94
 
90
95
  end
91
96
 
92
- describe "#rackup" do
93
-
94
- it "mounts an app created using Rack::Builder" do
95
-
96
- ShamRack.rackup("rackup.xyz") do
97
- use UpcaseBody
98
- run SimpleMessageApp.new("Racked!")
97
+ describe "response" do
98
+
99
+ before(:each) do
100
+ ShamRack.at("www.test.xyz") do
101
+ [
102
+ "201 Created",
103
+ { "Content-Type" => "text/plain", "X-Foo" => "bar" },
104
+ "BODY"
105
+ ]
99
106
  end
100
-
101
- open("http://rackup.xyz").read.should == "RACKED!"
102
-
107
+ @response = Net::HTTP.get_response(URI.parse("http://www.test.xyz/"))
108
+ end
109
+
110
+ it "has status returned by app" do
111
+ @response.code.should == "201"
103
112
  end
104
113
 
114
+ it "has body returned by app" do
115
+ @response.body.should == "BODY"
116
+ end
117
+
118
+ it "has Content-Type returned by app" do
119
+ @response.content_type.should == "text/plain"
120
+ end
121
+
122
+ it "has other headers returned by app" do
123
+ @response["x-foo"].should =="bar"
124
+ end
125
+
105
126
  end
127
+
128
+ describe "#at" do
106
129
 
107
- describe "#lambda" do
130
+ describe "with a block" do
108
131
 
109
- it "mounts associated block as an app" do
132
+ it "mounts associated block as an app" do
110
133
 
111
- ShamRack.lambda("simple.xyz") do |env|
112
- ["200 OK", { "Content-type" => "text/plain" }, "Easy, huh?"]
113
- end
134
+ ShamRack.at("simple.xyz") do |env|
135
+ ["200 OK", { "Content-type" => "text/plain" }, "Easy, huh?"]
136
+ end
114
137
 
115
- open("http://simple.xyz").read.should == "Easy, huh?"
138
+ open("http://simple.xyz").read.should == "Easy, huh?"
139
+
140
+ end
116
141
 
117
142
  end
118
143
 
119
- end
144
+ describe "#rackup" do
120
145
 
121
- describe "#sinatra" do
146
+ before do
147
+ @return_value = ShamRack.at("rackup.xyz").rackup do
148
+ use UpcaseBody
149
+ run SimpleMessageApp.new("Racked!")
150
+ end
151
+ end
122
152
 
123
- it "mounts associated block as a Sinatra app" do
153
+ it "mounts an app created using Rack::Builder" do
154
+ open("http://rackup.xyz").read.should == "RACKED!"
155
+ end
124
156
 
125
- ShamRack.sinatra("sinatra.xyz") do
126
- get "/hello/:subject" do
127
- "Hello, #{params[:subject]}"
128
- end
157
+ it "returns the app" do
158
+ @return_value.should respond_to(:call)
129
159
  end
130
160
 
131
- open("http://sinatra.xyz/hello/stranger").read.should == "Hello, stranger"
161
+ end
162
+
163
+ describe "#sinatra" do
132
164
 
165
+ before do
166
+ @return_value = ShamRack.at("sinatra.xyz").sinatra do
167
+ get "/hello/:subject" do
168
+ "Hello, #{params[:subject]}"
169
+ end
170
+ end
171
+ end
172
+
173
+ it "mounts associated block as a Sinatra app" do
174
+ open("http://sinatra.xyz/hello/stranger").read.should == "Hello, stranger"
175
+ end
176
+
177
+ it "returns the app" do
178
+ @return_value.should respond_to(:call)
179
+ end
180
+
133
181
  end
134
182
 
135
183
  end
@@ -138,7 +186,7 @@ describe ShamRack do
138
186
 
139
187
  before(:each) do
140
188
  @env_recorder = recorder = EnvRecordingApp.new
141
- ShamRack.rackup("env.xyz") do
189
+ ShamRack.at("env.xyz").rackup do
142
190
  use Rack::Lint
143
191
  run recorder
144
192
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdub-sham_rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-06 00:00:00 -07:00
12
+ date: 2009-06-05 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,6 +22,8 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - README.markdown
24
24
  files:
25
+ - .gitignore
26
+ - CHANGES.markdown
25
27
  - README.markdown
26
28
  - Rakefile
27
29
  - VERSION.yml
@@ -29,6 +31,7 @@ files:
29
31
  - lib/sham_rack/core_ext/net/http.rb
30
32
  - lib/sham_rack/http.rb
31
33
  - lib/sham_rack/registry.rb
34
+ - sham_rack.gemspec
32
35
  - spec/sham_rack_spec.rb
33
36
  - spec/spec_helper.rb
34
37
  has_rdoc: true
@@ -52,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
55
  version:
53
56
  requirements: []
54
57
 
55
- rubyforge_project:
58
+ rubyforge_project: shamrack
56
59
  rubygems_version: 1.2.0
57
60
  signing_key:
58
61
  specification_version: 3