webstub 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a5ee428399a0214cd16a8d72651cf34d6d07497
4
- data.tar.gz: e55c492aca59216cf5b245286e8ba9859d2b9abb
3
+ metadata.gz: 83ff338cdc8626be04a4ca16d8a72f4fa015e5ae
4
+ data.tar.gz: 32edcc1fb2dc10c867ce83ad0c8bfbd4860959dd
5
5
  SHA512:
6
- metadata.gz: 58f0a1305f0d7a7b0f6dceea41696210694991f88ddc36c20007a0312fe32f76e77c6087a12f96b7f89a9e67e37b41cb0074b31a13c7055c00b105a61b7213db
7
- data.tar.gz: 20330658e51a988deada8bfec184c32a699c8437eb635a0073a13ad6c4840788e9cba1e7cc4287c01895c1d2b862d6c3feaa993b1f9f8dd663e907f54df8a5e6
6
+ metadata.gz: f94be75dca1cacf1f27d03282ab04f98f791bc8f7e3ec6e4080a759914169a3b73ed4d9a83c4ad01ae5a71d6fec3152ae371c3c9f246c60034f9da5a8720a7fd
7
+ data.tar.gz: 28b5ea6be60a257bfb17a3e517dde84b2fb9a231eb4e245dd200fd5be0d5e17f9d99d2bc2dd0e91bcb36a34b122b5c54d103f98f0fa7c5dab65ed2b315ca8ba9
data/Gemfile CHANGED
@@ -1,11 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'rake'
4
-
5
- gem 'listen', :git => 'git://github.com/guard/listen.git'
6
- gem 'guard', :git => 'git://github.com/guard/guard.git'
7
- gem 'guard-motion'
8
- gem 'rb-fsevent', '~> 0.9.1'
9
- gem 'coolline'
10
- gem 'terminal-notifier-guard'
11
- gem 'rubygems-tasks'
3
+ gemspec
data/README.md CHANGED
@@ -91,6 +91,23 @@ describe "Example" do
91
91
  end
92
92
  end
93
93
  end
94
+
95
+ describe "Stubbing a GET request to fail" do
96
+ it "returns an NSError with the NSURLError domain" do
97
+ stub_request(:get, "https://example.com/action").
98
+ to_fail(code: NSURLErrorNotConnectedToInternet)
99
+
100
+ @error = nil
101
+ @api.get_albums do |results, error|
102
+ @error = error
103
+ resume
104
+ end
105
+
106
+ wait_max 1.0 do
107
+ @error.code.should == NSURLErrorNotConnectedToInternet
108
+ end
109
+ end
110
+ end
94
111
  end
95
112
  ```
96
113
 
@@ -108,6 +125,9 @@ Conventions
108
125
  - The `to_redirect` method accepts:
109
126
  - `url`: String of the URL to redirect to (*required*)
110
127
  - All options supported by `to_return` except for `status_code`
128
+ - The `to_fail` method accepts one of the following options:
129
+ - `code`: `NSURLErrorDomain` [error code](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html)
130
+ - `error`: `NSError` to fail the request with
111
131
 
112
132
  Expectations
113
133
  -----------------
@@ -51,6 +51,11 @@ module WebStub
51
51
  headerFields:@stub.response_headers)
52
52
  @stub.requests += 1
53
53
 
54
+ if @stub.error?
55
+ client.URLProtocol(self, didFailWithError: @stub.response_error)
56
+ return
57
+ end
58
+
54
59
  if @stub.redirects?
55
60
  url = NSURL.URLWithString(@stub.response_headers["Location"])
56
61
  redirect_request = NSURLRequest.requestWithURL(url)
data/lib/webstub/stub.rb CHANGED
@@ -14,10 +14,15 @@ module WebStub
14
14
 
15
15
  @response_body = ""
16
16
  @response_delay = 0.0
17
+ @response_error = nil
17
18
  @response_headers = {}
18
19
  @response_status_code = 200
19
20
  end
20
21
 
22
+ def error?
23
+ ! @response_error.nil?
24
+ end
25
+
21
26
  def matches?(method, url, options={})
22
27
  if @request_url != canonicalize_url(url)
23
28
  return false
@@ -58,9 +63,22 @@ module WebStub
58
63
 
59
64
  attr_reader :response_body
60
65
  attr_reader :response_delay
66
+ attr_reader :response_error
61
67
  attr_reader :response_headers
62
68
  attr_reader :response_status_code
63
69
 
70
+ def to_fail(options)
71
+ if error = options.delete(:error)
72
+ @response_error = error
73
+ elsif code = options.delete(:code)
74
+ @response_error = NSError.errorWithDomain(NSURLErrorDomain, code: code, userInfo: nil)
75
+ else
76
+ raise ArgumentError, "to_fail requires either the code or error option"
77
+ end
78
+
79
+ self
80
+ end
81
+
64
82
  def to_return(options)
65
83
  if status_code = options[:status_code]
66
84
  @response_status_code = status_code
@@ -1,3 +1,3 @@
1
1
  module WebStub
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/spec/api_spec.rb CHANGED
@@ -172,6 +172,17 @@ describe WebStub::API do
172
172
  response.body.should == '{}'
173
173
  end
174
174
  end
175
+
176
+ describe "when a stub is configured to fail" do
177
+ it "fails the request" do
178
+ WebStub::API.stub_request(:get, @url).
179
+ to_fail(code: NSURLErrorNetworkConnectionLost)
180
+
181
+ response = get(@url)
182
+
183
+ response.error.code.should == NSURLErrorNetworkConnectionLost
184
+ end
185
+ end
175
186
  end
176
187
 
177
188
  describe ".reset_stubs" do
data/spec/stub_spec.rb CHANGED
@@ -17,6 +17,26 @@ describe WebStub::Stub do
17
17
  lambda { WebStub::Stub.new("invalid", "http://www.yahoo.com/") }.should.raise(ArgumentError)
18
18
  end
19
19
 
20
+ it "does not have a response error by default" do
21
+ @stub.response_error.should.be.nil
22
+ end
23
+
24
+ describe "#error?" do
25
+ describe "by default" do
26
+ it "returns false" do
27
+ @stub.error?.should.be.false
28
+ end
29
+ end
30
+
31
+ describe "after calling to_fail" do
32
+ it "returns true" do
33
+ @stub.to_fail(code: NSURLErrorUnsupportedURL)
34
+
35
+ @stub.error?.should.be.true
36
+ end
37
+ end
38
+ end
39
+
20
40
  describe "#matches?" do
21
41
  it "returns true when provided an identical stub" do
22
42
  @stub.matches?(:get, "http://www.yahoo.com/").should.be.true
@@ -108,6 +128,30 @@ describe WebStub::Stub do
108
128
  end
109
129
  end
110
130
 
131
+ describe "#to_fail" do
132
+ it "rejects an empty options Hash" do
133
+ lambda { @stub.to_fail({}) }.should.raise(ArgumentError)
134
+ end
135
+
136
+ it "builds an NSError using the option specified by code" do
137
+ @stub.to_fail(code: NSURLErrorUnsupportedURL)
138
+
139
+ @stub.response_error.domain.should == NSURLErrorDomain
140
+ @stub.response_error.code.should == NSURLErrorUnsupportedURL
141
+ end
142
+
143
+ it "accepts an arbitrary NSError using the error option" do
144
+ error = NSError.errorWithDomain(0, code: 123, userInfo: nil)
145
+ @stub.to_fail(error: error)
146
+
147
+ @stub.response_error.should == error
148
+ end
149
+
150
+ it "returns self" do
151
+ @stub.to_fail(code: 123).should == @stub
152
+ end
153
+ end
154
+
111
155
  describe "#to_redirect" do
112
156
  it "requires the :url option" do
113
157
  lambda { @stub.to_redirect }.should.raise(ArgumentError)
data/webstub.gemspec CHANGED
@@ -13,4 +13,7 @@ Gem::Specification.new do |gem|
13
13
  gem.name = "webstub"
14
14
  gem.require_paths = ["lib"]
15
15
  gem.version = WebStub::VERSION
16
+
17
+ gem.add_development_dependency "rake"
18
+ gem.add_development_dependency "rubygems-tasks"
16
19
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webstub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.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-06-26 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2013-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubygems-tasks
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: Easily stub out HTTP responses in RubyMotion specs
14
42
  email:
15
43
  - mattgreenrocks@gmail.com