http 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of http might be problematic. Click here for more details.

Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +38 -93
  3. data/.travis.yml +5 -5
  4. data/.yardopts +2 -0
  5. data/CHANGES.md +21 -5
  6. data/Gemfile +12 -9
  7. data/Guardfile +3 -4
  8. data/LICENSE.txt +1 -1
  9. data/README.md +76 -53
  10. data/Rakefile +1 -1
  11. data/examples/parallel_requests_with_celluloid.rb +1 -1
  12. data/http.gemspec +6 -5
  13. data/lib/http.rb +0 -1
  14. data/lib/http/chainable.rb +54 -20
  15. data/lib/http/client.rb +14 -12
  16. data/lib/http/headers.rb +20 -17
  17. data/lib/http/mime_type/adapter.rb +1 -1
  18. data/lib/http/options.rb +1 -1
  19. data/lib/http/request.rb +23 -16
  20. data/lib/http/request/writer.rb +2 -7
  21. data/lib/http/response.rb +47 -76
  22. data/lib/http/response/body.rb +2 -3
  23. data/lib/http/response/status.rb +122 -0
  24. data/lib/http/response/status/reasons.rb +72 -0
  25. data/lib/http/version.rb +1 -1
  26. data/logo.png +0 -0
  27. data/spec/http/client_spec.rb +13 -47
  28. data/spec/http/content_type_spec.rb +15 -15
  29. data/spec/http/headers/mixin_spec.rb +1 -1
  30. data/spec/http/headers_spec.rb +42 -38
  31. data/spec/http/options/body_spec.rb +1 -1
  32. data/spec/http/options/form_spec.rb +1 -1
  33. data/spec/http/options/headers_spec.rb +2 -2
  34. data/spec/http/options/json_spec.rb +1 -1
  35. data/spec/http/options/merge_spec.rb +1 -1
  36. data/spec/http/options/new_spec.rb +2 -2
  37. data/spec/http/options/proxy_spec.rb +1 -1
  38. data/spec/http/options_spec.rb +1 -1
  39. data/spec/http/redirector_spec.rb +1 -1
  40. data/spec/http/request/writer_spec.rb +72 -24
  41. data/spec/http/request_spec.rb +31 -35
  42. data/spec/http/response/body_spec.rb +1 -1
  43. data/spec/http/response/status_spec.rb +139 -0
  44. data/spec/http/response_spec.rb +7 -7
  45. data/spec/http_spec.rb +41 -37
  46. data/spec/spec_helper.rb +2 -10
  47. data/spec/support/example_server.rb +14 -86
  48. data/spec/support/example_server/servlet.rb +102 -0
  49. metadata +46 -21
  50. data/lib/http/authorization_header.rb +0 -37
  51. data/lib/http/authorization_header/basic_auth.rb +0 -24
  52. data/lib/http/authorization_header/bearer_token.rb +0 -28
  53. data/lib/http/backports.rb +0 -2
  54. data/lib/http/backports/base64.rb +0 -6
  55. data/lib/http/backports/uri.rb +0 -131
  56. data/spec/http/authorization_header/basic_auth_spec.rb +0 -29
  57. data/spec/http/authorization_header/bearer_token_spec.rb +0 -36
  58. data/spec/http/authorization_header_spec.rb +0 -41
  59. data/spec/http/backports/base64_spec.rb +0 -13
  60. data/spec/http/backports/uri_spec.rb +0 -9
  61. data/spec/support/black_hole.rb +0 -5
  62. data/spec/support/create_certs.rb +0 -57
  63. data/spec/support/dummy_server.rb +0 -52
  64. data/spec/support/dummy_server/servlet.rb +0 -30
  65. data/spec/support/servers/config.rb +0 -13
  66. data/spec/support/servers/runner.rb +0 -17
@@ -11,20 +11,12 @@ if RUBY_VERSION >= '1.9'
11
11
  end
12
12
 
13
13
  require 'http'
14
+ require 'rspec/its'
14
15
  require 'support/example_server'
15
16
  require 'support/proxy_server'
16
17
 
17
- # Allow testing against a SSL server
18
- def certs_dir
19
- Pathname.new File.expand_path('../../tmp/certs', __FILE__)
20
- end
21
-
22
- require 'support/create_certs'
23
-
24
18
  RSpec.configure do |config|
25
- config.expect_with :rspec do |c|
26
- c.syntax = :expect
27
- end
19
+ config.disable_monkey_patching!
28
20
  end
29
21
 
30
22
  def capture_warning
@@ -1,101 +1,29 @@
1
1
  require 'webrick'
2
+ require 'singleton'
3
+ require 'forwardable'
2
4
 
3
- class ExampleService < WEBrick::HTTPServlet::AbstractServlet
4
- PORT = 65432 # rubocop:disable NumericLiterals
5
+ require 'support/example_server/servlet'
5
6
 
6
- def do_GET(request, response) # rubocop:disable MethodName
7
- case request.path
8
- when '/'
9
- handle_root(request, response)
10
- when '/params'
11
- handle_params(request, response)
12
- when '/multiple-params'
13
- handle_multiple_params(request, response)
14
- when '/proxy'
15
- response.status = 200
16
- response.body = 'Proxy!'
17
- when '/not-found'
18
- response.body = 'not found'
19
- response.status = 404
20
- when '/redirect-301'
21
- response.status = 301
22
- response['Location'] = "http://127.0.0.1:#{PORT}/"
23
- when '/redirect-302'
24
- response.status = 302
25
- response['Location'] = "http://127.0.0.1:#{PORT}/"
26
- else
27
- response.status = 404
28
- end
29
- end
7
+ class ExampleServer
8
+ extend Forwardable
30
9
 
31
- def handle_root(request, response)
32
- response.status = 200
33
- case request['Accept']
34
- when 'application/json'
35
- response['Content-Type'] = 'application/json'
36
- response.body = '{"json": true}'
37
- else
38
- response['Content-Type'] = 'text/html'
39
- response.body = '<!doctype html>'
40
- end
41
- end
10
+ include Singleton
42
11
 
43
- def handle_params(request, response)
44
- return unless request.query_string == 'foo=bar'
12
+ PORT = 65_432
13
+ ADDR = "127.0.0.1:#{PORT}"
45
14
 
46
- response.status = 200
47
- response.body = 'Params!'
15
+ def initialize
16
+ @server = WEBrick::HTTPServer.new :Port => PORT, :AccessLog => []
17
+ @server.mount '/', Servlet
48
18
  end
49
19
 
50
- def handle_multiple_params(request, response)
51
- params = CGI.parse(request.query_string)
52
-
53
- return unless params == {'foo' => ['bar'], 'baz' => ['quux']}
54
-
55
- response.status = 200
56
- response.body = 'More Params!'
57
- end
58
-
59
- def do_POST(request, response) # rubocop:disable MethodName
60
- case request.path
61
- when '/form'
62
- if request.query['example'] == 'testing-form'
63
- response.status = 200
64
- response.body = 'passed :)'
65
- else
66
- response.status = 400
67
- response.body = 'invalid! >:E'
68
- end
69
- when '/body'
70
- if request.body == 'testing-body'
71
- response.status = 200
72
- response.body = 'passed :)'
73
- else
74
- response.status = 400
75
- response.body = 'invalid! >:E'
76
- end
77
- else
78
- response.status = 404
79
- end
80
- end
81
-
82
- def do_HEAD(request, response) # rubocop:disable MethodName
83
- case request.path
84
- when '/'
85
- response.status = 200
86
- response['Content-Type'] = 'text/html'
87
- else
88
- response.status = 404
89
- end
90
- end
20
+ delegate [:start, :shutdown] => :@server
91
21
  end
92
22
 
93
- ExampleServer = WEBrick::HTTPServer.new(:Port => ExampleService::PORT, :AccessLog => [])
94
- ExampleServer.mount '/', ExampleService
23
+ t = Thread.new { ExampleServer.instance.start }
95
24
 
96
- t = Thread.new { ExampleServer.start }
97
25
  trap('INT') do
98
- ExampleServer.shutdown
26
+ ExampleServer.instance.shutdown
99
27
  exit
100
28
  end
101
29
 
@@ -0,0 +1,102 @@
1
+ require 'webrick'
2
+
3
+ class ExampleServer
4
+ class Servlet < WEBrick::HTTPServlet::AbstractServlet
5
+ def not_found(_req, res)
6
+ res.status = 404
7
+ res.body = 'Not Found'
8
+ end
9
+
10
+ def self.handlers
11
+ @handlers ||= {}
12
+ end
13
+
14
+ %w(get post head).each do |method|
15
+ class_eval <<-RUBY, __FILE__, __LINE__
16
+ def self.#{method}(path, &block)
17
+ handlers["#{method}:\#{path}"] = block
18
+ end
19
+
20
+ def do_#{method.upcase}(req, res)
21
+ handler = self.class.handlers["#{method}:\#{req.path}"]
22
+ return instance_exec(req, res, &handler) if handler
23
+ not_found
24
+ end
25
+ RUBY
26
+ end
27
+
28
+ get '/' do |req, res|
29
+ res.status = 200
30
+
31
+ case req['Accept']
32
+ when 'application/json'
33
+ res['Content-Type'] = 'application/json'
34
+ res.body = '{"json": true}'
35
+ else
36
+ res['Content-Type'] = 'text/html'
37
+ res.body = '<!doctype html>'
38
+ end
39
+ end
40
+
41
+ get '/params' do |req, res|
42
+ next not_found unless 'foo=bar' == req.query_string
43
+
44
+ res.status = 200
45
+ res.body = 'Params!'
46
+ end
47
+
48
+ get '/multiple-params' do |req, res|
49
+ params = CGI.parse req.query_string
50
+
51
+ next not_found unless {'foo' => ['bar'], 'baz' => ['quux']} == params
52
+
53
+ res.status = 200
54
+ res.body = 'More Params!'
55
+ end
56
+
57
+ get '/proxy' do |_req, res|
58
+ res.status = 200
59
+ res.body = 'Proxy!'
60
+ end
61
+
62
+ get '/not-found' do |_req, res|
63
+ res.status = 404
64
+ res.body = 'not found'
65
+ end
66
+
67
+ get '/redirect-301' do |_req, res|
68
+ res.status = 301
69
+ res['Location'] = "http://#{ExampleServer::ADDR}/"
70
+ end
71
+
72
+ get '/redirect-302' do |_req, res|
73
+ res.status = 302
74
+ res['Location'] = "http://#{ExampleServer::ADDR}/"
75
+ end
76
+
77
+ post '/form' do |req, res|
78
+ if 'testing-form' == req.query['example']
79
+ res.status = 200
80
+ res.body = 'passed :)'
81
+ else
82
+ res.status = 400
83
+ res.body = 'invalid! >:E'
84
+ end
85
+ end
86
+
87
+ post '/body' do |req, res|
88
+ if 'testing-body' == req.body
89
+ res.status = 200
90
+ res.body = 'passed :)'
91
+ else
92
+ res.status = 400
93
+ res.body = 'invalid! >:E'
94
+ end
95
+ end
96
+
97
+ head '/' do |_req, res|
98
+ res.status = 200
99
+ res['Content-Type'] = 'text/html'
100
+ end
101
+ end
102
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-25 00:00:00.000000000 Z
12
+ date: 2015-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: http_parser.rb
@@ -25,6 +25,20 @@ dependencies:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
27
  version: 0.6.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: form_data
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.0.1
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.0.1
28
42
  - !ruby/object:Gem::Dependency
29
43
  name: bundler
30
44
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,7 @@ files:
52
66
  - ".rspec"
53
67
  - ".rubocop.yml"
54
68
  - ".travis.yml"
69
+ - ".yardopts"
55
70
  - CHANGES.md
56
71
  - Gemfile
57
72
  - Guardfile
@@ -61,12 +76,6 @@ files:
61
76
  - examples/parallel_requests_with_celluloid.rb
62
77
  - http.gemspec
63
78
  - lib/http.rb
64
- - lib/http/authorization_header.rb
65
- - lib/http/authorization_header/basic_auth.rb
66
- - lib/http/authorization_header/bearer_token.rb
67
- - lib/http/backports.rb
68
- - lib/http/backports/base64.rb
69
- - lib/http/backports/uri.rb
70
79
  - lib/http/chainable.rb
71
80
  - lib/http/client.rb
72
81
  - lib/http/content_type.rb
@@ -83,13 +92,10 @@ files:
83
92
  - lib/http/response.rb
84
93
  - lib/http/response/body.rb
85
94
  - lib/http/response/parser.rb
95
+ - lib/http/response/status.rb
96
+ - lib/http/response/status/reasons.rb
86
97
  - lib/http/version.rb
87
98
  - logo.png
88
- - spec/http/authorization_header/basic_auth_spec.rb
89
- - spec/http/authorization_header/bearer_token_spec.rb
90
- - spec/http/authorization_header_spec.rb
91
- - spec/http/backports/base64_spec.rb
92
- - spec/http/backports/uri_spec.rb
93
99
  - spec/http/client_spec.rb
94
100
  - spec/http/content_type_spec.rb
95
101
  - spec/http/headers/mixin_spec.rb
@@ -106,18 +112,14 @@ files:
106
112
  - spec/http/request/writer_spec.rb
107
113
  - spec/http/request_spec.rb
108
114
  - spec/http/response/body_spec.rb
115
+ - spec/http/response/status_spec.rb
109
116
  - spec/http/response_spec.rb
110
117
  - spec/http_spec.rb
111
118
  - spec/spec_helper.rb
112
- - spec/support/black_hole.rb
113
- - spec/support/create_certs.rb
114
- - spec/support/dummy_server.rb
115
- - spec/support/dummy_server/servlet.rb
116
119
  - spec/support/example_server.rb
120
+ - spec/support/example_server/servlet.rb
117
121
  - spec/support/proxy_server.rb
118
- - spec/support/servers/config.rb
119
- - spec/support/servers/runner.rb
120
- homepage: https://github.com/tarcieri/http
122
+ homepage: https://github.com/tarcieri/http.rb
121
123
  licenses:
122
124
  - MIT
123
125
  metadata: {}
@@ -141,5 +143,28 @@ rubygems_version: 2.2.2
141
143
  signing_key:
142
144
  specification_version: 4
143
145
  summary: HTTP should be easy
144
- test_files: []
146
+ test_files:
147
+ - spec/http/client_spec.rb
148
+ - spec/http/content_type_spec.rb
149
+ - spec/http/headers/mixin_spec.rb
150
+ - spec/http/headers_spec.rb
151
+ - spec/http/options/body_spec.rb
152
+ - spec/http/options/form_spec.rb
153
+ - spec/http/options/headers_spec.rb
154
+ - spec/http/options/json_spec.rb
155
+ - spec/http/options/merge_spec.rb
156
+ - spec/http/options/new_spec.rb
157
+ - spec/http/options/proxy_spec.rb
158
+ - spec/http/options_spec.rb
159
+ - spec/http/redirector_spec.rb
160
+ - spec/http/request/writer_spec.rb
161
+ - spec/http/request_spec.rb
162
+ - spec/http/response/body_spec.rb
163
+ - spec/http/response/status_spec.rb
164
+ - spec/http/response_spec.rb
165
+ - spec/http_spec.rb
166
+ - spec/spec_helper.rb
167
+ - spec/support/example_server.rb
168
+ - spec/support/example_server/servlet.rb
169
+ - spec/support/proxy_server.rb
145
170
  has_rdoc:
@@ -1,37 +0,0 @@
1
- module HTTP
2
- # Authorization header value builders
3
- module AuthorizationHeader
4
- class << self
5
- # Associate type with given builder.
6
- # @param [#to_sym] type
7
- # @param [Class] klass
8
- # @return [void]
9
- def register(type, klass)
10
- builders[type.to_sym] = klass
11
- end
12
-
13
- # Builds Authorization header value with associated builder.
14
- # @param [#to_sym] type
15
- # @param [Object] opts
16
- # @return [String]
17
- def build(type, opts)
18
- klass = builders[type.to_sym]
19
-
20
- fail Error, "Unknown authorization type #{type}" unless klass
21
-
22
- klass.new opts
23
- end
24
-
25
- private
26
-
27
- # :nodoc:
28
- def builders
29
- @builders ||= {}
30
- end
31
- end
32
- end
33
- end
34
-
35
- # built-in builders
36
- require 'http/authorization_header/basic_auth'
37
- require 'http/authorization_header/bearer_token'
@@ -1,24 +0,0 @@
1
- require 'base64'
2
-
3
- module HTTP
4
- module AuthorizationHeader
5
- # Basic authorization header builder
6
- # @see http://tools.ietf.org/html/rfc2617
7
- class BasicAuth
8
- # @param [#fetch] opts
9
- # @option opts [#to_s] :user
10
- # @option opts [#to_s] :pass
11
- def initialize(opts)
12
- @user = opts.fetch :user
13
- @pass = opts.fetch :pass
14
- end
15
-
16
- # :nodoc:
17
- def to_s
18
- 'Basic ' << Base64.strict_encode64("#{@user}:#{@pass}")
19
- end
20
- end
21
-
22
- register :basic, BasicAuth
23
- end
24
- end
@@ -1,28 +0,0 @@
1
- require 'base64'
2
-
3
- module HTTP
4
- module AuthorizationHeader
5
- # OAuth2 Bearer token authorization header builder
6
- # @see http://tools.ietf.org/html/rfc6750
7
- #
8
- # @deprecated Will be remove in v0.7.0
9
- class BearerToken
10
- # @param [#fetch] opts
11
- # @option opts [#to_s] :token
12
- # @option opts [Boolean] :encode (false) deprecated
13
- def initialize(opts)
14
- warn "#{Kernel.caller.first}: [DEPRECATION] BearerToken deprecated."
15
-
16
- @token = opts.fetch :token
17
- @token = Base64.strict_encode64 @token if opts.fetch(:encode, false)
18
- end
19
-
20
- # :nodoc:
21
- def to_s
22
- "Bearer #{@token}"
23
- end
24
- end
25
-
26
- register :bearer, BearerToken
27
- end
28
- end