braintreehttp 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/braintreehttp/encoder.rb +21 -1
- data/lib/braintreehttp/version.rb +1 -1
- data/spec/braintreehttp/encoder_spec.rb +18 -2
- data/spec/braintreehttp/http_client_spec.rb +20 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f9eecbaee9baceb441762bc358a9fdd70ff6775
|
4
|
+
data.tar.gz: 8cca413a46d85a6f02d4208ced9aff63a0473bef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1231f2845245b696eea965a638bba671d9b1b342968e7855b705b053e90b00e7edb66b97528ecea0d17671ac3896b4a68f5c5e3f814deb3242160518f6a27656
|
7
|
+
data.tar.gz: 099178fdfadd1fd5ef8d01b01ae6df470bc5ca1be55234ce6816b4de02e6e659b42acb0dfd30523778e2c8edc9ab205bdcbd374520f737f7a4c44585007c6cce
|
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'uri'
|
2
3
|
|
3
4
|
module BraintreeHttp
|
4
5
|
class Encoder
|
5
6
|
def initialize
|
6
|
-
@encoders = [Json.new, Text.new, Multipart.new]
|
7
|
+
@encoders = [Json.new, Text.new, Multipart.new, FormEncoded.new]
|
7
8
|
end
|
8
9
|
|
9
10
|
def serialize_request(req)
|
@@ -69,6 +70,25 @@ module BraintreeHttp
|
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
73
|
+
class FormEncoded
|
74
|
+
def encode(request)
|
75
|
+
encoded_params = []
|
76
|
+
request.body.each do |k, v|
|
77
|
+
encoded_params.push("#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}")
|
78
|
+
end
|
79
|
+
|
80
|
+
encoded_params.join("&")
|
81
|
+
end
|
82
|
+
|
83
|
+
def decode(body)
|
84
|
+
raise UnsupportedEncodingError.new("FormEncoded does not support deserialization")
|
85
|
+
end
|
86
|
+
|
87
|
+
def content_type
|
88
|
+
/^application\/x-www-form-urlencoded/
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
72
92
|
class Multipart
|
73
93
|
def encode(request)
|
74
94
|
boundary = DateTime.now.strftime("%Q")
|
@@ -1 +1 @@
|
|
1
|
-
VERSION = "0.
|
1
|
+
VERSION = "0.3.0"
|
@@ -37,7 +37,6 @@ describe Encoder do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'serializes the request when content-type == multipart/form-data' do
|
40
|
-
http_client = HttpClient.new(@environment)
|
41
40
|
file = File.new("README.md", "r")
|
42
41
|
req = OpenStruct.new({
|
43
42
|
:verb => "POST",
|
@@ -48,7 +47,7 @@ describe Encoder do
|
|
48
47
|
:body => {
|
49
48
|
:key => "value",
|
50
49
|
:another_key => 1013,
|
51
|
-
:readme => file
|
50
|
+
:readme => file
|
52
51
|
}
|
53
52
|
})
|
54
53
|
|
@@ -62,6 +61,23 @@ describe Encoder do
|
|
62
61
|
expect(serialized).to include("1013")
|
63
62
|
end
|
64
63
|
|
64
|
+
it 'serializes the request when content-type == application/x-www-form-urlencoded' do
|
65
|
+
req = OpenStruct.new({
|
66
|
+
:verb => "POST",
|
67
|
+
:path => "/v1/api",
|
68
|
+
:headers => {
|
69
|
+
"Content-Type" => "application/x-www-form-urlencoded"
|
70
|
+
},
|
71
|
+
:body => {
|
72
|
+
:key => "value with a space",
|
73
|
+
:another_key => 1013,
|
74
|
+
}
|
75
|
+
})
|
76
|
+
serialized = Encoder.new.serialize_request(req)
|
77
|
+
|
78
|
+
expect(serialized).to eq("key=value%20with%20a%20space&another_key=1013")
|
79
|
+
end
|
80
|
+
|
65
81
|
it 'throws when content-type is not application/json' do
|
66
82
|
req = OpenStruct.new({
|
67
83
|
:headers => {
|
@@ -26,6 +26,26 @@ describe HttpClient do
|
|
26
26
|
expect(req.headers["Some-Key"]).to eq("Some Value")
|
27
27
|
end
|
28
28
|
|
29
|
+
it "uses method injector to modify request" do
|
30
|
+
WebMock.enable!
|
31
|
+
|
32
|
+
http_client = HttpClient.new(@environment)
|
33
|
+
|
34
|
+
def _inj(req)
|
35
|
+
req.headers["Some-Key"] = "Some Value"
|
36
|
+
end
|
37
|
+
|
38
|
+
http_client.add_injector(&method(:_inj))
|
39
|
+
|
40
|
+
req = OpenStruct.new({:verb => "GET", :path => "/"})
|
41
|
+
|
42
|
+
stub_request(:any, @environment.base_url)
|
43
|
+
|
44
|
+
http_client.execute(req)
|
45
|
+
|
46
|
+
expect(req.headers["Some-Key"]).to eq("Some Value")
|
47
|
+
end
|
48
|
+
|
29
49
|
it "sets User-Agent header in request if not set" do
|
30
50
|
WebMock.enable!
|
31
51
|
|