edh 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +12 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.travis.yml +13 -0
- data/.yardopts +3 -0
- data/Gemfile +23 -0
- data/Guardfile +6 -0
- data/LICENSE +22 -0
- data/Manifest +25 -0
- data/Rakefile +15 -0
- data/autotest/discover.rb +1 -0
- data/changelog.md +4 -0
- data/edh.gemspec +28 -0
- data/lib/edh.rb +47 -0
- data/lib/edh/api.rb +93 -0
- data/lib/edh/api/rest_api.rb +58 -0
- data/lib/edh/errors.rb +78 -0
- data/lib/edh/http_service.rb +142 -0
- data/lib/edh/http_service/multipart_request.rb +40 -0
- data/lib/edh/http_service/response.rb +18 -0
- data/lib/edh/test_users.rb +188 -0
- data/lib/edh/utils.rb +20 -0
- data/lib/edh/version.rb +3 -0
- data/readme.md +42 -0
- data/spec/cases/api_spec.rb +143 -0
- data/spec/cases/edh_spec.rb +64 -0
- data/spec/cases/edh_test_spec.rb +5 -0
- data/spec/cases/error_spec.rb +104 -0
- data/spec/cases/http_service_spec.rb +324 -0
- data/spec/cases/multipart_request_spec.rb +66 -0
- data/spec/cases/utils_spec.rb +24 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/custom_matchers.rb +28 -0
- data/spec/support/edh_test.rb +185 -0
- data/spec/support/mock_http_service.rb +124 -0
- data/spec/support/ordered_hash.rb +201 -0
- data/spec/support/rest_api_shared_examples.rb +114 -0
- metadata +182 -0
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "EDH::Passport::API" do
|
4
|
+
before(:each) do
|
5
|
+
@service = EDH::Passport::API.new
|
6
|
+
end
|
7
|
+
|
8
|
+
it "doesn't include an access token if none was given" do
|
9
|
+
EDH.should_receive(:make_request).with(
|
10
|
+
anything,
|
11
|
+
hash_not_including('access_token' => 1),
|
12
|
+
anything,
|
13
|
+
anything
|
14
|
+
).and_return(EDH::HTTPService::Response.new(200, "", ""))
|
15
|
+
|
16
|
+
@service.api('anything')
|
17
|
+
end
|
18
|
+
|
19
|
+
it "includes an access token if given" do
|
20
|
+
token = 'adfadf'
|
21
|
+
service = EDH::Passport::API.new token
|
22
|
+
|
23
|
+
EDH.should_receive(:make_request).with(
|
24
|
+
anything,
|
25
|
+
hash_including('access_token' => token),
|
26
|
+
anything,
|
27
|
+
anything
|
28
|
+
).and_return(EDH::HTTPService::Response.new(200, "", ""))
|
29
|
+
|
30
|
+
service.api('anything')
|
31
|
+
end
|
32
|
+
|
33
|
+
it "has an attr_reader for access token" do
|
34
|
+
token = 'adfadf'
|
35
|
+
service = EDH::Passport::API.new token
|
36
|
+
service.access_token.should == token
|
37
|
+
end
|
38
|
+
|
39
|
+
it "gets the attribute of a EDH::HTTPService::Response given by the http_component parameter" do
|
40
|
+
http_component = :method_name
|
41
|
+
|
42
|
+
response = mock('Mock EDHResponse', :body => '', :status => 200)
|
43
|
+
result = stub("result")
|
44
|
+
response.stub(http_component).and_return(result)
|
45
|
+
EDH.stub(:make_request).and_return(response)
|
46
|
+
|
47
|
+
@service.api('anything', {}, 'get', :http_component => http_component).should == result
|
48
|
+
end
|
49
|
+
|
50
|
+
it "returns the entire response if http_component => :response" do
|
51
|
+
http_component = :response
|
52
|
+
response = mock('Mock EDHResponse', :body => '', :status => 200)
|
53
|
+
EDH.stub(:make_request).and_return(response)
|
54
|
+
@service.api('anything', {}, 'get', :http_component => http_component).should == response
|
55
|
+
end
|
56
|
+
|
57
|
+
it "turns arrays of non-enumerables into comma-separated arguments" do
|
58
|
+
args = [12345, {:foo => [1, 2, "3", :four]}]
|
59
|
+
expected = ["/12345", {:foo => "1,2,3,four"}, "get", {}]
|
60
|
+
response = mock('Mock EDHResponse', :body => '', :status => 200)
|
61
|
+
EDH.should_receive(:make_request).with(*expected).and_return(response)
|
62
|
+
@service.api(*args)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "doesn't turn arrays containing enumerables into comma-separated strings" do
|
66
|
+
params = {:foo => [1, 2, ["3"], :four]}
|
67
|
+
args = [12345, params]
|
68
|
+
# we leave this as is -- the HTTP layer can either handle it appropriately
|
69
|
+
# (if appropriate behavior is defined)
|
70
|
+
# or raise an exception
|
71
|
+
expected = ["/12345", params, "get", {}]
|
72
|
+
response = mock('Mock EDHResponse', :body => '', :status => 200)
|
73
|
+
EDH.should_receive(:make_request).with(*expected).and_return(response)
|
74
|
+
@service.api(*args)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns the body of the request as JSON if no http_component is given" do
|
78
|
+
response = stub('response', :body => 'body', :status => 200)
|
79
|
+
EDH.stub(:make_request).and_return(response)
|
80
|
+
|
81
|
+
json_body = mock('JSON body')
|
82
|
+
MultiJson.stub(:load).and_return([json_body])
|
83
|
+
|
84
|
+
@service.api('anything').should == json_body
|
85
|
+
end
|
86
|
+
|
87
|
+
it "executes an error checking block if provided" do
|
88
|
+
response = EDH::HTTPService::Response.new(200, '{}', {})
|
89
|
+
EDH.stub(:make_request).and_return(response)
|
90
|
+
|
91
|
+
yield_test = mock('Yield Tester')
|
92
|
+
yield_test.should_receive(:pass)
|
93
|
+
|
94
|
+
@service.api('anything', {}, "get") do |arg|
|
95
|
+
yield_test.pass
|
96
|
+
arg.should == response
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
it "raises an API error if the HTTP response code is greater than or equal to 500" do
|
101
|
+
EDH.stub(:make_request).and_return(EDH::HTTPService::Response.new(500, 'response body', {}))
|
102
|
+
|
103
|
+
lambda { @service.api('anything') }.should raise_exception(EDH::Passport::APIError)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "handles rogue true/false as responses" do
|
107
|
+
EDH.should_receive(:make_request).and_return(EDH::HTTPService::Response.new(200, 'true', {}))
|
108
|
+
@service.api('anything').should be_true
|
109
|
+
|
110
|
+
EDH.should_receive(:make_request).and_return(EDH::HTTPService::Response.new(200, 'false', {}))
|
111
|
+
@service.api('anything').should be_false
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "with regard to leading slashes" do
|
115
|
+
it "adds a leading / to the path if not present" do
|
116
|
+
path = "anything"
|
117
|
+
EDH.should_receive(:make_request).with("/#{path}", anything, anything, anything).and_return(EDH::HTTPService::Response.new(200, 'true', {}))
|
118
|
+
@service.api(path)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "doesn't change the path if a leading / is present" do
|
122
|
+
path = "/anything"
|
123
|
+
EDH.should_receive(:make_request).with(path, anything, anything, anything).and_return(EDH::HTTPService::Response.new(200, 'true', {}))
|
124
|
+
@service.api(path)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "with an access token" do
|
129
|
+
before(:each) do
|
130
|
+
@api = EDH::Passport::API.new(@token)
|
131
|
+
end
|
132
|
+
|
133
|
+
it_should_behave_like "EDH RestAPI"
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "without an access token" do
|
137
|
+
before(:each) do
|
138
|
+
@api = EDH::Passport::API.new
|
139
|
+
end
|
140
|
+
|
141
|
+
it_should_behave_like "EDH RestAPI"
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EDH do
|
4
|
+
|
5
|
+
|
6
|
+
it "has an http_service accessor" do
|
7
|
+
EDH.should respond_to(:http_service)
|
8
|
+
EDH.should respond_to(:http_service=)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "constants" do
|
12
|
+
it "has a version" do
|
13
|
+
EDH.const_defined?("VERSION").should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
describe EDH::Passport do
|
17
|
+
it "defines REST_SERVER" do
|
18
|
+
EDH::Passport::REST_SERVER.should == "passport.everydayhero.com/api/v1"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "for deprecated services" do
|
24
|
+
before :each do
|
25
|
+
@service = EDH.http_service
|
26
|
+
end
|
27
|
+
|
28
|
+
after :each do
|
29
|
+
EDH.http_service = @service
|
30
|
+
end
|
31
|
+
|
32
|
+
it "invokes deprecated_interface if present" do
|
33
|
+
mock_service = stub("http service")
|
34
|
+
mock_service.should_receive(:deprecated_interface)
|
35
|
+
EDH.http_service = mock_service
|
36
|
+
end
|
37
|
+
|
38
|
+
it "does not set the service if it's deprecated" do
|
39
|
+
mock_service = stub("http service")
|
40
|
+
mock_service.stub(:deprecated_interface)
|
41
|
+
EDH.http_service = mock_service
|
42
|
+
EDH.http_service.should == @service
|
43
|
+
end
|
44
|
+
|
45
|
+
it "sets the service if it's not deprecated" do
|
46
|
+
mock_service = stub("http service")
|
47
|
+
EDH.http_service = mock_service
|
48
|
+
EDH.http_service.should == mock_service
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "make_request" do
|
53
|
+
it "passes all its arguments to the http_service" do
|
54
|
+
path = "foo"
|
55
|
+
args = {:a => 2}
|
56
|
+
verb = "get"
|
57
|
+
options = {:c => :d}
|
58
|
+
|
59
|
+
EDH.http_service.should_receive(:make_request).with(path, args, verb, options)
|
60
|
+
EDH.make_request(path, args, verb, options)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe EDH::Passport::APIError do
|
4
|
+
it "is a EDH::EDHError" do
|
5
|
+
EDH::Passport::APIError.new(nil, nil).should be_a(EDH::EDHError)
|
6
|
+
end
|
7
|
+
|
8
|
+
[:pp_error_type, :pp_error_code, :pp_error_subcode, :pp_error_message, :http_status, :response_body].each do |accessor|
|
9
|
+
it "has an accessor for #{accessor}" do
|
10
|
+
EDH::Passport::APIError.instance_methods.map(&:to_sym).should include(accessor)
|
11
|
+
EDH::Passport::APIError.instance_methods.map(&:to_sym).should include(:"#{accessor}=")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "sets http_status to the provided status" do
|
16
|
+
error_response = '{ "error": {"type": "foo", "other_details": "bar"} }'
|
17
|
+
EDH::Passport::APIError.new(400, error_response).response_body.should == error_response
|
18
|
+
end
|
19
|
+
|
20
|
+
it "sets response_body to the provided response body" do
|
21
|
+
EDH::Passport::APIError.new(400, '').http_status.should == 400
|
22
|
+
end
|
23
|
+
|
24
|
+
context "with an error_info hash" do
|
25
|
+
let(:error) {
|
26
|
+
error_info = {
|
27
|
+
'type' => 'type',
|
28
|
+
'message' => 'message',
|
29
|
+
'code' => 1,
|
30
|
+
'error_subcode' => 'subcode'
|
31
|
+
}
|
32
|
+
EDH::Passport::APIError.new(400, '', error_info)
|
33
|
+
}
|
34
|
+
|
35
|
+
{
|
36
|
+
:pp_error_type => 'type',
|
37
|
+
:pp_error_message => 'message',
|
38
|
+
:pp_error_code => 1,
|
39
|
+
:pp_error_subcode => 'subcode'
|
40
|
+
}.each_pair do |accessor, value|
|
41
|
+
it "sets #{accessor} to #{value}" do
|
42
|
+
error.send(accessor).should == value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "sets the error message \"type: error_info['type'], code: error_info['code'], error_subcode: error_info['error_subcode'], message: error_info['message'] [HTTP http_status]\"" do
|
47
|
+
error.message.should == "type: type, code: 1, error_subcode: subcode, message: message [HTTP 400]"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "with an error_info string" do
|
52
|
+
it "sets the error message \"error_info [HTTP http_status]\"" do
|
53
|
+
error_info = "Passport is down."
|
54
|
+
error = EDH::Passport::APIError.new(400, '', error_info)
|
55
|
+
error.message.should == "Passport is down. [HTTP 400]"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "with no error_info and a response_body containing error JSON" do
|
60
|
+
it "should extract the error info from the response body" do
|
61
|
+
response_body = '{ "error": { "type": "type", "message": "message", "code": 1, "error_subcode": "subcode" } }'
|
62
|
+
error = EDH::Passport::APIError.new(400, response_body)
|
63
|
+
{
|
64
|
+
:pp_error_type => 'type',
|
65
|
+
:pp_error_message => 'message',
|
66
|
+
:pp_error_code => 1,
|
67
|
+
:pp_error_subcode => 'subcode'
|
68
|
+
}.each_pair do |accessor, value|
|
69
|
+
error.send(accessor).should == value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
describe EDH::EDHError do
|
77
|
+
it "is a StandardError" do
|
78
|
+
EDH::EDHError.new.should be_a(StandardError)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe EDH::Passport::BadPassportResponse do
|
83
|
+
it "is a EDH::Passport::APIError" do
|
84
|
+
EDH::Passport::BadPassportResponse.new(nil, nil).should be_a(EDH::Passport::APIError)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe EDH::Passport::ServerError do
|
89
|
+
it "is a EDH::Passport::APIError" do
|
90
|
+
EDH::Passport::ServerError.new(nil, nil).should be_a(EDH::Passport::APIError)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe EDH::Passport::ClientError do
|
95
|
+
it "is a EDH::Passport::APIError" do
|
96
|
+
EDH::Passport::ClientError.new(nil, nil).should be_a(EDH::Passport::APIError)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe EDH::Passport::AuthenticationError do
|
101
|
+
it "is a EDH::Passport::ClientError" do
|
102
|
+
EDH::Passport::AuthenticationError.new(nil, nil).should be_a(EDH::Passport::ClientError)
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,324 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "EDH::HTTPService" do
|
4
|
+
it "has a faraday_middleware accessor" do
|
5
|
+
EDH::HTTPService.methods.map(&:to_sym).should include(:faraday_middleware)
|
6
|
+
EDH::HTTPService.methods.map(&:to_sym).should include(:faraday_middleware=)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has an http_options accessor" do
|
10
|
+
EDH::HTTPService.should respond_to(:http_options)
|
11
|
+
EDH::HTTPService.should respond_to(:http_options=)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "sets http_options to {} by default" do
|
15
|
+
EDH::HTTPService.http_options.should == {}
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "DEFAULT_MIDDLEWARE" do
|
19
|
+
before :each do
|
20
|
+
@builder = stub("Faraday connection builder")
|
21
|
+
@builder.stub(:request)
|
22
|
+
@builder.stub(:adapter)
|
23
|
+
@builder.stub(:use)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "is defined" do
|
27
|
+
EDH::HTTPService.const_defined?("DEFAULT_MIDDLEWARE").should be_true
|
28
|
+
end
|
29
|
+
|
30
|
+
it "adds multipart" do
|
31
|
+
@builder.should_receive(:use).with(EDH::HTTPService::MultipartRequest)
|
32
|
+
EDH::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "adds url_encoded" do
|
36
|
+
@builder.should_receive(:request).with(:url_encoded)
|
37
|
+
EDH::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "uses the default adapter" do
|
41
|
+
adapter = :testing_now
|
42
|
+
Faraday.stub(:default_adapter).and_return(adapter)
|
43
|
+
@builder.should_receive(:adapter).with(adapter)
|
44
|
+
EDH::HTTPService::DEFAULT_MIDDLEWARE.call(@builder)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe ".encode_params" do
|
49
|
+
it "returns an empty string if param_hash evaluates to false" do
|
50
|
+
EDH::HTTPService.encode_params(nil).should == ''
|
51
|
+
end
|
52
|
+
|
53
|
+
it "converts values to JSON if the value is not a String" do
|
54
|
+
val = 'json_value'
|
55
|
+
not_a_string = 'not_a_string'
|
56
|
+
not_a_string.stub(:is_a?).and_return(false)
|
57
|
+
MultiJson.should_receive(:dump).with(not_a_string).and_return(val)
|
58
|
+
|
59
|
+
string = "hi"
|
60
|
+
|
61
|
+
args = {
|
62
|
+
not_a_string => not_a_string,
|
63
|
+
string => string
|
64
|
+
}
|
65
|
+
|
66
|
+
result = EDH::HTTPService.encode_params(args)
|
67
|
+
result.split('&').find do |key_and_val|
|
68
|
+
key_and_val.match("#{not_a_string}=#{val}")
|
69
|
+
end.should be_true
|
70
|
+
end
|
71
|
+
|
72
|
+
it "escapes all values" do
|
73
|
+
args = Hash[*(1..4).map {|i| [i.to_s, "Value #{i}($"]}.flatten]
|
74
|
+
|
75
|
+
result = EDH::HTTPService.encode_params(args)
|
76
|
+
result.split('&').each do |key_val|
|
77
|
+
key, val = key_val.split('=')
|
78
|
+
val.should == CGI.escape(args[key])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "encodes parameters in alphabetical order" do
|
83
|
+
args = {:b => '2', 'a' => '1'}
|
84
|
+
|
85
|
+
result = EDH::HTTPService.encode_params(args)
|
86
|
+
result.split('&').map{|key_val| key_val.split('=')[0]}.should == ['a', 'b']
|
87
|
+
end
|
88
|
+
|
89
|
+
it "converts all keys to Strings" do
|
90
|
+
args = Hash[*(1..4).map {|i| [i, "val#{i}"]}.flatten]
|
91
|
+
|
92
|
+
result = EDH::HTTPService.encode_params(args)
|
93
|
+
result.split('&').each do |key_val|
|
94
|
+
key, val = key_val.split('=')
|
95
|
+
key.should == args.find{|key_val_arr| key_val_arr.last == val}.first.to_s
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe ".make_request" do
|
101
|
+
before :each do
|
102
|
+
# Setup stubs for make_request to execute without exceptions
|
103
|
+
@mock_body = stub('Typhoeus response body')
|
104
|
+
@mock_headers_hash = stub({:value => "headers hash"})
|
105
|
+
@mock_http_response = stub("Faraday Response", :status => 200, :headers => @mock_headers_hash, :body => @mock_body)
|
106
|
+
|
107
|
+
@mock_connection = stub("Faraday connection")
|
108
|
+
@mock_connection.stub(:get).and_return(@mock_http_response)
|
109
|
+
@mock_connection.stub(:post).and_return(@mock_http_response)
|
110
|
+
Faraday.stub(:new).and_return(@mock_connection)
|
111
|
+
end
|
112
|
+
|
113
|
+
describe "creating the Faraday connection" do
|
114
|
+
it "creates a Faraday connection using the server" do
|
115
|
+
server = "foo"
|
116
|
+
EDH::HTTPService.stub(:server).and_return(server)
|
117
|
+
Faraday.should_receive(:new).with(server, anything).and_return(@mock_connection)
|
118
|
+
EDH::HTTPService.make_request("anything", {}, "anything")
|
119
|
+
end
|
120
|
+
|
121
|
+
it "merges EDH::HTTPService.http_options into the request params" do
|
122
|
+
http_options = {:a => 2, :c => "3"}
|
123
|
+
EDH::HTTPService.http_options = http_options
|
124
|
+
Faraday.should_receive(:new).with(anything, hash_including(http_options)).and_return(@mock_connection)
|
125
|
+
EDH::HTTPService.make_request("anything", {}, "get")
|
126
|
+
end
|
127
|
+
|
128
|
+
it "merges any provided options into the request params" do
|
129
|
+
options = {:a => 2, :c => "3"}
|
130
|
+
Faraday.should_receive(:new).with(anything, hash_including(options)).and_return(@mock_connection)
|
131
|
+
EDH::HTTPService.make_request("anything", {}, "get", options)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "overrides EDH::HTTPService.http_options with any provided options for the request params" do
|
135
|
+
options = {:a => 2, :c => "3"}
|
136
|
+
http_options = {:a => :a}
|
137
|
+
EDH::HTTPService.stub(:http_options).and_return(http_options)
|
138
|
+
|
139
|
+
Faraday.should_receive(:new).with(anything, hash_including(http_options.merge(options))).and_return(@mock_connection)
|
140
|
+
EDH::HTTPService.make_request("anything", {}, "get", options)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "allows you to set other verify modes if you really want" do
|
144
|
+
options = {:ssl => {:verify => :foo}}
|
145
|
+
Faraday.should_receive(:new).with(anything, hash_including(options)).and_return(@mock_connection)
|
146
|
+
EDH::HTTPService.make_request("anything", {"access_token" => "foo"}, "get", options)
|
147
|
+
end
|
148
|
+
|
149
|
+
it "calls server with the composite options" do
|
150
|
+
options = {:a => 2, :c => "3"}
|
151
|
+
http_options = {:a => :a}
|
152
|
+
EDH::HTTPService.stub(:http_options).and_return(http_options)
|
153
|
+
EDH::HTTPService.should_receive(:server).with(hash_including(http_options.merge(options))).and_return("foo")
|
154
|
+
EDH::HTTPService.make_request("anything", {}, "get", options)
|
155
|
+
end
|
156
|
+
|
157
|
+
it "uses the default builder block if HTTPService.faraday_middleware block is not defined" do
|
158
|
+
block = Proc.new {}
|
159
|
+
stub_const("EDH::HTTPService::DEFAULT_MIDDLEWARE", block)
|
160
|
+
EDH::HTTPService.stub(:faraday_middleware).and_return(nil)
|
161
|
+
Faraday.should_receive(:new).with(anything, anything, &block).and_return(@mock_connection)
|
162
|
+
EDH::HTTPService.make_request("anything", {}, "get")
|
163
|
+
end
|
164
|
+
|
165
|
+
it "uses the defined HTTPService.faraday_middleware block if defined" do
|
166
|
+
block = Proc.new { }
|
167
|
+
EDH::HTTPService.should_receive(:faraday_middleware).and_return(block)
|
168
|
+
Faraday.should_receive(:new).with(anything, anything, &block).and_return(@mock_connection)
|
169
|
+
EDH::HTTPService.make_request("anything", {}, "get")
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
it "makes a POST request if the verb isn't get" do
|
174
|
+
@mock_connection.should_receive(:post).and_return(@mock_http_response)
|
175
|
+
EDH::HTTPService.make_request("anything", {}, "anything")
|
176
|
+
end
|
177
|
+
|
178
|
+
it "includes the verb in the body if the verb isn't get" do
|
179
|
+
verb = "eat"
|
180
|
+
@mock_connection.should_receive(:post).with(anything, hash_including("method" => verb)).and_return(@mock_http_response)
|
181
|
+
EDH::HTTPService.make_request("anything", {}, verb)
|
182
|
+
end
|
183
|
+
|
184
|
+
it "makes a GET request if the verb is get" do
|
185
|
+
@mock_connection.should_receive(:get).and_return(@mock_http_response)
|
186
|
+
EDH::HTTPService.make_request("anything", {}, "get")
|
187
|
+
end
|
188
|
+
|
189
|
+
describe "for GETs" do
|
190
|
+
it "submits the arguments in the body" do
|
191
|
+
# technically this is done for all requests, but you don't send GET requests with files
|
192
|
+
args = {"a" => :b, "c" => 3}
|
193
|
+
Faraday.should_receive(:new).with(anything, hash_including(:params => args)).and_return(@mock_connection)
|
194
|
+
EDH::HTTPService.make_request("anything", args, "get")
|
195
|
+
end
|
196
|
+
|
197
|
+
it "submits nothing to the body" do
|
198
|
+
# technically this is done for all requests, but you don't send GET requests with files
|
199
|
+
args = {"a" => :b, "c" => 3}
|
200
|
+
@mock_connection.should_receive(:get).with(anything, {}).and_return(@mock_http_response)
|
201
|
+
EDH::HTTPService.make_request("anything", args, "get")
|
202
|
+
end
|
203
|
+
|
204
|
+
it "logs verb, url and params to debug" do
|
205
|
+
args = {"a" => :b, "c" => 3}
|
206
|
+
log_message_stem = "GET: anything params: "
|
207
|
+
EDH::Utils.logger.should_receive(:debug) do |log_message|
|
208
|
+
# unordered hashes are a bane
|
209
|
+
# Ruby in 1.8 modes tends to return different hash orderings,
|
210
|
+
# which makes checking the content of the stringified hash hard
|
211
|
+
# it's enough just to ensure that there's hash content in the string, I think
|
212
|
+
log_message.should include(log_message_stem)
|
213
|
+
log_message.match(/\{.*\}/).should_not be_nil
|
214
|
+
end
|
215
|
+
|
216
|
+
EDH::HTTPService.make_request("anything", args, "get")
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
describe "for POSTs" do
|
221
|
+
it "submits the arguments in the body" do
|
222
|
+
# technically this is done for all requests, but you don't send GET requests with files
|
223
|
+
args = {"a" => :b, "c" => 3}
|
224
|
+
@mock_connection.should_receive(:post).with(anything, hash_including(args)).and_return(@mock_http_response)
|
225
|
+
EDH::HTTPService.make_request("anything", args, "post")
|
226
|
+
end
|
227
|
+
|
228
|
+
it "logs verb, url and params to debug" do
|
229
|
+
args = {"a" => :b, "c" => 3}
|
230
|
+
log_message_stem = "POST: anything params: "
|
231
|
+
EDH::Utils.logger.should_receive(:debug) do |log_message|
|
232
|
+
# unordered hashes are a bane
|
233
|
+
# Ruby in 1.8 modes tends to return different hash orderings,
|
234
|
+
# which makes checking the content of the stringified hash hard
|
235
|
+
# it's enough just to ensure that there's hash content in the string, I think
|
236
|
+
log_message.should include(log_message_stem)
|
237
|
+
log_message.match(/\{.*\}/).should_not be_nil
|
238
|
+
end
|
239
|
+
EDH::HTTPService.make_request("anything", args, "post")
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe "deprecated options" do
|
245
|
+
before :each do
|
246
|
+
EDH::HTTPService.stub(:http_options).and_return({})
|
247
|
+
@service = EDH.http_service
|
248
|
+
end
|
249
|
+
|
250
|
+
after :each do
|
251
|
+
EDH.http_service = @service
|
252
|
+
end
|
253
|
+
|
254
|
+
describe "per-request options" do
|
255
|
+
before :each do
|
256
|
+
# Setup stubs for make_request to execute without exceptions
|
257
|
+
@mock_body = stub('Typhoeus response body')
|
258
|
+
@mock_headers_hash = stub({:value => "headers hash"})
|
259
|
+
@mock_http_response = stub("Faraday Response", :status => 200, :headers => @mock_headers_hash, :body => @mock_body)
|
260
|
+
|
261
|
+
@mock_connection = stub("Faraday connection")
|
262
|
+
@mock_connection.stub(:get).and_return(@mock_http_response)
|
263
|
+
@mock_connection.stub(:post).and_return(@mock_http_response)
|
264
|
+
Faraday.stub(:new).and_return(@mock_connection)
|
265
|
+
end
|
266
|
+
|
267
|
+
describe ":typhoeus_options" do
|
268
|
+
it "merges any typhoeus_options into options" do
|
269
|
+
typhoeus_options = {:a => 2}
|
270
|
+
Faraday.should_receive(:new).with(anything, hash_including(typhoeus_options)).and_return(@mock_connection)
|
271
|
+
EDH::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
|
272
|
+
end
|
273
|
+
|
274
|
+
it "deletes the typhoeus_options key" do
|
275
|
+
typhoeus_options = {:a => 2}
|
276
|
+
Faraday.should_receive(:new).with(anything, hash_not_including(:typhoeus_options => typhoeus_options)).and_return(@mock_connection)
|
277
|
+
EDH::HTTPService.make_request("anything", {}, "get", :typhoeus_options => typhoeus_options)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
describe ":ca_path" do
|
282
|
+
it "sets any ca_path into options[:ssl]" do
|
283
|
+
ca_path = :foo
|
284
|
+
Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:ca_path => ca_path))).and_return(@mock_connection)
|
285
|
+
EDH::HTTPService.make_request("anything", {}, "get", :ca_path => ca_path)
|
286
|
+
end
|
287
|
+
|
288
|
+
it "deletes the ca_path key" do
|
289
|
+
ca_path = :foo
|
290
|
+
Faraday.should_receive(:new).with(anything, hash_not_including(:ca_path => ca_path)).and_return(@mock_connection)
|
291
|
+
EDH::HTTPService.make_request("anything", {}, "get", :ca_path => ca_path)
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
describe ":ca_file" do
|
296
|
+
it "sets any ca_file into options[:ssl]" do
|
297
|
+
ca_file = :foo
|
298
|
+
Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:ca_file => ca_file))).and_return(@mock_connection)
|
299
|
+
EDH::HTTPService.make_request("anything", {}, "get", :ca_file => ca_file)
|
300
|
+
end
|
301
|
+
|
302
|
+
it "deletes the ca_file key" do
|
303
|
+
ca_file = :foo
|
304
|
+
Faraday.should_receive(:new).with(anything, hash_not_including(:ca_file => ca_file)).and_return(@mock_connection)
|
305
|
+
EDH::HTTPService.make_request("anything", {}, "get", :ca_file => ca_file)
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
describe ":verify_mode" do
|
310
|
+
it "sets any verify_mode into options[:ssl]" do
|
311
|
+
verify_mode = :foo
|
312
|
+
Faraday.should_receive(:new).with(anything, hash_including(:ssl => hash_including(:verify_mode => verify_mode))).and_return(@mock_connection)
|
313
|
+
EDH::HTTPService.make_request("anything", {}, "get", :verify_mode => verify_mode)
|
314
|
+
end
|
315
|
+
|
316
|
+
it "deletes the verify_mode key" do
|
317
|
+
verify_mode = :foo
|
318
|
+
Faraday.should_receive(:new).with(anything, hash_not_including(:verify_mode => verify_mode)).and_return(@mock_connection)
|
319
|
+
EDH::HTTPService.make_request("anything", {}, "get", :verify_mode => verify_mode)
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|