webstub 0.3.1 → 0.3.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.
data/README.md CHANGED
@@ -107,9 +107,10 @@ Conventions
107
107
  - Hashes are assumed to be form data (with a `application/x-www-form-urlencoded` content type)
108
108
  - Strings are matched as is
109
109
  - The `to_return` method accepts a few options:
110
- - `json`: accepts either a Hash or a String. If a Hash is provided, it will be converted to JSON. Strings are returned as is, with the Content-Type set to `application/json`.
110
+ - `json`: accepts either a Hash, Array or a String. If a Hash or Array is provided, it will be converted to JSON. Strings are returned as is, with the Content-Type set to `application/json`.
111
111
  - `body`: accepts a String, and returns it as-is
112
112
  - `content_type`: sets the Content-Type when using the `body` parameter
113
+ - `status_code`: sets the integer Status Code of the response. Defaults to `200`.
113
114
 
114
115
  TODO
115
116
  ---------
@@ -44,7 +44,7 @@ module WebStub
44
44
  end
45
45
 
46
46
  response = NSHTTPURLResponse.alloc.initWithURL(request.URL,
47
- statusCode:200,
47
+ statusCode:stub.response_status_code,
48
48
  HTTPVersion:"HTTP/1.1",
49
49
  headerFields:stub.response_headers)
50
50
 
data/lib/webstub/stub.rb CHANGED
@@ -34,13 +34,16 @@ module WebStub
34
34
 
35
35
  attr_reader :response_body
36
36
  attr_reader :response_headers
37
+ attr_reader :response_status_code
37
38
 
38
39
  def to_return(options)
40
+ @response_status_code = options[:status_code] || 200
41
+
39
42
  if json = options[:json]
40
43
  @response_body = json
41
44
  @response_headers["Content-Type"] = "application/json"
42
45
 
43
- if @response_body.is_a? Hash
46
+ if @response_body.is_a?(Hash) || @response_body.is_a?(Array)
44
47
  @response_body = JSON.generate(@response_body)
45
48
  end
46
49
  else
@@ -1,3 +1,3 @@
1
1
  module WebStub
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/spec/api_spec.rb CHANGED
@@ -62,6 +62,10 @@ describe WebStub::API do
62
62
  it "returns a nil error" do
63
63
  @response.error.should.be.nil
64
64
  end
65
+
66
+ it "returns a 200 status code" do
67
+ @response.status_code.should.be == 200
68
+ end
65
69
  end
66
70
 
67
71
  describe "and the request includes a body" do
@@ -96,6 +100,19 @@ describe WebStub::API do
96
100
  end
97
101
  end
98
102
 
103
+ describe "when a stub sets a specified status code" do
104
+ before do
105
+ WebStub::API.stub_request(:get, @url).
106
+ to_return(json: {error: "Not Found"}, status_code: 400)
107
+
108
+ @response = get @url
109
+ end
110
+
111
+ it "the status code of the response should match" do
112
+ @response.status_code.should.be == 400
113
+ end
114
+ end
115
+
99
116
  describe ".reset_stubs" do
100
117
  before do
101
118
  WebStub::API.stub_request(:get, @url)
@@ -3,11 +3,13 @@ class Response
3
3
  @body = body ? NSString.alloc.initWithData(body, encoding:NSUTF8StringEncoding) : nil
4
4
  @headers = response ? response.allHeaderFields : nil
5
5
  @error = error
6
+ @status_code = response ? response.statusCode : nil
6
7
  end
7
8
 
8
9
  attr_reader :body
9
10
  attr_reader :headers
10
11
  attr_reader :error
12
+ attr_reader :status_code
11
13
  end
12
14
 
13
15
  def get(url)
data/spec/stub_spec.rb CHANGED
@@ -91,9 +91,20 @@ describe WebStub::Stub do
91
91
  @stub.response_body.should == '{"value":42}'
92
92
  end
93
93
 
94
+ it "allows JSON results by passing :json with an array" do
95
+ @stub.to_return(json: [{:value => 42}])
96
+ @stub.response_headers["Content-Type"].should == "application/json"
97
+ @stub.response_body.should == '[{"value":42}]'
98
+ end
99
+
94
100
  it "sets response headers" do
95
101
  @stub.to_return(body: "{}", headers: { "Content-Type" => "application/json" })
96
102
  @stub.response_headers.should.be == { "Content-Type" => "application/json" }
97
103
  end
104
+
105
+ it "sets the response status code" do
106
+ @stub.to_return(body: "{}", status_code: 400)
107
+ @stub.response_status_code.should.be == 400
108
+ end
98
109
  end
99
110
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webstub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-22 00:00:00.000000000 Z
12
+ date: 2013-01-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Easily stub out HTTP responses in RubyMotion specs
15
15
  email: