sham_rack 1.3.3 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ ## 1-May-2012 [mdub@dogbiscuit.org]
2
+
3
+ * Validate arguments to `ShamRack#mount`.
4
+ * Update for compatibility with patron-0.4.x.
5
+
1
6
  ## 18-Oct-2010 [mdub@dogbiscuit.org]
2
7
 
3
8
  * Add support for Patron.
@@ -29,7 +29,7 @@ Using it
29
29
  require 'sham_rack'
30
30
 
31
31
  ShamRack.at("www.greetings.com") do |env|
32
- ["200 OK", { "Content-type" => "text/plain" }, "Hello, world!"]
32
+ ["200 OK", { "Content-type" => "text/plain" }, ["Hello, world!"]]
33
33
  end
34
34
 
35
35
  require 'open-uri'
@@ -1,9 +1,11 @@
1
+ require "sham_rack/net_http"
2
+ require "sham_rack/registry"
3
+ require "sham_rack/version"
4
+
1
5
  # ShamRack allows access to Rack applications using Net::Http, but without network traffic.
2
6
  #
3
7
  # For more detail, see http://github.com/mdub/sham_rack
4
8
  #
5
- module ShamRack; end
6
-
7
- require "sham_rack/net_http"
8
- require "sham_rack/registry"
9
- require "sham_rack/version"
9
+ module ShamRack
10
+ extend ShamRack::Registry
11
+ end
@@ -1,10 +1,11 @@
1
1
  require "net/http"
2
+ require "rack"
2
3
  require "sham_rack/registry"
3
4
 
4
- class << Net::HTTP
5
+ class << Net::HTTP
5
6
 
6
7
  alias :new_without_sham_rack :new
7
-
8
+
8
9
  def new(address, port = nil, *proxy_args)
9
10
  port ||= Net::HTTP.default_port
10
11
  rack_app = ShamRack.application_for(address, port)
@@ -20,14 +21,14 @@ end
20
21
 
21
22
  module ShamRack
22
23
  module NetHttp
23
-
24
+
24
25
  module Extensions
25
26
 
26
27
  attr_accessor :rack_app
27
28
 
28
29
  def start
29
30
  if block_given?
30
- yield self
31
+ yield self
31
32
  else
32
33
  self
33
34
  end
@@ -47,14 +48,14 @@ module ShamRack
47
48
  rack_env.merge!(header_env(request))
48
49
  rack_env.merge!(server_env)
49
50
  end
50
-
51
+
51
52
  def server_env
52
53
  {
53
- "SERVER_NAME" => @address,
54
+ "SERVER_NAME" => @address,
54
55
  "SERVER_PORT" => @port.to_s
55
56
  }
56
57
  end
57
-
58
+
58
59
  def header_env(request)
59
60
  env = {}
60
61
  request.each_header do |header, content|
@@ -34,13 +34,7 @@ module Patron
34
34
 
35
35
  def patron_response(rack_response)
36
36
  status, headers, body = rack_response
37
- status_code = Rack::Utils::HTTP_STATUS_CODES[status.to_i]
38
- res = Patron::Response.new
39
- res.instance_variable_set(:@status, status)
40
- res.instance_variable_set(:@status_line, "HTTP/1.1 #{status} #{status_code}")
41
- res.instance_variable_set(:@body, assemble_body(body))
42
- res.instance_variable_set(:@headers, headers)
43
- res
37
+ Patron::Response.new("", status.to_i, 0, assemble_headers(status, headers), assemble_body(body))
44
38
  end
45
39
 
46
40
  def header_env(patron_request)
@@ -53,6 +47,15 @@ module Patron
53
47
  env
54
48
  end
55
49
 
50
+ def assemble_headers(status, headers)
51
+ status_code = Rack::Utils::HTTP_STATUS_CODES[status.to_i]
52
+ content = "HTTP/1.1 #{status} #{status_code}\r\n"
53
+ headers.each do |k, v|
54
+ content << "#{k}: #{v}\r\n"
55
+ end
56
+ content
57
+ end
58
+
56
59
  def assemble_body(body)
57
60
  content = ""
58
61
  body.each { |fragment| content << fragment }
@@ -61,4 +64,4 @@ module Patron
61
64
 
62
65
  end
63
66
 
64
- end
67
+ end
@@ -1,12 +1,21 @@
1
1
  module ShamRack
2
2
 
3
3
  module Registry
4
-
4
+
5
+ ADDRESS_PATTERN = /^[a-z0-9-]+(\.[a-z0-9-]+)*$/i
6
+
5
7
  def mount(rack_app, address, port = nil)
6
- port ||= Net::HTTP.default_port
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
7
16
  registry[[address, port]] = rack_app
8
17
  end
9
-
18
+
10
19
  def unmount_all
11
20
  registry.clear
12
21
  end
@@ -18,24 +27,22 @@ module ShamRack
18
27
  Registrar.new(address, port)
19
28
  end
20
29
  end
21
-
30
+
22
31
  def application_for(address, port = nil)
23
32
  port ||= Net::HTTP.default_port
24
33
  registry[[address, port]]
25
34
  end
26
35
 
27
36
  private
28
-
37
+
29
38
  def registry
30
39
  @registry ||= {}
31
40
  end
32
-
41
+
33
42
  end
34
-
35
- extend Registry
36
43
 
37
44
  class Registrar
38
-
45
+
39
46
  def initialize(address, port = nil)
40
47
  @address = address
41
48
  @port = port
@@ -46,7 +53,7 @@ module ShamRack
46
53
  app = Rack::Builder.new(&block).to_app
47
54
  ShamRack.mount(app, @address, @port)
48
55
  end
49
-
56
+
50
57
  def sinatra(&block)
51
58
  require "sinatra/base"
52
59
  sinatra_app = Class.new(Sinatra::Base)
@@ -58,7 +65,7 @@ module ShamRack
58
65
  require "sham_rack/stub_web_service"
59
66
  ShamRack.mount(StubWebService.new, @address, @port)
60
67
  end
61
-
68
+
62
69
  end
63
70
 
64
71
  end
@@ -1,3 +1,3 @@
1
1
  module ShamRack
2
- VERSION = "1.3.3"
2
+ VERSION = "1.3.4"
3
3
  end
@@ -56,6 +56,20 @@ describe ShamRack do
56
56
 
57
57
  end
58
58
 
59
+ describe ".mount" do
60
+
61
+ context "with a URL" do
62
+
63
+ it "raises an ArgumentError" do
64
+ lambda do
65
+ ShamRack.mount(GreetingApp.new, "http://www.greetings.com")
66
+ end.should raise_error(ArgumentError, "invalid address")
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
59
73
  describe ".at" do
60
74
 
61
75
  describe "with a block" do
@@ -100,7 +114,7 @@ describe ShamRack do
100
114
  end
101
115
  end
102
116
  end
103
-
117
+
104
118
  it "mounts associated block as a Sinatra app" do
105
119
  open("http://sinatra.xyz/hello/stranger").read.should == "Hello, stranger"
106
120
  end
@@ -108,11 +122,11 @@ describe ShamRack do
108
122
  it "returns the app" do
109
123
  @return_value.should respond_to(:call)
110
124
  end
111
-
125
+
112
126
  end
113
127
 
114
128
  describe "#stub" do
115
-
129
+
116
130
  before do
117
131
  @return_value = ShamRack.at("stubbed.xyz").stub
118
132
  end
@@ -120,28 +134,28 @@ describe ShamRack do
120
134
  it "mounts a StubWebService" do
121
135
  ShamRack.application_for("stubbed.xyz").should be_kind_of(ShamRack::StubWebService)
122
136
  end
123
-
137
+
124
138
  it "returns the StubWebService" do
125
139
  @return_value.should == ShamRack.application_for("stubbed.xyz")
126
140
  end
127
-
141
+
128
142
  end
129
-
143
+
130
144
  end
131
145
 
132
146
  describe "response" do
133
-
147
+
134
148
  before(:each) do
135
149
  ShamRack.at("www.greetings.com") do
136
150
  [
137
- "201 Created",
151
+ "201 Created",
138
152
  { "Content-Type" => "text/plain", "X-Foo" => "bar" },
139
153
  ["BODY"]
140
154
  ]
141
155
  end
142
156
  @response = Net::HTTP.get_response(URI.parse("http://www.greetings.com/"))
143
157
  end
144
-
158
+
145
159
  it "has status returned by app" do
146
160
  @response.code.should == "201"
147
161
  end
@@ -149,17 +163,17 @@ describe ShamRack do
149
163
  it "has body returned by app" do
150
164
  @response.body.should == "BODY"
151
165
  end
152
-
166
+
153
167
  it "has Content-Type returned by app" do
154
168
  @response.content_type.should == "text/plain"
155
169
  end
156
-
170
+
157
171
  it "has other headers returned by app" do
158
172
  @response["x-foo"].should =="bar"
159
173
  end
160
-
174
+
161
175
  end
162
-
176
+
163
177
  describe "Rack environment" do
164
178
 
165
179
  before(:each) do
@@ -232,8 +246,8 @@ describe ShamRack do
232
246
  patron = Patron::Session.new
233
247
  response = patron.post("http://env.xyz/resource", "<xml/>", "Content-Type" => "application/xml")
234
248
 
235
- response.status.should == "200 OK"
236
-
249
+ response.status.should == 200
250
+
237
251
  env["REQUEST_METHOD"].should == "POST"
238
252
  env["rack.input"].read.should == "<xml/>"
239
253
  env["CONTENT_TYPE"].should == "application/xml"
metadata CHANGED
@@ -1,46 +1,39 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sham_rack
3
- version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 3
9
- - 3
10
- version: 1.3.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.4
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Mike Williams
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-12-22 00:00:00 +11:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rack
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :runtime
34
- version_requirements: *id001
35
- description: ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP testing.
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP
31
+ testing.
36
32
  email: mdub@dogbiscuit.org
37
33
  executables: []
38
-
39
34
  extensions: []
40
-
41
35
  extra_rdoc_files: []
42
-
43
- files:
36
+ files:
44
37
  - lib/sham_rack/net_http.rb
45
38
  - lib/sham_rack/patron.rb
46
39
  - lib/sham_rack/registry.rb
@@ -56,41 +49,37 @@ files:
56
49
  - Rakefile
57
50
  - benchmark/benchmark.rb
58
51
  - benchmark/hello_app.rb
59
- has_rdoc: true
60
52
  homepage: http://github.com/mdub/sham_rack
61
53
  licenses: []
62
-
63
54
  post_install_message:
64
55
  rdoc_options: []
65
-
66
- require_paths:
56
+ require_paths:
67
57
  - lib
68
- required_ruby_version: !ruby/object:Gem::Requirement
58
+ required_ruby_version: !ruby/object:Gem::Requirement
69
59
  none: false
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- hash: 3
74
- segments:
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ segments:
75
65
  - 0
76
- version: "0"
77
- required_rubygems_version: !ruby/object:Gem::Requirement
66
+ hash: -1856658297304903275
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
68
  none: false
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- hash: 3
83
- segments:
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ segments:
84
74
  - 0
85
- version: "0"
75
+ hash: -1856658297304903275
86
76
  requirements: []
87
-
88
77
  rubyforge_project: shamrack
89
- rubygems_version: 1.3.7
78
+ rubygems_version: 1.8.21
90
79
  signing_key:
91
80
  specification_version: 3
92
81
  summary: Net::HTTP-to-Rack plumbing
93
- test_files:
82
+ test_files:
94
83
  - spec/sham_rack/stub_web_service_spec.rb
95
84
  - spec/sham_rack_spec.rb
96
85
  - spec/spec_helper.rb