webstub 0.6.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: 9f498c840e10f87af4e242bf40649b85c6e5f554
4
- data.tar.gz: 6f73bdb65fd0884065a5aa35a75b5196ebb3a326
3
+ metadata.gz: 37e56234c1c4c8d97d42419be40c43f66fd225fe
4
+ data.tar.gz: 2c31c16fee03426ce3b2b2c12e40517da4a9f8f2
5
5
  SHA512:
6
- metadata.gz: 89d90b6d50d6ead629cd3817d5ed730f61f33263bdab2db6bc5adc563cd7642bd464bc20c64d6affcf22f4f1a0489b2b2434e8ca1591a60fad1ed203906ed0a4
7
- data.tar.gz: 6c7c9d832871faa3ad313c4606d18c12cdc8f8893465e189b97f0a4ea381af7ed356ef97efc45d9c3cf927f9aeaefa942c8300f92ce1e394e5fc42bab36ada34
6
+ metadata.gz: 1ce84e80cef83a0a0cd1dfdfffc8c1fcdcf93e6a1e375bfd3a67ce80138be6b9ca2c6a04f15b28784d4df5b54b2bfa30f10d727085b5c36b46227a8e9221bb94
7
+ data.tar.gz: fa376e32a5774b2afbf70e11fe2ef11ad46b5ec32c34e9a6a64b5976fe704f555a411aec44b542884a1e76e4b976d2226dd809474c58474dd294f112d9213d40
data/Rakefile CHANGED
@@ -28,6 +28,7 @@ Motion::Project::App.setup do |app|
28
28
 
29
29
  app.files << File.join(File.dirname(__FILE__), "lib/spec/spec_delegate.rb")
30
30
  app.delegate_class = "SpecDelegate"
31
+ app.resources_dirs = %w(spec/resources/images)
31
32
  end
32
33
 
33
34
  app.name = gem_name
@@ -6,5 +6,17 @@ module WebStub
6
6
 
7
7
  NSString.alloc.initWithData(result, encoding:NSUTF8StringEncoding)
8
8
  end
9
+
10
+ def self.parse(str)
11
+ data = str
12
+ unless data.is_a?(NSData)
13
+ data = str.dataUsingEncoding(NSUTF8StringEncoding)
14
+ end
15
+
16
+ error = Pointer.new(:object)
17
+ result = NSJSONSerialization.JSONObjectWithData(data, options: 0, error: error)
18
+
19
+ result
20
+ end
9
21
  end
10
22
  end
@@ -0,0 +1,29 @@
1
+ if Kernel.const_defined?(:NSURLSessionConfiguration)
2
+ class NSURLSessionConfiguration
3
+ class << self
4
+ alias_method :originalDefaultSessionConfiguration, :defaultSessionConfiguration
5
+
6
+ def self.defaultSessionConfiguration
7
+ config = originalDefaultSessionConfiguration
8
+
9
+ unless config.include?(WebStub::Protocol)
10
+ config.protocolClasses << WebStub::Protocol
11
+ end
12
+
13
+ config
14
+ end
15
+
16
+ alias_method :originalEphemeralSessionConfiguration, :ephemeralSessionConfiguration
17
+
18
+ def self.ephemeralSessionConfiguration
19
+ config = originalEphemeralSessionConfiguration
20
+
21
+ unless config.include?(WebStub::Protocol)
22
+ config.protocolClasses << WebStub::Protocol
23
+ end
24
+
25
+ config
26
+ end
27
+ end
28
+ end
29
+ end
@@ -74,7 +74,8 @@ module WebStub
74
74
  end
75
75
 
76
76
  client.URLProtocol(self, didReceiveResponse:response, cacheStoragePolicy:NSURLCacheStorageNotAllowed)
77
- client.URLProtocol(self, didLoadData:@stub.response_body.dataUsingEncoding(NSUTF8StringEncoding))
77
+ client.URLProtocol(self, didLoadData: @stub.response_body.is_a?(NSData) ? @stub.response_body :
78
+ @stub.response_body.dataUsingEncoding(NSUTF8StringEncoding))
78
79
  client.URLProtocolDidFinishLoading(self)
79
80
  end
80
81
 
@@ -1,3 +1,3 @@
1
1
  module WebStub
2
- VERSION = "0.6.1"
2
+ VERSION = "1.0"
3
3
  end
@@ -116,6 +116,21 @@ describe WebStub::API do
116
116
  end
117
117
  end
118
118
 
119
+ describe "response with binary data" do
120
+ before do
121
+ @image = load_image('homer')
122
+ @url = 'http://somehost/image'
123
+ WebStub::API.stub_request(:get, @url).
124
+ to_return(body: load_image('homer'), content_type: "image/jpeg")
125
+ end
126
+
127
+ it "returns the correct body" do
128
+ @response = get(@url, :binary_resp)
129
+ @response.body.isEqualToData(@image).should == true
130
+ end
131
+ end
132
+
133
+
119
134
  describe "when a stub sets a specified status code" do
120
135
  before do
121
136
  WebStub::API.stub_request(:get, @url).
@@ -152,7 +167,7 @@ describe WebStub::API do
152
167
  end
153
168
 
154
169
  it "returns the correct body" do
155
- response = get(@url, { "My-Header" => "123" })
170
+ response = get(@url, :string_resp, { "My-Header" => "123" })
156
171
 
157
172
  response.body.should == '{}'
158
173
  end
@@ -1,18 +1,30 @@
1
1
  class Response
2
- def initialize(body, response, error)
3
- @body = body ? NSString.alloc.initWithData(body, encoding:NSUTF8StringEncoding) : nil
2
+
3
+ def initialize(body, response, error, resp_type)
4
+ allocators = {:string_resp => ->(b) { alloc_data_string(b) },
5
+ :binary_resp => ->(b) { alloc_data_binary(b) }}
6
+
7
+ @body = allocators[resp_type].call(body)
4
8
  @headers = response ? response.allHeaderFields : nil
5
9
  @error = error
6
10
  @status_code = response ? response.statusCode : nil
7
11
  end
8
12
 
13
+ def alloc_data_string(body)
14
+ body ? NSString.alloc.initWithData(body, encoding: NSUTF8StringEncoding) : nil
15
+ end
16
+
17
+ def alloc_data_binary(body)
18
+ NSData.alloc.initWithData(body)
19
+ end
20
+
9
21
  attr_reader :body
10
22
  attr_reader :headers
11
23
  attr_reader :error
12
24
  attr_reader :status_code
13
25
  end
14
26
 
15
- def get(url, headers={})
27
+ def get(url, response_type = :string_resp, headers={})
16
28
  request = NSMutableURLRequest.alloc.init
17
29
  request.URL = NSURL.URLWithString(url)
18
30
 
@@ -20,7 +32,7 @@ def get(url, headers={})
20
32
  request.setValue(value, forHTTPHeaderField: key.to_s)
21
33
  end
22
34
 
23
- issue_request(request)
35
+ issue_request(request, response_type)
24
36
  end
25
37
 
26
38
  def post(url, body)
@@ -34,16 +46,16 @@ def post(url, body)
34
46
 
35
47
  "#{key}=#{value}"
36
48
  end.join("&")
37
-
38
- request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type")
49
+
50
+ request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
39
51
  end
40
52
 
41
53
  request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
42
54
 
43
- issue_request(request)
55
+ issue_request(request, :string_resp)
44
56
  end
45
57
 
46
- def issue_request(request)
58
+ def issue_request(request, response_type)
47
59
  result = {}
48
60
  queue = NSOperationQueue.alloc.init
49
61
  lock = NSConditionLock.alloc.initWithCondition(0)
@@ -59,6 +71,7 @@ def issue_request(request)
59
71
  end)
60
72
 
61
73
  lock.lockWhenCondition(1)
74
+ lock.unlockWithCondition(1)
62
75
 
63
- Response.new(result[:data], result[:response], result[:error])
76
+ Response.new(result[:data], result[:response], result[:error], response_type)
64
77
  end
@@ -0,0 +1,4 @@
1
+ def load_image(path)
2
+ NSData.dataWithContentsOfFile(
3
+ NSBundle.mainBundle.pathForResource(path, ofType:'jpeg'))
4
+ end
@@ -18,4 +18,34 @@ describe WebStub::JSON do
18
18
  }}).should == "{\"int\":42,\"string\":\"hello\",\"array\":[1,2,3],\"hash\":{\"title\":\"the title\"}}"
19
19
  end
20
20
  end
21
+
22
+ describe ".parse" do
23
+ [:string, :data].each do |type|
24
+ before do
25
+ if type == :string
26
+ @transformer = lambda { |s| s }
27
+ else
28
+ @transformer = lambda { |s| s.dataUsingEncoding(NSUTF8StringEncoding) }
29
+ end
30
+ end
31
+
32
+ it "parses an empty Array" do
33
+ WebStub::JSON.parse(@transformer.call("[]")).should == []
34
+ end
35
+
36
+ it "parses an empty Hash" do
37
+ WebStub::JSON.parse(@transformer.call("{}")).should == {}
38
+ end
39
+
40
+ it "parses a dictionary" do
41
+ json = %q({"int":42,"string":"hello","array":[1,2,3],"hash":{"title":"nested"}})
42
+ WebStub::JSON.parse(@transformer.call(json)).should == {
43
+ "int" => 42,
44
+ "string" => "hello",
45
+ "array" => [1,2,3],
46
+ "hash" => {"title" => "nested"}
47
+ }
48
+ end
49
+ end
50
+ end
21
51
  end
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.6.1
4
+ version: '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-07-07 00:00:00.000000000 Z
11
+ date: 2013-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -56,6 +56,7 @@ files:
56
56
  - lib/webstub.rb
57
57
  - lib/webstub/api.rb
58
58
  - lib/webstub/json.rb
59
+ - lib/webstub/patch/session_configuration.rb
59
60
  - lib/webstub/protocol.rb
60
61
  - lib/webstub/registry.rb
61
62
  - lib/webstub/spec_helpers.rb
@@ -64,9 +65,11 @@ files:
64
65
  - lib/webstub/version.rb
65
66
  - spec/api_spec.rb
66
67
  - spec/helpers/request_helper.rb
68
+ - spec/helpers/resource_helper.rb
67
69
  - spec/json_spec.rb
68
70
  - spec/protocol_spec.rb
69
71
  - spec/registry_spec.rb
72
+ - spec/resources/images/homer.jpeg
70
73
  - spec/spec_helpers_spec.rb
71
74
  - spec/stub_spec.rb
72
75
  - spec/uri_spec.rb
@@ -90,16 +93,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
93
  version: '0'
91
94
  requirements: []
92
95
  rubyforge_project:
93
- rubygems_version: 2.0.2
96
+ rubygems_version: 2.1.1
94
97
  signing_key:
95
98
  specification_version: 4
96
99
  summary: Easily stub out HTTP responses in RubyMotion specs
97
100
  test_files:
98
101
  - spec/api_spec.rb
99
102
  - spec/helpers/request_helper.rb
103
+ - spec/helpers/resource_helper.rb
100
104
  - spec/json_spec.rb
101
105
  - spec/protocol_spec.rb
102
106
  - spec/registry_spec.rb
107
+ - spec/resources/images/homer.jpeg
103
108
  - spec/spec_helpers_spec.rb
104
109
  - spec/stub_spec.rb
105
110
  - spec/uri_spec.rb
111
+ has_rdoc: