webstub 1.0.1 → 1.1.0

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: f8a0647b249466d2189e9ebf25a250591112b85b
4
- data.tar.gz: 919be82dac97c1de010be6ec29d29272991c1a8e
3
+ metadata.gz: 1dd5474dfb7e84f9c5c4ab0a6272c62273425de0
4
+ data.tar.gz: 19f99236e92eda0fadb4b317363ef984b81bd3ac
5
5
  SHA512:
6
- metadata.gz: ec623ef6875c97b66c376786436be175110e7dad8f5294d6756edbda3f6fabf1fe71d916f4aaa1a23730642072258d45728f175b221930c30299e65badb20145
7
- data.tar.gz: 0a109dab7b355c1d46ef2d288d6f024ef6a6af7d433ce0ec0eae1c11f16b051f2181dab6deb879b33c7aee10d5138fe2cea52a4654faa2c63a9f0f07eac3aa58
6
+ metadata.gz: ae67b53bd163b33bf3a1e0116b01f778e19cda56a99267e5aa71e345141109b518beb17ed87f6cbc55f831a076cc09e3fa2686801113f5db418df297b681d26f
7
+ data.tar.gz: 67712acc73b86d72de7fe45152361be12a81352b9a9e6e3ce580634e6528b283a3aa538b7ddb8149179331015e3bbbd19278e31aff07be9eae707ca545daf22e
data/README.md CHANGED
@@ -149,6 +149,35 @@ describe Elevate::HTTP do
149
149
  end
150
150
  ```
151
151
 
152
+ Callbacks
153
+ ----------
154
+
155
+ Sometimes, you may want to inspect the request made for a given URL. In this case, you can use the `with_callback` method of the stub returned by `stub_request` to add a callback hook:
156
+
157
+ ```ruby
158
+ describe Elevate:HTTP do
159
+ extend WebStub::SpecHelpers
160
+
161
+ describe ".post" do
162
+ it "synchronously issues an HTTP POST request" do
163
+ stub = stub_request(:post, "http://www.example.com/")
164
+
165
+ stub.with_callback do |headers, body|
166
+ headers.kind_of?(Hash).should == true
167
+ body.kind_of?(Hash).should == true
168
+ body.should == {"key" => "value"}
169
+ end
170
+
171
+ Elevate::HTTP.post("http://www.example.com/", json: {key: "value"})
172
+ end
173
+ end
174
+ end
175
+
176
+
177
+
178
+ ```
179
+
180
+
152
181
  Caveats
153
182
  ---------
154
183
  While WebStub supports `NSURLSession`, it does not support background sessions, as they don't allow the use of custom `NSURLProtocol` classes.
@@ -90,6 +90,9 @@ module WebStub
90
90
  return
91
91
  end
92
92
 
93
+ if body = self.class.parse_body(request)
94
+ @stub.do_callback(self.request.allHTTPHeaderFields, body)
95
+ end
93
96
  @timer = NSTimer.scheduledTimerWithTimeInterval(@stub.response_delay, target:self, selector: :completeLoading, userInfo:nil, repeats:false)
94
97
  end
95
98
 
data/lib/webstub/stub.rb CHANGED
@@ -11,6 +11,7 @@ module WebStub
11
11
  @request_url = canonicalize_url(url)
12
12
  @request_headers = nil
13
13
  @request_body = nil
14
+ @callback = nil
14
15
 
15
16
  @response_body = ""
16
17
  @response_delay = 0.0
@@ -52,6 +53,7 @@ module WebStub
52
53
  end
53
54
 
54
55
  attr_accessor :requests
56
+ attr_accessor :callback
55
57
 
56
58
  def redirects?
57
59
  @response_status_code.between?(300, 399) && @response_headers["Location"] != nil
@@ -110,6 +112,16 @@ module WebStub
110
112
  self
111
113
  end
112
114
 
115
+ def do_callback(headers, body)
116
+ if @callback
117
+ @callback.call(headers, body)
118
+ end
119
+ end
120
+
121
+ def with_callback(&callback)
122
+ @callback = callback
123
+ end
124
+
113
125
  def to_redirect(options)
114
126
  unless url = options.delete(:url)
115
127
  raise ArgumentError, "to_redirect requires the :url option"
@@ -1,3 +1,3 @@
1
1
  module WebStub
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
data/spec/api_spec.rb CHANGED
@@ -144,6 +144,22 @@ describe WebStub::API do
144
144
  end
145
145
  end
146
146
 
147
+ describe "when a stub sets a callback" do
148
+
149
+ before do
150
+ @stub = WebStub::API.stub_request(:post, @url)
151
+ end
152
+
153
+ it "calls the callback with header and body passed in" do
154
+ @stub.with_callback do |headers, body|
155
+ body.should == {"q" => "hello"}
156
+ body.kind_of?(Hash).should.be.true
157
+ headers.kind_of?(Hash).should.be.true
158
+ end
159
+ @response = post @url, :q => "hello"
160
+ end
161
+ end
162
+
147
163
  describe "when a stub sets a delay" do
148
164
  before do
149
165
  WebStub::API.stub_request(:get, @url).
data/spec/stub_spec.rb CHANGED
@@ -37,6 +37,24 @@ describe WebStub::Stub do
37
37
  end
38
38
  end
39
39
 
40
+ describe "callback" do
41
+ before do
42
+ @called = 0
43
+ @stub.with_callback do |headers,body|
44
+ @called += 1
45
+ headers.should == "headers"
46
+ body.should == "body"
47
+ end
48
+ end
49
+ it "calls the callback method" do
50
+ @stub.callback.should.not.be.nil
51
+ end
52
+ it "should call the callback" do
53
+ @stub.do_callback("headers", "body")
54
+ @called.should == 1
55
+ end
56
+ end
57
+
40
58
  describe "#matches?" do
41
59
  it "returns true when provided an identical stub" do
42
60
  @stub.matches?(:get, "http://www.yahoo.com/").should.be.true
data/webstub.gemspec CHANGED
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.files = `git ls-files`.split($\)
12
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
13
  gem.name = "webstub"
14
+ gem.license = "MIT"
14
15
  gem.require_paths = ["lib"]
15
16
  gem.version = WebStub::VERSION
16
17
 
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webstub
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Green
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-16 00:00:00.000000000 Z
11
+ date: 2014-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rubygems-tasks
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Easily stub out HTTP responses in RubyMotion specs
@@ -45,8 +45,8 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - .gitignore
49
- - .travis.yml
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
50
  - Gemfile
51
51
  - Guardfile
52
52
  - LICENSE
@@ -75,7 +75,8 @@ files:
75
75
  - spec/uri_spec.rb
76
76
  - webstub.gemspec
77
77
  homepage: https://github.com/mattgreen/webstub
78
- licenses: []
78
+ licenses:
79
+ - MIT
79
80
  metadata: {}
80
81
  post_install_message:
81
82
  rdoc_options: []
@@ -83,17 +84,17 @@ require_paths:
83
84
  - lib
84
85
  required_ruby_version: !ruby/object:Gem::Requirement
85
86
  requirements:
86
- - - '>='
87
+ - - ">="
87
88
  - !ruby/object:Gem::Version
88
89
  version: '0'
89
90
  required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  requirements:
91
- - - '>='
92
+ - - ">="
92
93
  - !ruby/object:Gem::Version
93
94
  version: '0'
94
95
  requirements: []
95
96
  rubyforge_project:
96
- rubygems_version: 2.1.1
97
+ rubygems_version: 2.2.2
97
98
  signing_key:
98
99
  specification_version: 4
99
100
  summary: Easily stub out HTTP responses in RubyMotion specs
@@ -108,4 +109,3 @@ test_files:
108
109
  - spec/spec_helpers_spec.rb
109
110
  - spec/stub_spec.rb
110
111
  - spec/uri_spec.rb
111
- has_rdoc: