hyperclient 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,214 +0,0 @@
1
- require_relative '../test_helper'
2
- require 'hyperclient/http'
3
-
4
- module Hyperclient
5
- describe HTTP do
6
- let(:url) do
7
- '/productions/1'
8
- end
9
-
10
- let(:config) { {base_uri: 'http://api.example.org'} }
11
-
12
- let(:http) do
13
- HTTP.instance_variable_set("@default_options", {})
14
- HTTP.new(url, config)
15
- end
16
-
17
- describe 'initialize' do
18
- it 'passes options to faraday' do
19
- Faraday.expects(:new).with(:headers => {}, :url => config[:base_uri],
20
- :x => :y).returns(stub('faraday', :headers => {},
21
- :run_request => stub(:body => '{}', :status => 200)))
22
-
23
- HTTP.new(url, config.merge(:faraday_options => {:x => :y})).get
24
- end
25
-
26
- it 'passes the options to faraday again when initializing it again' do
27
- Faraday.expects(:new).with(:headers => {}, :url => config[:base_uri],
28
- :x => :y).returns(stub('faraday', :headers => {},
29
- :run_request => stub(:body => '{}', :status => 200))).times(2)
30
-
31
- full_config = config.merge(:faraday_options => {:x => :y})
32
- 2.times { HTTP.new(url, full_config).get }
33
- end
34
-
35
- it 'passes a block to faraday' do
36
- app = stub('app')
37
- http = HTTP.new(url, config.merge(
38
- :faraday_options => {:block => lambda{|f| f.adapter :rack, app}}))
39
-
40
- app.expects(:call).returns([200, {}, '{}'] )
41
-
42
- http.get
43
- end
44
-
45
- it 'passes a block to faraday again when initializing again' do
46
- app = stub('app')
47
-
48
- app.expects(:call).returns([200, {}, '{}'] ).times(2)
49
-
50
- full_config = config.merge(:faraday_options => {:block => lambda{|f|
51
- f.adapter :rack, app}})
52
- 2.times {
53
- http = HTTP.new(url, full_config)
54
- http.get
55
- }
56
- end
57
- end
58
-
59
- describe 'url' do
60
- it 'merges the resource url with the base uri' do
61
- http.url.to_s.must_equal 'http://api.example.org/productions/1'
62
- end
63
-
64
- it 'returns the given url if it cannot merge it' do
65
- config = {base_uri: nil}
66
- http = HTTP.new(url, config)
67
- http.url.to_s.must_equal '/productions/1'
68
- end
69
- end
70
-
71
- describe 'authentication' do
72
- it 'sets the basic authentication options' do
73
- stub_request(:get, 'http://user:pass@api.example.org/productions/1').
74
- to_return(body: '{"resource": "This is the resource"}')
75
-
76
- config.update({auth: {type: :basic, user: 'user', password: 'pass'}})
77
-
78
- http.get.must_equal({'resource' => 'This is the resource'})
79
- end
80
-
81
- it 'sets the digest authentication options' do
82
- stub_request(:get, 'http://api.example.org/productions/1').
83
- to_return(status: 401, headers: {'www-authenticate' => 'private area'})
84
- stub_request(:get, 'http://api.example.org/productions/1').
85
- with(headers: {'Authorization' =>
86
- %r{Digest username="user", realm="", algorithm=MD5, uri="/productions/1"}}).
87
- to_return(body: '{"resource": "This is the resource"}')
88
-
89
- config.update({auth: {type: :digest, user: 'user', password: 'pass'}})
90
-
91
- http.get.must_equal({'resource' => 'This is the resource'})
92
- end
93
- end
94
-
95
- describe 'headers' do
96
- it 'sets headers from the given option' do
97
- config.update({headers: {'accept-encoding' => 'deflate, gzip'}})
98
-
99
- stub_request(:get, 'http://api.example.org/productions/1').
100
- with(headers: {'Accept-Encoding' => 'deflate, gzip'}).
101
- to_return(body: '{"resource": "This is the resource"}')
102
-
103
- http.get
104
- end
105
- end
106
-
107
- describe 'debug' do
108
- before(:each) do
109
- @stderr = $stderr
110
- stub_request(:get, 'http://api.example.org/productions/1').
111
- to_return(body: '{"resource": "This is the resource"}')
112
- end
113
-
114
- after(:each) do
115
- $stderr = @stderr
116
- end
117
-
118
- it 'enables debugging' do
119
- $stderr = StringIO.new
120
- config.update({debug: true})
121
-
122
- http.get
123
-
124
- $stderr.string.must_include('get http://api.example.org/productions/1')
125
- end
126
-
127
- it 'uses a custom stream' do
128
- stream = StringIO.new
129
- config.update({debug: stream})
130
-
131
- http.get
132
-
133
- stream.string.must_include('get http://api.example.org/productions/1')
134
- end
135
- end
136
-
137
- describe 'get' do
138
- it 'sends a GET request and returns the response body' do
139
- stub_request(:get, 'http://api.example.org/productions/1').
140
- to_return(body: '{"resource": "This is the resource"}')
141
-
142
- http.get.must_equal({'resource' => 'This is the resource'})
143
- end
144
-
145
- it 'returns the parsed response' do
146
- stub_request(:get, 'http://api.example.org/productions/1').
147
- to_return(body: '{"some_json": 12345 }', headers: {content_type: 'application/json'})
148
-
149
- http.get.must_equal({'some_json' => 12345})
150
- end
151
-
152
- it 'returns nil if the response body is nil' do
153
- stub_request(:get, 'http://api.example.org/productions/1').
154
- to_return(body: nil)
155
-
156
- http.get.must_equal(nil)
157
- end
158
- end
159
-
160
- describe 'post' do
161
- it 'sends a POST request' do
162
- stub_request(:post, 'http://api.example.org/productions/1').
163
- to_return(body: 'Posting like a big boy huh?', status: 201)
164
-
165
- response = http.post({data: 'foo'})
166
- response.code.must_equal 201
167
- assert_requested :post, 'http://api.example.org/productions/1',
168
- body: {data: 'foo'}
169
- end
170
- end
171
-
172
- describe 'put' do
173
- it 'sends a PUT request' do
174
- stub_request(:put, 'http://api.example.org/productions/1').
175
- to_return(body: 'No changes were made', status: 204)
176
-
177
- response = http.put({attribute: 'changed'})
178
- response.code.must_equal 204
179
- assert_requested :put, 'http://api.example.org/productions/1',
180
- body: {attribute: 'changed'}
181
- end
182
- end
183
-
184
- describe 'options' do
185
- it 'sends a OPTIONS request' do
186
- stub_request(:options, 'http://api.example.org/productions/1').
187
- to_return(status: 200, headers: {allow: 'GET, POST'})
188
-
189
- response = http.options
190
- response.headers.must_include 'allow'
191
- end
192
- end
193
-
194
- describe 'head' do
195
- it 'sends a HEAD request' do
196
- stub_request(:head, 'http://api.example.org/productions/1').
197
- to_return(status: 200, headers: {content_type: 'application/json'})
198
-
199
- response = http.head
200
- response.headers.must_include 'content-type'
201
- end
202
- end
203
-
204
- describe 'delete' do
205
- it 'sends a DELETE request' do
206
- stub_request(:delete, 'http://api.example.org/productions/1').
207
- to_return(body: 'Resource deleted', status: 200)
208
-
209
- response = http.delete
210
- response.code.must_equal 200
211
- end
212
- end
213
- end
214
- end