http_content_type 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 423abcfd36ea5de5ddb56ef406aa65d20c51a2e3
4
- data.tar.gz: 41ce1d5652424ebd802464a109a42e8e9e463fbd
3
+ metadata.gz: 48e54ccadb5f2a3be4ffb565490d947e6f187268
4
+ data.tar.gz: 58e4f06e28abe976cdd72733858b3fc1b043137e
5
5
  SHA512:
6
- metadata.gz: a35dbb8bc55e95a17683f25167798a9090b81cc58c63aab0c8b139628e0ad0a8e71ce939d742a81779ce9ee2a750b54dffa7a28bbb10ec1aabc1d1dfb358fa29
7
- data.tar.gz: 0921e556085c0d2428678114eab56c93cdcad58257649ae19efebf72b87e0ca1045ac18fa235d44bc0377aea2dafb44bcd02ca4e22774a52a4b53a04f1f30fbf
6
+ metadata.gz: 280f74d2bf0e83856be5aadafe41a6acfdac65406d05a9d33819321fb14b62395a63cc183d7de215a692d9e74bc7f17e81f082108dc519bd35fff2b41a8a3d74
7
+ data.tar.gz: 50782d036ee864a6f68bd0a07e32c29e4e67c95e31dacc6defbd8c3055e7bc70753335b96238e1b415e1eb7e6f4df80cc6322a960d998919a1478653acadc2c3
@@ -2,6 +2,11 @@
2
2
 
3
3
  No changes.
4
4
 
5
+ ### 0.0.3 - 5 September, 2013 - ([@rymai][])
6
+
7
+ - Fix issues due to specs focused in the CI!
8
+ - Removed the `TooManyRedirections` exception.
9
+
5
10
  ### 0.0.2 - 5 September, 2013 - ([@rymai][])
6
11
 
7
12
  - Handle redirections & url with query params.
data/README.md CHANGED
@@ -3,6 +3,8 @@
3
3
 
4
4
  This gem allows you to check the Content-Type of any asset accessible via HTTP.
5
5
 
6
+ Compatible with Ruby >= 1.9, JRuby (1.9 mode) or Rubinius (1.9 mode).
7
+
6
8
  ## Installation
7
9
 
8
10
  Add this line to your application's Gemfile:
@@ -27,8 +29,30 @@ $ gem install http_content_type
27
29
  ```ruby
28
30
  checker = HttpContentType::Checker.new('http://domain.com/video.mp4')
29
31
 
30
- checker.found? # => true (asset doesn't return a 404)
31
- checker.expected_content_type # => 'video/mp4' (the expected content type is based on file extension)
32
+ checker.error? # => false (true if there was an error requesting
33
+ # the asset)
34
+ checker.found? # => true (the asset was found, i.e. request
35
+ # returned an `Net::HTTPSuccess` response)
36
+ checker.expected_content_type # => 'video/mp4' (the expected content type is
37
+ # based on file extension but can also be
38
+ # hardcoded with the :expected_content_type option)
39
+ checker.content_type # => 'video/mp4'
40
+ checker.valid_content_type? # => true
41
+ ```
42
+
43
+ ### Options
44
+
45
+ #### `:timeout`
46
+
47
+ ```ruby
48
+ checker = HttpContentType::Checker.new('http://domain.com/video.mp4', timeout: 10) # in seconds
49
+ ```
50
+
51
+ #### `:expected_content_type`
52
+
53
+ ```ruby
54
+ checker = HttpContentType::Checker.new('http://domain.com/dynamic_asset.php', expected_content_type: 'video/mp4')
55
+ checker.expected_content_type # => 'video/mp4'
32
56
  checker.content_type # => 'video/mp4'
33
57
  checker.valid_content_type? # => true
34
58
  ```
@@ -3,7 +3,6 @@ require 'net/http'
3
3
  module HttpContentType
4
4
 
5
5
  class Checker
6
- class TooManyRedirections < Exception; end
7
6
 
8
7
  DEFAULT_OPTIONS = {
9
8
  timeout: 5
@@ -27,9 +26,8 @@ module HttpContentType
27
26
  end
28
27
 
29
28
  # Returns true if there was an error requesting the asset. Most common
30
- # errors are `HTTPClientError`, `HTTPServerError`, `HTTPUnknownResponse`,
31
- # `HttpContentType::TooManyRedirections`. Note that any other (less common)
32
- # exceptions are catched as well.
29
+ # errors are `HTTPClientError`, `HTTPServerError`, `HTTPUnknownResponse`.
30
+ # Note that any other (less common) exceptions are catched as well.
33
31
  #
34
32
  # @return [Boolean] whether or not there was an error while requesting the
35
33
  # asset.
@@ -105,9 +103,10 @@ module HttpContentType
105
103
  end
106
104
 
107
105
  def _fetch(url, limit = 10)
108
- raise TooManyRedirections if limit == 0
109
-
110
106
  uri ||= URI.parse(URI.escape(url))
107
+
108
+ return _other_error_response(uri, nil, error: 'Too many redirections') if limit == 0
109
+
111
110
  @last_response = Net::HTTP.start(uri.host, uri.port, _connection_options(uri)) do |http|
112
111
  req = Net::HTTP::Head.new(uri.request_uri)
113
112
  http.request(req)
@@ -115,30 +114,30 @@ module HttpContentType
115
114
 
116
115
  case @last_response
117
116
  when Net::HTTPSuccess
118
- _found_response(uri, @last_response)
117
+ _success_response(uri, @last_response)
119
118
  when Net::HTTPRedirection
120
119
  _fetch(@last_response['location'], limit - 1)
121
- when HTTPClientError
122
- _not_found_response(uri, @last_response)
120
+ when Net::HTTPClientError
121
+ _client_error_response(uri, @last_response)
123
122
  else
124
- _errored_response(uri, @last_response)
123
+ _other_error_response(uri, @last_response)
125
124
  end
126
125
 
127
126
  rescue => ex
128
127
  puts "Exception from HttpContentType::Checker#_fetch('#{uri}', #{limit}):"
129
128
  puts ex
130
- _errored_response(@last_response, error: ex)
129
+ _other_error_response(uri, @last_response, error: ex)
131
130
  end
132
131
 
133
- def _found_response(uri, http_response)
132
+ def _success_response(uri, http_response)
134
133
  _base_response(uri).merge(found: true, error: nil, content_type: http_response['content-type'])
135
134
  end
136
135
 
137
- def _not_found_response(uri, http_response)
136
+ def _client_error_response(uri, http_response)
138
137
  _base_response(uri).merge(found: false, error: nil, content_type: 'unknown')
139
138
  end
140
139
 
141
- def _errored_response(uri, http_response, opts = {})
140
+ def _other_error_response(uri, http_response, opts = {})
142
141
  error = opts[:error] || "#{http_response.code}: #{http_response.message}"
143
142
 
144
143
  _base_response(uri).merge(found: false, error: error, content_type: 'unknown')
@@ -1,3 +1,3 @@
1
1
  module HttpContentType
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -130,24 +130,60 @@ describe HttpContentType::Checker do
130
130
  it { be_valid_content_type }
131
131
  end
132
132
 
133
- context 'asset cannot be found and do not return an error', :focus do
133
+ context 'asset cannot be found and do not return an error' do
134
134
  before { checker.stub(:_head).and_return({ uri: URI('http://foo.com/bar.mp4'), found: false, error: nil }) }
135
135
  it { be_valid_content_type }
136
136
  end
137
137
 
138
- context 'asset is found without an error with valid content type', :focus do
138
+ context 'asset is found without an error with valid content type' do
139
139
  before { checker.stub(:_head).and_return({ uri: URI('http://foo.com/bar.mp4'), found: true, error: nil, content_type: 'video/mp4' }) }
140
140
  it { be_valid_content_type }
141
141
  end
142
142
 
143
- context 'asset is found without an error with invalid content type', :focus do
143
+ context 'asset is found without an error with invalid content type' do
144
144
  before { checker.stub(:_head).and_return({ uri: URI('http://foo.com/bar.mov'), found: true, error: nil, content_type: 'video/mov' }) }
145
145
  it { be_valid_content_type }
146
146
  end
147
147
  end
148
148
 
149
149
  describe '#_fetch' do
150
- context 'too many redirections' do
150
+ describe 'unexpected exception' do
151
+ before { Net::HTTP.should_receive(:start) { raise 'Unexpected error!' } }
152
+
153
+ it 'returns the right response' do
154
+ res = checker.send(:_fetch, 'http://domain.com/video.mp4')
155
+
156
+ expect(res).to include({ found: false, content_type: 'unknown' })
157
+ end
158
+ end
159
+
160
+ describe 'HTTP client error' do
161
+ let(:response) do
162
+ Net::HTTPNotFound.new('1.1', '404', 'Not found')
163
+ end
164
+ before { Net::HTTP.any_instance.stub(:request).and_return(response) }
165
+
166
+ it 'returns the right response' do
167
+ res = checker.send(:_fetch, 'http://domain.com/video.mp4')
168
+
169
+ expect(res).to include({ found: false, error: nil, content_type: 'unknown' })
170
+ end
171
+ end
172
+
173
+ describe 'other HTTP errors' do
174
+ let(:response) do
175
+ Net::HTTPServiceUnavailable.new('1.1', '503', 'The service is unavailable')
176
+ end
177
+ before { Net::HTTP.any_instance.stub(:request).and_return(response) }
178
+
179
+ it 'returns the right response' do
180
+ res = checker.send(:_fetch, 'http://domain.com/video.mp4')
181
+
182
+ expect(res).to include({ found: false, error: '503: The service is unavailable', content_type: 'unknown' })
183
+ end
184
+ end
185
+
186
+ describe 'too many redirections' do
151
187
  let(:response) do
152
188
  Net::HTTPRedirection.new('1.1', 302, '').tap do |res|
153
189
  res['content-type'] = 'foo/bar'
@@ -156,8 +192,10 @@ describe HttpContentType::Checker do
156
192
  end
157
193
  before { Net::HTTP.any_instance.stub(:request).and_return(response) }
158
194
 
159
- it 'raise a TooManyRedirections exception' do
160
- expect { checker.send(:_fetch, 'http://domain.com/video.mp4') }.to raise_error(HttpContentType::TooManyRedirections)
195
+ it 'returns the right response' do
196
+ res = checker.send(:_fetch, 'http://domain.com/video.mp4')
197
+
198
+ expect(res).to include({ found: false, error: 'Too many redirections', content_type: 'unknown' })
161
199
  end
162
200
  end
163
201
  end
@@ -8,7 +8,7 @@ require 'rspec'
8
8
  RSpec.configure do |config|
9
9
  config.color_enabled = true
10
10
  config.order = :random
11
- config.filter_run :focus => true
11
+ config.filter_run focus: ENV['CI'] != 'true'
12
12
  config.treat_symbols_as_metadata_keys_with_true_values = true
13
13
  config.run_all_when_everything_filtered = true
14
14
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_content_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémy Coutable