Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module HTTP | ||
ContentType = Struct.new(:mime_type, :charset) do | ||
MIME_TYPE_RE = %r{^([^/]+/[^;]+)(?:$|;)} | ||
CHARSET_RE = /;\s*charset=([^;]+)/i | ||
|
||
class << self | ||
# Parse string and return ContentType struct | ||
def parse(str) | ||
new mime_type(str), charset(str) | ||
end | ||
|
||
private | ||
|
||
# :nodoc: | ||
def mime_type(str) | ||
md = str.to_s.match MIME_TYPE_RE | ||
md && md[1].to_s.strip.downcase | ||
end | ||
|
||
# :nodoc: | ||
def charset(str) | ||
md = str.to_s.match CHARSET_RE | ||
md && md[1].to_s.strip.gsub(/^"|"$/, '') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'spec_helper' | ||
|
||
describe HTTP::ContentType do | ||
describe '.parse' do | ||
context 'with text/plain' do | ||
subject { described_class.parse 'text/plain' } | ||
its(:mime_type) { should eq 'text/plain' } | ||
its(:charset) { should be_nil } | ||
end | ||
|
||
context 'with tEXT/plaIN' do | ||
subject { described_class.parse 'tEXT/plaIN' } | ||
its(:mime_type) { should eq 'text/plain' } | ||
its(:charset) { should be_nil } | ||
end | ||
|
||
context 'with text/plain; charset=utf-8' do | ||
subject { described_class.parse 'text/plain; charset=utf-8' } | ||
its(:mime_type) { should eq 'text/plain' } | ||
its(:charset) { should eq 'utf-8' } | ||
end | ||
|
||
context 'with text/plain; charset="utf-8"' do | ||
subject { described_class.parse 'text/plain; charset="utf-8"' } | ||
its(:mime_type) { should eq 'text/plain' } | ||
its(:charset) { should eq 'utf-8' } | ||
end | ||
|
||
context 'with text/plain; charSET=utf-8' do | ||
subject { described_class.parse 'text/plain; charSET=utf-8' } | ||
its(:mime_type) { should eq 'text/plain' } | ||
its(:charset) { should eq 'utf-8' } | ||
end | ||
|
||
context 'with text/plain; foo=bar; charset=utf-8' do | ||
subject { described_class.parse 'text/plain; foo=bar; charset=utf-8' } | ||
its(:mime_type) { should eq 'text/plain' } | ||
its(:charset) { should eq 'utf-8' } | ||
end | ||
|
||
context 'with text/plain;charset=utf-8;foo=bar' do | ||
subject { described_class.parse 'text/plain;charset=utf-8;foo=bar' } | ||
its(:mime_type) { should eq 'text/plain' } | ||
its(:charset) { should eq 'utf-8' } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters