request-replay 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 757f765cde5cb73d14e26de907f2bc03322973ee
4
- data.tar.gz: 0fa338334b76b3d678a2e676a3fe86e19a1d5cff
3
+ metadata.gz: 82468210714a1dd60ceadcd91e95752c0e85ce85
4
+ data.tar.gz: 906d7755d38d5a0748707068bc1f31d7c34beadb
5
5
  SHA512:
6
- metadata.gz: 3c9a20be6d4bb76253ddebefd6aa54176564d7909b8508e1e7bd02fada6d0e9a7c986f6a678bde351276af31a34c0f4d4a1ab63ada383990369a33062789d19f
7
- data.tar.gz: 739d4d94923788d1e27f1f12539c03c7182e1679ed601164e39d655bb2ab147a8061aef27c85ca0fbad557a3ed9af8a54df6d7a0bfe88dc84c93f2fe8a4dabd0
6
+ metadata.gz: cf7b7b776083558e322f502a0de20cccf7dc51e4cd14f0be2a8eddfd8316fe3748add3a749ac336f50567e6c33755da185b8a2bd52618d2ef706a521f9f691e3
7
+ data.tar.gz: f61d88e1673fb50cc512e87b8702dc2ddeb51bf6e7976a90ecf3982033c1f243d477299eb5fe1b4db6dc199da29f773091af90dcaf39262d367cf1eabd5cc17b
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGES
2
2
 
3
+ ## request-replay 0.6.2 -- 2013-10-01
4
+
5
+ * Added :rewrite_env option for rewriting env for specific use.
6
+
3
7
  ## request-replay 0.6.1 -- 2013-10-01
4
8
 
5
9
  * Print error messages to env['rack.errors']
data/README.md CHANGED
@@ -31,28 +31,15 @@ overwrite in the original request.
31
31
  ``` ruby
32
32
  require 'request-replay'
33
33
  use RequestReplay::Middleware, 'localhost:8080',
34
- :add_headers => {'Host' => 'example.com'},
35
- :read_wait => 5
36
- run lambda{ |env| [200, {}, [env.inspect]] }
37
- ```
38
-
39
- It's effectively the same as:
40
-
41
- ``` ruby
42
- require 'request-replay'
43
- use Class.new{
44
- def initialize app, host, options={}
45
- @app, @host, @options = app, host, options
46
- end
47
-
48
- def call env
49
- # We don't want to read the socket in a thread, so create it in main
50
- # thread, and send the data in a thread as we don't care the responses
51
- Thread.new(RequestReplay.new(env, @host, @options), &:start)
52
- @app.call(env)
53
- end
54
- }, 'localhost:8080', :add_headers => {'Host' => 'example.com'},
55
- :read_wait => 5
34
+ :add_headers => {'Host' => 'example.com'},
35
+ :read_wait => 5,
36
+ # We could also rewrite the env
37
+ :rewrite_env => lambda{ |env|
38
+ if env['HTTP_HOST'].start_with?('api.')
39
+ env['PATH_INFO'] = "/api/#{env['PATH_INFO']}"
40
+ end
41
+ env
42
+ }
56
43
  run lambda{ |env| [200, {}, [env.inspect]] }
57
44
  ```
58
45
 
data/Rakefile CHANGED
@@ -8,6 +8,6 @@ end
8
8
 
9
9
  Gemgem.init(dir) do |s|
10
10
  s.name = 'request-replay'
11
- s.version = '0.6.1'
11
+ s.version = '0.6.2'
12
12
  %w[bacon muack rack].each{ |g| s.add_development_dependency(g) }
13
13
  end
@@ -7,11 +7,17 @@ class RequestReplay::Middleware
7
7
  end
8
8
 
9
9
  def call env
10
+ # Unfortunately, we need to dup env because other middleware might be
11
+ # modifying it and make RequestReplay not able to get the original env.
12
+ rr_env = if rewrite = @options[:rewrite_env]
13
+ rewrite.call(env.dup)
14
+ else
15
+ env.dup
16
+ end
17
+
10
18
  # We don't want to read the socket in a thread, so create it in main
11
19
  # thread, and send the data in a thread as we don't care the responses.
12
- # Also, unfortunately, we need to dup env because other middleware
13
- # might be modifying it and make it not able to get the original env.
14
- Thread.new(RequestReplay.new(env.dup, @host, @options), &:start)
20
+ Thread.new(RequestReplay.new(rr_env, @host, @options), &:start)
15
21
  @app.call(env)
16
22
  end
17
23
  end
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: request-replay 0.6.1 ruby lib
2
+ # stub: request-replay 0.6.2 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "request-replay"
6
- s.version = "0.6.1"
6
+ s.version = "0.6.2"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.authors = ["Lin Jen-Shin (godfat)"]
data/test/test_basic.rb CHANGED
@@ -15,14 +15,14 @@ require 'request-replay'
15
15
  require 'rack'
16
16
 
17
17
  describe RequestReplay do
18
- host = 'localhost'
18
+ host = 'localhost'.freeze
19
19
  port = 1024 + rand(2**16 - 1024)
20
20
  serv = TCPServer.new('localhost', port)
21
- hopt = "#{host}:#{port}"
21
+ hopt = "#{host}:#{port}".freeze
22
22
  env = {'REQUEST_METHOD' => 'GET',
23
23
  'PATH_INFO' => '/', 'QUERY_STRING' => 'q=1',
24
24
  'HTTP_HOST' => 'localhost',
25
- 'HTTP_PORK' => 'BEEF' }
25
+ 'HTTP_PORK' => 'BEEF' }.freeze
26
26
 
27
27
  verify = lambda do |response, expected|
28
28
  sock = serv.accept
@@ -103,17 +103,22 @@ Pork: BEEF\r
103
103
  end
104
104
 
105
105
  app.call(env.merge('REQUEST_METHOD' => 'PUT'))
106
- sock = serv.accept
107
- sock.read.should.eq <<-HTTP
106
+ begin
107
+ sock = serv.accept
108
+ sock.read.should.eq <<-HTTP
108
109
  PUT /?q=1 HTTP/1.1\r
109
110
  Host: localhost\r
110
111
  Pork: BEEF\r
111
112
  \r
112
113
  HTTP
113
- sock.close
114
+ ensure
115
+ sock.close
116
+ end
114
117
  end
115
118
 
116
119
  should 'retain original env' do
120
+ e = env.dup
121
+
117
122
  app = Rack::Builder.app do
118
123
  use RequestReplay::Middleware, hopt
119
124
  run lambda{ |env|
@@ -122,15 +127,43 @@ Pork: BEEF\r
122
127
  }
123
128
  end
124
129
 
125
- app.call(env)
126
- sock = serv.accept
127
- sock.read.should.eq <<-HTTP
130
+ app.call(e)
131
+ begin
132
+ sock = serv.accept
133
+ sock.read.should.eq <<-HTTP
128
134
  GET /?q=1 HTTP/1.1\r
129
135
  Host: localhost\r
130
136
  Pork: BEEF\r
131
137
  \r
132
138
  HTTP
133
- sock.close
139
+ ensure
140
+ sock.close
141
+ end
142
+ end
143
+
144
+ should 'rewrite_env' do
145
+ app = Rack::Builder.app do
146
+ use RequestReplay::Middleware, hopt, :rewrite_env => lambda{ |env|
147
+ if env['HTTP_HOST'].start_with?('api.')
148
+ env['PATH_INFO'] = "/api#{env['PATH_INFO']}"
149
+ end
150
+ env
151
+ }, :add_headers => {'Host' => 'eg.com'}
152
+ run lambda{ |env| [200, {}, []] }
153
+ end
154
+
155
+ app.call(env.merge('HTTP_HOST' => 'api.localhost'))
156
+ begin
157
+ sock = serv.accept
158
+ sock.read.should.eq <<-HTTP
159
+ GET /api/?q=1 HTTP/1.1\r
160
+ Host: eg.com\r
161
+ Pork: BEEF\r
162
+ \r
163
+ HTTP
164
+ ensure
165
+ sock.close
166
+ end
134
167
  end
135
168
  end
136
169
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request-replay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lin Jen-Shin (godfat)