braintreehttp 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/braintreehttp.gemspec +15 -0
- data/lib/braintreehttp.rb +7 -0
- data/lib/braintreehttp/encoder.rb +33 -0
- data/lib/braintreehttp/environment.rb +9 -0
- data/lib/braintreehttp/errors.rb +16 -0
- data/lib/braintreehttp/http_client.rb +132 -0
- data/lib/braintreehttp/injector.rb +7 -0
- data/spec/braintreehttp/encoder_spec.rb +82 -0
- data/spec/braintreehttp/http_client_spec.rb +271 -0
- data/spec/spec_helper.rb +109 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6711edadc9c81e8a9a4f96d89a1e7b657d5aac7b
|
4
|
+
data.tar.gz: c2cf94d1703e260828b7c356c7bb35288d680c00
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6129ea639803e97db4b699426fc11717fd214fecbda36b5f4a32c41847d80a319558b6b40fcdcd0839b01bb73dfbc73a31c885792b2f712f1078002830abb982
|
7
|
+
data.tar.gz: 896163eca59f9cab89bca38af47083811dad69821983fab2148b0f4689e27be695cf20c8e33289016613962d2248bb1795468743b5e12888ed178bafea0e7a04
|
@@ -0,0 +1,15 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "braintreehttp"
|
5
|
+
s.summary = "BraintreeHttp Client Library"
|
6
|
+
s.description = "Used for generated API clients"
|
7
|
+
s.version = "0.1.0"
|
8
|
+
s.license = "MIT"
|
9
|
+
s.author = "Braintree"
|
10
|
+
s.email = "code@getbraintree.com"
|
11
|
+
s.homepage = "http://www.braintreepayments.com/"
|
12
|
+
s.rubyforge_project = "braintreehttp"
|
13
|
+
s.has_rdoc = false
|
14
|
+
s.files = Dir.glob ["lib/**/*.{rb}", "spec/**/*", "*.gemspec"]
|
15
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module BraintreeHttp
|
4
|
+
|
5
|
+
class Encoder
|
6
|
+
|
7
|
+
def serialize_request(req)
|
8
|
+
raise UnsupportedEncodingError.new('HttpRequest did not have Content-Type header set') unless req.headers && (req.headers['content-type'] || req.headers['Content-Type'])
|
9
|
+
|
10
|
+
content_type = req.headers['content-type'] || req.headers['Content-Type']
|
11
|
+
raise UnsupportedEncodingError.new("Unable to serialize request with Content-Type #{content_type}. Supported encodings are #{supported_encodings}") unless content_type == 'application/json'
|
12
|
+
|
13
|
+
JSON.generate(req.body)
|
14
|
+
end
|
15
|
+
|
16
|
+
def deserialize_response(resp, headers)
|
17
|
+
raise UnsupportedEncodingError.new('HttpResponse did not have Content-Type header set') unless headers && (headers['content-type'] || headers['Content-Type'])
|
18
|
+
|
19
|
+
content_type = headers['content-type'] || headers['Content-Type']
|
20
|
+
raise UnsupportedEncodingError.new("Unable to deserialize response with Content-Type #{content_type}. Supported decodings are #{supported_decodings}") unless content_type.include? 'application/json'
|
21
|
+
|
22
|
+
JSON.parse(resp)
|
23
|
+
end
|
24
|
+
|
25
|
+
def supported_encodings
|
26
|
+
['application/json']
|
27
|
+
end
|
28
|
+
|
29
|
+
def supported_decodings
|
30
|
+
['application/json']
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module BraintreeHttp
|
2
|
+
class ServiceIOError < IOError
|
3
|
+
attr_accessor :status_code, :result, :headers
|
4
|
+
def initialize(status_code, result, headers)
|
5
|
+
@status_code = status_code
|
6
|
+
@result = result
|
7
|
+
@headers = headers
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class UnsupportedEncodingError < IOError
|
12
|
+
def initialize(msg)
|
13
|
+
super(msg)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module BraintreeHttp
|
4
|
+
|
5
|
+
LINE_FEED = "\r\n"
|
6
|
+
|
7
|
+
class HttpClient
|
8
|
+
attr_accessor :environment
|
9
|
+
|
10
|
+
def initialize(environment)
|
11
|
+
@environment = environment
|
12
|
+
@injectors = []
|
13
|
+
@encoder = Encoder.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def user_agent
|
17
|
+
"BraintreeHttp-Ruby HTTP/1.1"
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_injector(inj)
|
21
|
+
@injectors << inj
|
22
|
+
end
|
23
|
+
|
24
|
+
def has_body(request)
|
25
|
+
request.respond_to?(:body) and request.body
|
26
|
+
end
|
27
|
+
|
28
|
+
def execute(request)
|
29
|
+
if !request.headers
|
30
|
+
request.headers = {}
|
31
|
+
end
|
32
|
+
|
33
|
+
@injectors.each do |injector|
|
34
|
+
injector.inject(request)
|
35
|
+
end
|
36
|
+
|
37
|
+
if !request.headers["User-Agent"] || request.headers["User-Agent"] == "Ruby"
|
38
|
+
request.headers["User-Agent"] = user_agent
|
39
|
+
end
|
40
|
+
|
41
|
+
httpRequest = Net::HTTPGenericRequest.new(request.verb, true, true, request.path, request.headers)
|
42
|
+
|
43
|
+
content_type = request.headers["Content-Type"]
|
44
|
+
if content_type && content_type.start_with?("multipart/")
|
45
|
+
boundary = DateTime.now.strftime("%Q")
|
46
|
+
httpRequest.set_content_type("multipart/form-data; boundary=#{boundary}")
|
47
|
+
|
48
|
+
form_params = []
|
49
|
+
request.body.each do |k, v|
|
50
|
+
if v.is_a? File
|
51
|
+
form_params.push(_add_file_part(k, v))
|
52
|
+
else
|
53
|
+
form_params.push(_add_form_field(k, v))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
httpRequest.body = form_params.collect {|p| "--" + boundary + "#{LINE_FEED}" + p}.join("") + "--" + boundary + "--"
|
58
|
+
elsif has_body(request)
|
59
|
+
if request.body.is_a? String
|
60
|
+
httpRequest.body = request.body
|
61
|
+
else
|
62
|
+
httpRequest.body = serialize_request(request)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
uri = URI(@environment.base_url)
|
67
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
68
|
+
_parse_response(http.request(httpRequest))
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def serialize_request(request)
|
73
|
+
@encoder.serialize_request(request)
|
74
|
+
end
|
75
|
+
|
76
|
+
def deserialize_response(response_body, headers)
|
77
|
+
@encoder.deserialize_response(response_body, headers)
|
78
|
+
end
|
79
|
+
|
80
|
+
def _add_form_field(key, value)
|
81
|
+
return "Content-Disposition: form-data; name=\"#{key}\"#{LINE_FEED}#{LINE_FEED}#{value}#{LINE_FEED}"
|
82
|
+
end
|
83
|
+
|
84
|
+
def _add_file_part(key, file)
|
85
|
+
mime_type = _mime_type_for_file_name(file.path)
|
86
|
+
return "Content-Disposition: form-data; name=\"#{key}\"; filename=\"#{File.basename(file.path)}\"#{LINE_FEED}" +
|
87
|
+
"Content-Type: #{mime_type}#{LINE_FEED}#{LINE_FEED}#{file.read}#{LINE_FEED}"
|
88
|
+
end
|
89
|
+
|
90
|
+
def _mime_type_for_file_name(filename)
|
91
|
+
file_extension = File.extname(filename).strip.downcase[1..-1]
|
92
|
+
if file_extension == "jpeg" || file_extension == "jpg"
|
93
|
+
return "image/jpeg"
|
94
|
+
elsif file_extension == "png"
|
95
|
+
return "image/png"
|
96
|
+
elsif file_extension == "pdf"
|
97
|
+
return "application/pdf"
|
98
|
+
else
|
99
|
+
return "application/octet-stream"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def _parse_response(response)
|
104
|
+
status_code = response.code.to_i
|
105
|
+
|
106
|
+
result = response.body
|
107
|
+
headers = response.to_hash
|
108
|
+
if result && !result.empty?
|
109
|
+
deserialized = deserialize_response(response.body, headers)
|
110
|
+
if deserialized.is_a? String
|
111
|
+
result = deserialized
|
112
|
+
else
|
113
|
+
result = OpenStruct.new(deserialized)
|
114
|
+
end
|
115
|
+
else
|
116
|
+
result = nil
|
117
|
+
end
|
118
|
+
|
119
|
+
obj = OpenStruct.new({
|
120
|
+
:status_code => status_code,
|
121
|
+
:result => result,
|
122
|
+
:headers => response.to_hash,
|
123
|
+
})
|
124
|
+
|
125
|
+
if status_code >= 200 and status_code < 300
|
126
|
+
return obj
|
127
|
+
elsif
|
128
|
+
raise ServiceIOError.new(obj.status_code, obj.result, obj.headers)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
describe Encoder do
|
4
|
+
|
5
|
+
describe 'serialize_request' do
|
6
|
+
it 'serializes the request' do
|
7
|
+
req = OpenStruct.new({
|
8
|
+
:headers => {
|
9
|
+
"content-type" => "application/json"
|
10
|
+
},
|
11
|
+
:body => {
|
12
|
+
"string" => "value",
|
13
|
+
"number" => 1.23,
|
14
|
+
"bool" => true,
|
15
|
+
"array" => ["one", "two", "three"],
|
16
|
+
"nested" => {
|
17
|
+
"nested_string" => "nested_value",
|
18
|
+
"nested_array" => [1,2,3]
|
19
|
+
}
|
20
|
+
}
|
21
|
+
})
|
22
|
+
|
23
|
+
expected = '{"string":"value","number":1.23,"bool":true,"array":["one","two","three"],"nested":{"nested_string":"nested_value","nested_array":[1,2,3]}}'
|
24
|
+
|
25
|
+
expect(Encoder.new.serialize_request(req)).to eq(expected)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'throws when content-type is not application/json' do
|
29
|
+
req = OpenStruct.new({
|
30
|
+
:headers => {
|
31
|
+
"content-type" => "not application/json"
|
32
|
+
},
|
33
|
+
:body => { :string => "value" }
|
34
|
+
})
|
35
|
+
|
36
|
+
expect{Encoder.new.serialize_request(req)}.to raise_error(UnsupportedEncodingError, /Unable to serialize request with Content-Type .*\. Supported encodings are \["application\/json"\]/)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'throws when headers undefined' do
|
40
|
+
req = OpenStruct.new({
|
41
|
+
:body => { :string => "value" }
|
42
|
+
})
|
43
|
+
|
44
|
+
expect{Encoder.new.serialize_request(req)}.to raise_error(UnsupportedEncodingError, 'HttpRequest did not have Content-Type header set')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'deserialize_response' do
|
49
|
+
it 'throws when content-type not application/json' do
|
50
|
+
body = '{"string":"value","number":1.23,"bool":true,"array":["one","two","three"],"nested":{"nested_string":"nested_value","nested_array":[1,2,3]}}'
|
51
|
+
headers = {
|
52
|
+
"content-type" => ["application/xml"]
|
53
|
+
}
|
54
|
+
|
55
|
+
expect{Encoder.new.deserialize_response(body, headers)}.to raise_error(UnsupportedEncodingError, /Unable to deserialize response with Content-Type .*\. Supported decodings are \["application\/json"\]/)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'throws when headers undefined' do
|
59
|
+
body = '{"string":"value","number":1.23,"bool":true,"array":["one","two","three"],"nested":{"nested_string":"nested_value","nested_array":[1,2,3]}}'
|
60
|
+
|
61
|
+
expect{Encoder.new.deserialize_response(body, nil)}.to raise_error(UnsupportedEncodingError, 'HttpResponse did not have Content-Type header set')
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'deserializes the response' do
|
65
|
+
expected = {
|
66
|
+
"string" => "value",
|
67
|
+
"number" => 1.23,
|
68
|
+
"bool" => true,
|
69
|
+
"array" => ["one", "two", "three"],
|
70
|
+
"nested" => {
|
71
|
+
"nested_string" => "nested_value",
|
72
|
+
"nested_array" => [1,2,3]
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
headers = {"content-type" => ["application/json"]}
|
77
|
+
body = '{"string":"value","number":1.23,"bool":true,"array":["one","two","three"],"nested":{"nested_string":"nested_value","nested_array":[1,2,3]}}'
|
78
|
+
|
79
|
+
expect(Encoder.new.deserialize_response(body, headers)).to eq(expected)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,271 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
describe HttpClient do
|
5
|
+
|
6
|
+
before do
|
7
|
+
WebMock.disable!
|
8
|
+
@environment = Environment.new('https://ip.jsontest.com')
|
9
|
+
end
|
10
|
+
|
11
|
+
it "uses injectors to modify request" do
|
12
|
+
WebMock.enable!
|
13
|
+
|
14
|
+
http_client = HttpClient.new(@environment)
|
15
|
+
|
16
|
+
class CustomInjector < Injector
|
17
|
+
def inject(request)
|
18
|
+
request.headers["Some-Key"] = "Some Value"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
http_client.add_injector(CustomInjector.new)
|
23
|
+
req = OpenStruct.new({:verb => "GET", :path => "/"})
|
24
|
+
|
25
|
+
stub_request(:any, @environment.base_url)
|
26
|
+
|
27
|
+
http_client.execute(req)
|
28
|
+
|
29
|
+
expect(req.headers["Some-Key"]).to eq("Some Value")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "sets User-Agent header in request if not set" do
|
33
|
+
WebMock.enable!
|
34
|
+
|
35
|
+
http_client = HttpClient.new(@environment)
|
36
|
+
req = OpenStruct.new({:verb => "GET", :path => "/"})
|
37
|
+
|
38
|
+
stub_request(:any, @environment.base_url)
|
39
|
+
|
40
|
+
http_client.execute(req)
|
41
|
+
|
42
|
+
expect(req.headers["User-Agent"]).to eq("BraintreeHttp-Ruby HTTP/1.1")
|
43
|
+
end
|
44
|
+
|
45
|
+
it "does not overwrite User-Agent header if set" do
|
46
|
+
WebMock.enable!
|
47
|
+
|
48
|
+
http_client = HttpClient.new(@environment)
|
49
|
+
|
50
|
+
req = OpenStruct.new({:verb => "GET", :path => "/", :headers => {"User-Agent" => "Not Ruby Http/1.1"}})
|
51
|
+
|
52
|
+
stub_request(:any, @environment.base_url)
|
53
|
+
|
54
|
+
http_client.execute(req)
|
55
|
+
|
56
|
+
expect(req.headers["User-Agent"]).to eq("Not Ruby Http/1.1")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "uses body in request" do
|
60
|
+
WebMock.enable!
|
61
|
+
|
62
|
+
stub_request(:delete, @environment.base_url + "/path")
|
63
|
+
|
64
|
+
req = OpenStruct.new({:verb => "DELETE", :path => "/path"})
|
65
|
+
|
66
|
+
req.body = "I want to delete the thing"
|
67
|
+
|
68
|
+
http_client = HttpClient.new(@environment)
|
69
|
+
|
70
|
+
resp = http_client.execute(req)
|
71
|
+
expect(resp.status_code).to eq(200)
|
72
|
+
|
73
|
+
assert_requested(:delete, @environment.base_url + "/path") { |requested| requested.body == "I want to delete the thing" }
|
74
|
+
end
|
75
|
+
|
76
|
+
it "parses 200 level response" do
|
77
|
+
WebMock.enable!
|
78
|
+
|
79
|
+
return_data = JSON.generate({
|
80
|
+
:some_key => "value"
|
81
|
+
})
|
82
|
+
|
83
|
+
stub_request(:any, @environment.base_url).
|
84
|
+
to_return(body: return_data, status: 204,
|
85
|
+
headers: {
|
86
|
+
'Some-Weird-Header' => 'Some weird value',
|
87
|
+
'Content-Type' => 'application/json'
|
88
|
+
})
|
89
|
+
|
90
|
+
http_client = HttpClient.new(@environment)
|
91
|
+
req = OpenStruct.new({:verb => "GET", :path => "/"})
|
92
|
+
|
93
|
+
resp = http_client.execute(req)
|
94
|
+
|
95
|
+
expect(resp.status_code).to eq(204)
|
96
|
+
expect(resp.result.some_key).to eq('value')
|
97
|
+
expect(resp.headers["Some-Weird-Header".downcase]).to eq(["Some weird value"])
|
98
|
+
end
|
99
|
+
|
100
|
+
it "throws for non-200 level response" do
|
101
|
+
WebMock.enable!
|
102
|
+
|
103
|
+
return_data = {
|
104
|
+
:error => "error message",
|
105
|
+
:another_key => 1013
|
106
|
+
}
|
107
|
+
|
108
|
+
json = JSON.generate(return_data)
|
109
|
+
|
110
|
+
stub_request(:any, @environment.base_url).
|
111
|
+
to_return(body: json, status: 400,
|
112
|
+
headers: {
|
113
|
+
'Some-Weird-Header' => 'Some weird value',
|
114
|
+
'Content-Type' => 'application/json'
|
115
|
+
})
|
116
|
+
|
117
|
+
http_client = HttpClient.new(@environment)
|
118
|
+
req = OpenStruct.new({:verb => "GET", :path => URI(@environment.base_url)})
|
119
|
+
|
120
|
+
begin
|
121
|
+
resp = http_client.execute(req)
|
122
|
+
fail
|
123
|
+
rescue => e
|
124
|
+
resp = e
|
125
|
+
expect(resp.status_code).to eq(400)
|
126
|
+
expect(resp.result.error).to eq('error message')
|
127
|
+
expect(resp.result.another_key).to eq(1013)
|
128
|
+
expect(resp.headers["Some-Weird-Header".downcase]).to eq(["Some weird value"])
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
it "makes request when only a path is specified" do
|
133
|
+
WebMock.enable!
|
134
|
+
|
135
|
+
stub_request(:any, @environment.base_url + "/v1/api")
|
136
|
+
.to_return(status: 200)
|
137
|
+
|
138
|
+
http_client = HttpClient.new(@environment)
|
139
|
+
req = OpenStruct.new({:verb => "GET", :path => "/v1/api"})
|
140
|
+
|
141
|
+
resp = http_client.execute(req)
|
142
|
+
expect(resp.status_code).to eq(200)
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'uses encoder to serialize requests by default' do
|
146
|
+
WebMock.enable!
|
147
|
+
|
148
|
+
return_data = {
|
149
|
+
:key => "value"
|
150
|
+
}
|
151
|
+
|
152
|
+
http_client = HttpClient.new(@environment)
|
153
|
+
stub_request(:get, @environment.base_url + "/v1/api")
|
154
|
+
.to_return(body: JSON.generate(return_data), status: 200, headers: {"Content-Type" => "application/json"})
|
155
|
+
|
156
|
+
req = OpenStruct.new({:verb => "GET", :path => "/v1/api"})
|
157
|
+
resp = http_client.execute(req)
|
158
|
+
|
159
|
+
expect(resp.status_code).to eq(200)
|
160
|
+
expect(resp.result.key).to eq('value')
|
161
|
+
end
|
162
|
+
|
163
|
+
it "allows subclasses to modify response body" do
|
164
|
+
WebMock.enable!
|
165
|
+
|
166
|
+
return_data = {
|
167
|
+
:key => "value"
|
168
|
+
}
|
169
|
+
|
170
|
+
class JSONHttpClient < HttpClient
|
171
|
+
def deserialize_response(body, headers)
|
172
|
+
if headers["content-type"].include? "application/json"
|
173
|
+
return 'something else'
|
174
|
+
end
|
175
|
+
|
176
|
+
body
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
http_client = JSONHttpClient.new(@environment)
|
181
|
+
|
182
|
+
stub_request(:get, @environment.base_url + "/v1/api")
|
183
|
+
.to_return(body: JSON.generate(return_data), status: 200, headers: {"Content-Type" => "application/json"})
|
184
|
+
|
185
|
+
req = OpenStruct.new({:verb => "GET", :path => "/v1/api"})
|
186
|
+
|
187
|
+
resp = http_client.execute(req)
|
188
|
+
|
189
|
+
expect(resp.status_code).to eq(200)
|
190
|
+
expect(resp.result).to eq('something else')
|
191
|
+
end
|
192
|
+
|
193
|
+
it "encodes multipart/form-data when a file is present without body" do
|
194
|
+
WebMock.enable!
|
195
|
+
|
196
|
+
stub_request(:any, @environment.base_url + "/v1/api")
|
197
|
+
|
198
|
+
http_client = HttpClient.new(@environment)
|
199
|
+
file = File.new("README.md", "r")
|
200
|
+
req = OpenStruct.new({:verb => "POST", :path => "/v1/api", :headers => {"Content-Type" => "multipart/form-data"}})
|
201
|
+
req.body = {:readme => file}
|
202
|
+
|
203
|
+
resp = http_client.execute(req)
|
204
|
+
|
205
|
+
assert_requested(:post, @environment.base_url + "/v1/api") { |requested|
|
206
|
+
requested.body.include? "Content-Disposition: form-data; name=\"readme\"; filename=\"README.md\""
|
207
|
+
}
|
208
|
+
end
|
209
|
+
|
210
|
+
it "encodes multipart/form-data when a file is present with body" do
|
211
|
+
WebMock.enable!
|
212
|
+
|
213
|
+
stub_request(:any, @environment.base_url + "/v1/api")
|
214
|
+
|
215
|
+
http_client = HttpClient.new(@environment)
|
216
|
+
file = File.new("README.md", "r")
|
217
|
+
|
218
|
+
req = OpenStruct.new({:verb => "POST", :path => "/v1/api", :headers => {"Content-Type" => "multipart/form-data"}})
|
219
|
+
req.body = {
|
220
|
+
:key => "value",
|
221
|
+
:another_key => 1013,
|
222
|
+
:readme => file,
|
223
|
+
}
|
224
|
+
|
225
|
+
resp = http_client.execute(req)
|
226
|
+
|
227
|
+
assert_requested(:post, @environment.base_url + "/v1/api") { |requested|
|
228
|
+
requested.body.include? "Content-Disposition: form-data; name=\"readme\"; filename=\"README.md\""
|
229
|
+
requested.body.include? "Content-Disposition: form-data; name=\"key\""
|
230
|
+
requested.body.include? "value"
|
231
|
+
requested.body.include? "Content-Disposition: form-data; name=\"another_key\""
|
232
|
+
requested.body.include? "1013"
|
233
|
+
}
|
234
|
+
end
|
235
|
+
|
236
|
+
it "does not error if no file or body present on a request class" do
|
237
|
+
class Request
|
238
|
+
|
239
|
+
attr_accessor :path, :body, :headers, :verb, :file
|
240
|
+
|
241
|
+
def initialize()
|
242
|
+
@headers = {}
|
243
|
+
@verb = "POST"
|
244
|
+
@path = "/v1/api"
|
245
|
+
end
|
246
|
+
|
247
|
+
def requestBody(body)
|
248
|
+
@body = body
|
249
|
+
end
|
250
|
+
|
251
|
+
def setFile(file)
|
252
|
+
@file = file
|
253
|
+
end
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
WebMock.enable!
|
258
|
+
|
259
|
+
stub_request(:any, @environment.base_url + "/v1/api")
|
260
|
+
http_client = HttpClient.new(@environment)
|
261
|
+
|
262
|
+
begin
|
263
|
+
http_client.execute(Request.new)
|
264
|
+
rescue Exception => e
|
265
|
+
fail e.message
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
269
|
+
|
270
|
+
end
|
271
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'webmock/rspec'
|
2
|
+
require_relative "../lib/braintreehttp.rb"
|
3
|
+
include WebMock::API
|
4
|
+
include BraintreeHttp
|
5
|
+
|
6
|
+
|
7
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
8
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
9
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
10
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
11
|
+
# files.
|
12
|
+
#
|
13
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
14
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
15
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
16
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
17
|
+
# a separate helper file that requires the additional dependencies and performs
|
18
|
+
# the additional setup, and require it from the spec files that actually need
|
19
|
+
# it.
|
20
|
+
#
|
21
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
22
|
+
# users commonly want.
|
23
|
+
#
|
24
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
25
|
+
RSpec.configure do |config|
|
26
|
+
# rspec-expectations config goes here. You can use an alternate
|
27
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
28
|
+
# assertions if you prefer.
|
29
|
+
config.expect_with :rspec do |expectations|
|
30
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
31
|
+
# and `failure_message` of custom matchers include text for helper methods
|
32
|
+
# defined using `chain`, e.g.:
|
33
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
34
|
+
# # => "be bigger than 2 and smaller than 4"
|
35
|
+
# ...rather than:
|
36
|
+
# # => "be bigger than 2"
|
37
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
38
|
+
end
|
39
|
+
|
40
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
41
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
42
|
+
config.mock_with :rspec do |mocks|
|
43
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
44
|
+
# a real object. This is generally recommended, and will default to
|
45
|
+
# `true` in RSpec 4.
|
46
|
+
mocks.verify_partial_doubles = true
|
47
|
+
end
|
48
|
+
|
49
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
50
|
+
# have no way to turn it off -- the option exists only for backwards
|
51
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
52
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
53
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
54
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
55
|
+
|
56
|
+
# The settings below are suggested to provide a good initial experience
|
57
|
+
# with RSpec, but feel free to customize to your heart's content.
|
58
|
+
=begin
|
59
|
+
# This allows you to limit a spec run to individual examples or groups
|
60
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
61
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
62
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
63
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
64
|
+
config.filter_run_when_matching :focus
|
65
|
+
|
66
|
+
# Allows RSpec to persist some state between runs in order to support
|
67
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
68
|
+
# you configure your source control system to ignore this file.
|
69
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
70
|
+
|
71
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
72
|
+
# recommended. For more details, see:
|
73
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
74
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
75
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
76
|
+
config.disable_monkey_patching!
|
77
|
+
|
78
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
79
|
+
# be too noisy due to issues in dependencies.
|
80
|
+
config.warnings = true
|
81
|
+
|
82
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
83
|
+
# file, and it's useful to allow more verbose output when running an
|
84
|
+
# individual spec file.
|
85
|
+
if config.files_to_run.one?
|
86
|
+
# Use the documentation formatter for detailed output,
|
87
|
+
# unless a formatter has already been configured
|
88
|
+
# (e.g. via a command-line flag).
|
89
|
+
config.default_formatter = 'doc'
|
90
|
+
end
|
91
|
+
|
92
|
+
# Print the 10 slowest examples and example groups at the
|
93
|
+
# end of the spec run, to help surface which specs are running
|
94
|
+
# particularly slow.
|
95
|
+
config.profile_examples = 10
|
96
|
+
|
97
|
+
# Run specs in random order to surface order dependencies. If you find an
|
98
|
+
# order dependency and want to debug it, you can fix the order by providing
|
99
|
+
# the seed, which is printed after each run.
|
100
|
+
# --seed 1234
|
101
|
+
config.order = :random
|
102
|
+
|
103
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
104
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
105
|
+
# test failures related to randomization by passing the same `--seed` value
|
106
|
+
# as the one that triggered the failure.
|
107
|
+
Kernel.srand config.seed
|
108
|
+
=end
|
109
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: braintreehttp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Braintree
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Used for generated API clients
|
14
|
+
email: code@getbraintree.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- braintreehttp.gemspec
|
20
|
+
- lib/braintreehttp.rb
|
21
|
+
- lib/braintreehttp/encoder.rb
|
22
|
+
- lib/braintreehttp/environment.rb
|
23
|
+
- lib/braintreehttp/errors.rb
|
24
|
+
- lib/braintreehttp/http_client.rb
|
25
|
+
- lib/braintreehttp/injector.rb
|
26
|
+
- spec/braintreehttp/encoder_spec.rb
|
27
|
+
- spec/braintreehttp/http_client_spec.rb
|
28
|
+
- spec/spec_helper.rb
|
29
|
+
homepage: http://www.braintreepayments.com/
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project: braintreehttp
|
49
|
+
rubygems_version: 2.2.2
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: BraintreeHttp Client Library
|
53
|
+
test_files: []
|