webstub 0.4.0 → 0.5.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: 8b1fadc30394c28a2c70f47cdf1b3bb00fd53b25
4
- data.tar.gz: 7a378b835765d1bc94256aac54c28ddb83692d9e
3
+ metadata.gz: 5a5ee428399a0214cd16a8d72651cf34d6d07497
4
+ data.tar.gz: e55c492aca59216cf5b245286e8ba9859d2b9abb
5
5
  SHA512:
6
- metadata.gz: 8c6e61d19d442611bf3ba6d94c20eff7aa50264073e6d5b4ca95f2f655b9e920dc87ddacdaea6f1a3f039c11a753dfd9f7cf8f1a3114028568c766d20ae282f5
7
- data.tar.gz: 4b15563577dff7e90410f4709a30c098825425bbf21fe39bf2618cf8d6819d87bbcd228d76c1a61e9bbb417eb911ed99a4e12c596c03bc1a3f2628a0de888cf0
6
+ metadata.gz: 58f0a1305f0d7a7b0f6dceea41696210694991f88ddc36c20007a0312fe32f76e77c6087a12f96b7f89a9e67e37b41cb0074b31a13c7055c00b105a61b7213db
7
+ data.tar.gz: 20330658e51a988deada8bfec184c32a699c8437eb635a0073a13ad6c4840788e9cba1e7cc4287c01895c1d2b862d6c3feaa993b1f9f8dd663e907f54df8a5e6
data/.gitignore CHANGED
@@ -1,6 +1,7 @@
1
1
  .rvmrc
2
2
  .repl_history
3
3
  build
4
+ pkg
4
5
  resources/*.nib
5
6
  resources/*.momd
6
7
  resources/*.storyboardc
data/README.md CHANGED
@@ -15,7 +15,7 @@ Installation
15
15
  ------------
16
16
  Update your Gemfile:
17
17
 
18
- gem "webstub", "~> 0.4.0"
18
+ gem "webstub"
19
19
 
20
20
  Bundle:
21
21
 
@@ -105,6 +105,9 @@ Conventions
105
105
  - `body`: accepts a String, and returns it as-is
106
106
  - `content_type`: sets the Content-Type when using the `body` parameter
107
107
  - `status_code`: sets the integer Status Code of the response. Defaults to `200`.
108
+ - The `to_redirect` method accepts:
109
+ - `url`: String of the URL to redirect to (*required*)
110
+ - All options supported by `to_return` except for `status_code`
108
111
 
109
112
  Expectations
110
113
  -----------------
@@ -49,6 +49,24 @@ module WebStub
49
49
  statusCode:@stub.response_status_code,
50
50
  HTTPVersion:"HTTP/1.1",
51
51
  headerFields:@stub.response_headers)
52
+ @stub.requests += 1
53
+
54
+ if @stub.redirects?
55
+ url = NSURL.URLWithString(@stub.response_headers["Location"])
56
+ redirect_request = NSURLRequest.requestWithURL(url)
57
+
58
+ client.URLProtocol(self, wasRedirectedToRequest: redirect_request, redirectResponse: response)
59
+
60
+ unless @stub = self.class.stub_for(redirect_request)
61
+ error = NSError.errorWithDomain("WebStub", code:0, userInfo:{ NSLocalizedDescriptionKey: "network access is not permitted!"})
62
+ client.URLProtocol(self, didFailWithError:error)
63
+
64
+ return
65
+ end
66
+
67
+ @timer = NSTimer.scheduledTimerWithTimeInterval(@stub.response_delay, target:self, selector: :completeLoading, userInfo:nil, repeats:false)
68
+ return
69
+ end
52
70
 
53
71
  client.URLProtocol(self, didReceiveResponse:response, cacheStoragePolicy:NSURLCacheStorageNotAllowed)
54
72
  client.URLProtocol(self, didLoadData:@stub.response_body.dataUsingEncoding(NSUTF8StringEncoding))
@@ -66,8 +84,6 @@ module WebStub
66
84
  return
67
85
  end
68
86
 
69
- @stub.requests += 1
70
-
71
87
  @timer = NSTimer.scheduledTimerWithTimeInterval(@stub.response_delay, target:self, selector: :completeLoading, userInfo:nil, repeats:false)
72
88
  end
73
89
 
data/lib/webstub/stub.rb CHANGED
@@ -48,6 +48,10 @@ module WebStub
48
48
 
49
49
  attr_accessor :requests
50
50
 
51
+ def redirects?
52
+ @response_status_code.between?(300, 399) && @response_headers["Location"] != nil
53
+ end
54
+
51
55
  def requested?
52
56
  @requests > 0
53
57
  end
@@ -62,6 +66,10 @@ module WebStub
62
66
  @response_status_code = status_code
63
67
  end
64
68
 
69
+ if headers = options[:headers]
70
+ @response_headers.merge!(headers)
71
+ end
72
+
65
73
  if json = options[:json]
66
74
  @response_body = json
67
75
  @response_headers["Content-Type"] = "application/json"
@@ -71,7 +79,6 @@ module WebStub
71
79
  end
72
80
  else
73
81
  @response_body = options[:body] || ""
74
- @response_headers = options[:headers] || {}
75
82
 
76
83
  if content_type = options[:content_type]
77
84
  @response_headers["Content-Type"] = content_type
@@ -85,6 +92,18 @@ module WebStub
85
92
  self
86
93
  end
87
94
 
95
+ def to_redirect(options)
96
+ unless url = options.delete(:url)
97
+ raise ArgumentError, "to_redirect requires the :url option"
98
+ end
99
+
100
+ options[:headers] ||= {}
101
+ options[:headers]["Location"] = url
102
+ options[:status_code] = 301
103
+
104
+ to_return(options)
105
+ end
106
+
88
107
  def with(options)
89
108
  if body = options[:body]
90
109
  @request_body = body
@@ -1,3 +1,3 @@
1
1
  module WebStub
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/spec/api_spec.rb CHANGED
@@ -2,7 +2,7 @@ describe WebStub::API do
2
2
  before do
3
3
  WebStub::API.reset_stubs
4
4
 
5
- @url = "http://www.google.com/search"
5
+ @url = "http://www.example.com/"
6
6
  @request = NSURLRequest.requestWithURL(NSURL.URLWithString(@url))
7
7
  end
8
8
 
@@ -157,6 +157,21 @@ describe WebStub::API do
157
157
  response.body.should == '{}'
158
158
  end
159
159
  end
160
+
161
+ describe "when a stub redirects" do
162
+ it "redirects the request" do
163
+ @redirect_url = @url + "redirect"
164
+
165
+ WebStub::API.stub_request(:get, @url).
166
+ to_redirect(url: @redirect_url)
167
+ WebStub::API.stub_request(:get, @redirect_url).
168
+ to_return(json: {})
169
+
170
+ response = get(@url)
171
+
172
+ response.body.should == '{}'
173
+ end
174
+ end
160
175
  end
161
176
 
162
177
  describe ".reset_stubs" do
@@ -20,11 +20,7 @@ def get(url, headers={})
20
20
  request.setValue(value, forHTTPHeaderField: key.to_s)
21
21
  end
22
22
 
23
- response = Pointer.new(:object)
24
- error = Pointer.new(:object)
25
- body = NSURLConnection.sendSynchronousRequest(request, returningResponse:response, error:error)
26
-
27
- Response.new(body, response[0], error[0])
23
+ issue_request(request)
28
24
  end
29
25
 
30
26
  def post(url, body)
@@ -44,10 +40,25 @@ def post(url, body)
44
40
 
45
41
  request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
46
42
 
47
- response = Pointer.new(:object)
48
- error = Pointer.new(:object)
49
- body = NSURLConnection.sendSynchronousRequest(request, returningResponse:response, error:error)
50
-
51
- Response.new(body, response[0], error[0])
43
+ issue_request(request)
52
44
  end
53
45
 
46
+ def issue_request(request)
47
+ result = {}
48
+ queue = NSOperationQueue.alloc.init
49
+ lock = NSConditionLock.alloc.initWithCondition(0)
50
+
51
+ NSURLConnection.sendAsynchronousRequest(request,
52
+ queue: queue,
53
+ completionHandler: lambda do |response, data, error|
54
+ lock.lockWhenCondition(0)
55
+ result[:data] = data
56
+ result[:response] = response
57
+ result[:error] = error
58
+ lock.unlockWithCondition(1)
59
+ end)
60
+
61
+ lock.lockWhenCondition(1)
62
+
63
+ Response.new(result[:data], result[:response], result[:error])
64
+ end
data/spec/stub_spec.rb CHANGED
@@ -108,6 +108,28 @@ describe WebStub::Stub do
108
108
  end
109
109
  end
110
110
 
111
+ describe "#to_redirect" do
112
+ it "requires the :url option" do
113
+ lambda { @stub.to_redirect }.should.raise(ArgumentError)
114
+ end
115
+
116
+ it "sets the Location header to the specified URL" do
117
+ @stub.to_redirect(url: "http://example.org/")
118
+
119
+ @stub.response_headers.should.include("Location")
120
+ end
121
+
122
+ it "sets the status code to 301" do
123
+ @stub.to_redirect(url: "http://example.org/")
124
+
125
+ @stub.response_status_code.should == 301
126
+ end
127
+
128
+ it "returns the stub" do
129
+ @stub.to_redirect(url: "http://example.org/").should == @stub
130
+ end
131
+ end
132
+
111
133
  describe "#to_return" do
112
134
  it "sets the response body" do
113
135
  @stub.to_return(body: "hello")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webstub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.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-24 00:00:00.000000000 Z
11
+ date: 2013-06-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Easily stub out HTTP responses in RubyMotion specs
14
14
  email: