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 +4 -4
- data/Rakefile +1 -0
- data/lib/webstub/json.rb +12 -0
- data/lib/webstub/patch/session_configuration.rb +29 -0
- data/lib/webstub/protocol.rb +2 -1
- data/lib/webstub/version.rb +1 -1
- data/spec/api_spec.rb +16 -1
- data/spec/helpers/request_helper.rb +22 -9
- data/spec/helpers/resource_helper.rb +4 -0
- data/spec/json_spec.rb +30 -0
- data/spec/resources/images/homer.jpeg +0 -0
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37e56234c1c4c8d97d42419be40c43f66fd225fe
|
4
|
+
data.tar.gz: 2c31c16fee03426ce3b2b2c12e40517da4a9f8f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ce84e80cef83a0a0cd1dfdfffc8c1fcdcf93e6a1e375bfd3a67ce80138be6b9ca2c6a04f15b28784d4df5b54b2bfa30f10d727085b5c36b46227a8e9221bb94
|
7
|
+
data.tar.gz: fa376e32a5774b2afbf70e11fe2ef11ad46b5ec32c34e9a6a64b5976fe704f555a411aec44b542884a1e76e4b976d2226dd809474c58474dd294f112d9213d40
|
data/Rakefile
CHANGED
data/lib/webstub/json.rb
CHANGED
@@ -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
|
data/lib/webstub/protocol.rb
CHANGED
@@ -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
|
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
|
|
data/lib/webstub/version.rb
CHANGED
data/spec/api_spec.rb
CHANGED
@@ -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
|
-
|
3
|
-
|
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
|
data/spec/json_spec.rb
CHANGED
@@ -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
|
Binary file
|
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
|
+
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-
|
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.
|
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:
|