Skip to content

Commit

Permalink
Create more fine-grained errors and warnings for content type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ldodds committed Jan 13, 2014
1 parent 1deedcf commit 02ce93d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
Binary file added features/fixtures/spreadsheet.xls
Binary file not shown.
33 changes: 32 additions & 1 deletion features/validation_errors.feature
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,35 @@ Feature: Get validation errors
And I set an encoding header of "ISO-8859-1"
And it is stored at the url "http://example.com/example1.csv"
When I ask if there are errors
Then there should be 0 error
Then there should be 0 error
Scenario: Report invalid file
Given I have a CSV file called "spreadsheet.xls"
And it is stored at the url "http://example.com/example1.csv"
When I ask if there are errors
Then there should be 1 error
And that error should have the type "invalid_encoding"
Scenario: Incorrect content type
Given I have a CSV with the following content:
"""
"abc","2","3"
"""
And the content type is set to "application/excel"
And it is stored at the url "http://example.com/example1.xls"
And I ask if there are errors
Then there should be 1 error
And that error should have the type "wrong_content_type"
Scenario: Incorrect extension
Given I have a CSV with the following content:
"""
"abc","2","3"
"""
And the content type is set to "application/excel"
And it is stored at the url "http://example.com/example1.csv"
And I ask if there are errors
Then there should be 1 error
And that error should have the type "wrong_content_type"
33 changes: 0 additions & 33 deletions features/validation_warnings.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,6 @@ Feature: Validation warnings
And it is stored at the url "http://example.com/example1.csv"
And I ask if there are warnings
Then there should be 0 warnings

Scenario: Incorrect content type
Given I have a CSV with the following content:
"""
"abc","2","3"
"""
And the content type is set to "application/excel"
And it is stored at the url "http://example.com/example1.xls"
And I ask if there are warnings
Then there should be 1 warnings
And that warning should have the type "content_type"

Scenario: Incorrect extension
Given I have a CSV with the following content:
"""
"abc","2","3"
"""
And the content type is set to "text/csv"
And it is stored at the url "http://example.com/example1.xls"
And I ask if there are warnings
Then there should be 1 warnings
And that warning should have the type "extension"

Scenario: Incorrect extension
Given I have a CSV with the following content:
"""
"abc","2","3"
"""
And the content type is set to "application/excel"
And it is stored at the url "http://example.com/example1.csv"
And I ask if there are warnings
Then there should be 2 warnings
And that warning should have the type "extension"

Scenario: No extension
Given I have a CSV with the following content:
Expand Down
8 changes: 3 additions & 5 deletions lib/csvlint/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ def validate
@encoding = s.charset rescue nil
@content_type = s.content_type rescue nil
@headers = s.meta
mime_types = MIME::Types.type_for(@url)
if mime_types.count > 0 && mime_types.select { |m| @content_type == m.content_type }.count == 0
build_warnings(:extension, nil)
end
if @headers["content-type"] !~ /charset=/
build_warnings(:no_encoding, nil)
else
build_warnings(:encoding, nil) if @encoding != "utf-8"
end
build_warnings(:content_type, nil) unless @content_type =~ /text\/csv/
build_warnings(:no_content_type, nil) if @content_type == nil
build_warnings(:excel, nil) if @content_type == nil && @extension =~ /.xls(x)?/
build_errors(:wrong_content_type, nil) unless (@content_type && @content_type =~ /text\/csv/)
s.each_line(@line_terminator) do |line|
begin
current_line = current_line + 1
Expand Down

0 comments on commit 02ce93d

Please sign in to comment.