rest-builder 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,205 @@
1
+
2
+ require 'rest-builder/test'
3
+
4
+ describe RestBuilder::Payload do
5
+ describe 'A regular Payload' do
6
+ would 'use standard enctype as default content-type' do
7
+ RestBuilder::Payload::UrlEncoded.new({}).headers['Content-Type'].
8
+ should.eq 'application/x-www-form-urlencoded'
9
+ end
10
+
11
+ would 'form properly encoded params' do
12
+ RestBuilder::Payload::UrlEncoded.new(:foo => 'bar').read.
13
+ should.eq 'foo=bar'
14
+ RestBuilder::Payload::UrlEncoded.new(:foo => 'bar', :baz => 'qux').read.
15
+ should.eq 'baz=qux&foo=bar'
16
+ end
17
+
18
+ would 'escape parameters' do
19
+ RestBuilder::Payload::UrlEncoded.new('foo ' => 'bar').read.
20
+ should.eq 'foo%20=bar'
21
+ end
22
+
23
+ would 'properly handle arrays as repeated parameters' do
24
+ RestBuilder::Payload::UrlEncoded.new(:foo => ['bar']).read.
25
+ should.eq 'foo=bar'
26
+ RestBuilder::Payload::UrlEncoded.new(:foo => ['bar', 'baz']).read.
27
+ should.eq 'foo=bar&foo=baz'
28
+ end
29
+
30
+ would 'not close if stream already closed' do
31
+ p = RestBuilder::Payload::UrlEncoded.new('foo ' => 'bar')
32
+ p.close
33
+ 2.times{ p.close.should.eq nil }
34
+ end
35
+ end
36
+
37
+ describe 'A multipart Payload' do
38
+ would 'use standard enctype as default content-type' do
39
+ p = RestBuilder::Payload::Multipart.new({})
40
+ stub(p).boundary{123}
41
+ p.headers['Content-Type'].should.eq 'multipart/form-data; boundary=123'
42
+ end
43
+
44
+ would 'not error on close if stream already closed' do
45
+ p = RestBuilder::Payload::Multipart.new(:file => File.open(__FILE__))
46
+ p.close
47
+ 2.times{ p.close.should.eq nil }
48
+ end
49
+
50
+ would 'form properly separated multipart data' do
51
+ p = RestBuilder::Payload::Multipart.new(:bar => 'baz', :foo => 'bar')
52
+ p.read.should.eq <<-EOS
53
+ --#{p.boundary}\r
54
+ Content-Disposition: form-data; name="bar"\r
55
+ \r
56
+ baz\r
57
+ --#{p.boundary}\r
58
+ Content-Disposition: form-data; name="foo"\r
59
+ \r
60
+ bar\r
61
+ --#{p.boundary}--\r
62
+ EOS
63
+ end
64
+
65
+ would 'form multiple files with the same name' do
66
+ with_img do |f, n|
67
+ with_img do |ff, nn|
68
+ p = RestBuilder::Payload::Multipart.new(:foo => [f, ff])
69
+ p.read.should.eq <<-EOS
70
+ --#{p.boundary}\r
71
+ Content-Disposition: form-data; name="foo"; filename="#{n}"\r
72
+ Content-Type: image/jpeg\r
73
+ \r
74
+ #{'a'*10}\r
75
+ --#{p.boundary}\r
76
+ Content-Disposition: form-data; name="foo"; filename="#{nn}"\r
77
+ Content-Type: image/jpeg\r
78
+ \r
79
+ #{'a'*10}\r
80
+ --#{p.boundary}--\r
81
+ EOS
82
+ end
83
+ end
84
+ end
85
+
86
+ would 'not escape parameters names' do
87
+ p = RestBuilder::Payload::Multipart.new('bar ' => 'baz')
88
+ p.read.should.eq <<-EOS
89
+ --#{p.boundary}\r
90
+ Content-Disposition: form-data; name="bar "\r
91
+ \r
92
+ baz\r
93
+ --#{p.boundary}--\r
94
+ EOS
95
+ end
96
+
97
+ would 'form properly separated multipart data' do
98
+ with_img do |f, n|
99
+ p = RestBuilder::Payload::Multipart.new(:foo => f)
100
+ p.read.should.eq <<-EOS
101
+ --#{p.boundary}\r
102
+ Content-Disposition: form-data; name="foo"; filename="#{n}"\r
103
+ Content-Type: image/jpeg\r
104
+ \r
105
+ #{File.read(f.path)}\r
106
+ --#{p.boundary}--\r
107
+ EOS
108
+ end
109
+ end
110
+
111
+ would "ignore the name attribute when it's not set" do
112
+ with_img do |f, n|
113
+ p = RestBuilder::Payload::Multipart.new(nil => f)
114
+ p.read.should.eq <<-EOS
115
+ --#{p.boundary}\r
116
+ Content-Disposition: form-data; filename="#{n}"\r
117
+ Content-Type: image/jpeg\r
118
+ \r
119
+ #{File.read(f.path)}\r
120
+ --#{p.boundary}--\r
121
+ EOS
122
+ end
123
+ end
124
+
125
+ would 'detect optional (original) content type and filename' do
126
+ File.open(__FILE__) do |f|
127
+ def f.content_type ; 'image/jpeg'; end
128
+ def f.original_filename; 'foo.txt' ; end
129
+ p = RestBuilder::Payload::Multipart.new(:foo => f)
130
+ p.read.should.eq <<-EOS
131
+ --#{p.boundary}\r
132
+ Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
133
+ Content-Type: image/jpeg\r
134
+ \r
135
+ #{File.read(f.path)}\r
136
+ --#{p.boundary}--\r
137
+ EOS
138
+ end
139
+ end
140
+ end
141
+
142
+ describe 'streamed payloads' do
143
+ would 'properly determine the size of file payloads' do
144
+ File.open(__FILE__) do |f|
145
+ p = RestBuilder::Payload.generate(f)
146
+ p.size.should.eq f.stat.size
147
+ end
148
+ end
149
+
150
+ would 'properly determine the size of other kinds of payloads' do
151
+ s = StringIO.new('foo')
152
+ p = RestBuilder::Payload.generate(s)
153
+ p.size.should.eq 3
154
+
155
+ begin
156
+ f = Tempfile.new('rest-core')
157
+ f.write('foo bar')
158
+ f.rewind
159
+
160
+ p = RestBuilder::Payload.generate(f)
161
+ p.size.should.eq 7
162
+ ensure
163
+ f.close!
164
+ end
165
+ end
166
+ end
167
+
168
+ describe 'Payload generation' do
169
+ would 'recognize standard urlencoded params' do
170
+ RestBuilder::Payload.generate('foo' => 'bar').should.
171
+ kind_of?(RestBuilder::Payload::UrlEncoded)
172
+ end
173
+
174
+ would 'recognize multipart params' do
175
+ File.open(__FILE__) do |f|
176
+ RestBuilder::Payload.generate('foo' => f).should.
177
+ kind_of?(RestBuilder::Payload::Multipart)
178
+ end
179
+ end
180
+
181
+ would 'return data if none of the above' do
182
+ RestBuilder::Payload.generate('data').should.
183
+ kind_of?(RestBuilder::Payload::StreamedString)
184
+ end
185
+
186
+ would 'recognize nested multipart payloads in arrays' do
187
+ File.open(__FILE__) do |f|
188
+ RestBuilder::Payload.generate('foo' => [f]).should.
189
+ kind_of?(RestBuilder::Payload::Multipart)
190
+ end
191
+ end
192
+
193
+ would 'recognize file payloads that can be streamed' do
194
+ File.open(__FILE__) do |f|
195
+ RestBuilder::Payload.generate(f).
196
+ should.kind_of?(RestBuilder::Payload::Streamed)
197
+ end
198
+ end
199
+
200
+ would 'recognize other payloads that can be streamed' do
201
+ RestBuilder::Payload.generate(StringIO.new('foo')).should.
202
+ kind_of?(RestBuilder::Payload::Streamed)
203
+ end
204
+ end
205
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest-builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Lin Jen-Shin (godfat)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: promise_pool
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httpclient
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mime-types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: |-
56
+ Modular Ruby clients interface for REST APIs.
57
+
58
+ Build your own API clients for less dependencies, less codes, less memory,
59
+ less conflicts, and run faster. Checkout [rest-core][] for pre-built
60
+ middleware and [rest-more][] for pre-built clients.
61
+
62
+ [rest-core]: https://github.com/godfat/rest-core
63
+ [rest-more]: https://github.com/godfat/rest-more
64
+ email:
65
+ - godfat (XD) godfat.org
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - ".gitignore"
71
+ - ".gitmodules"
72
+ - ".travis.yml"
73
+ - CHANGES.md
74
+ - Gemfile
75
+ - README.md
76
+ - Rakefile
77
+ - lib/rest-builder.rb
78
+ - lib/rest-builder/builder.rb
79
+ - lib/rest-builder/client.rb
80
+ - lib/rest-builder/engine.rb
81
+ - lib/rest-builder/engine/dry.rb
82
+ - lib/rest-builder/engine/http-client.rb
83
+ - lib/rest-builder/error.rb
84
+ - lib/rest-builder/event_source.rb
85
+ - lib/rest-builder/middleware.rb
86
+ - lib/rest-builder/payload.rb
87
+ - lib/rest-builder/promise.rb
88
+ - lib/rest-builder/test.rb
89
+ - lib/rest-builder/version.rb
90
+ - rest-builder.gemspec
91
+ - task/README.md
92
+ - task/gemgem.rb
93
+ - test/test_builder.rb
94
+ - test/test_client.rb
95
+ - test/test_event_source.rb
96
+ - test/test_future.rb
97
+ - test/test_httpclient.rb
98
+ - test/test_payload.rb
99
+ homepage: https://github.com/godfat/rest-builder
100
+ licenses:
101
+ - Apache License 2.0
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.5.1
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Modular Ruby clients interface for REST APIs.
123
+ test_files:
124
+ - test/test_builder.rb
125
+ - test/test_client.rb
126
+ - test/test_event_source.rb
127
+ - test/test_future.rb
128
+ - test/test_httpclient.rb
129
+ - test/test_payload.rb