Skip to content

Commit

Permalink
chupa-text: add --mime-type option to overwrite input data MIME type
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jul 10, 2017
1 parent 08ee0eb commit b0e8779
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/chupa-text/command/chupa-text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def initialize
@configuration = Configuration.load_default
@enable_gems = true
@uri = nil
@mime_type = nil
@format = :json
@need_screenshot = true
@expected_screenshot_size = [200, 200]
Expand Down Expand Up @@ -113,6 +114,10 @@ def create_option_parser
"Input data URI.") do |uri|
@uri = URI.parse(uri)
end
parser.on("--mime-type=MIME_TYPE",
"Input data MIME type.") do |mime_type|
@mime_type = mime_type
end

parser.separator("")
parser.separator("Output related options")
Expand Down Expand Up @@ -185,6 +190,7 @@ def create_extractor
end

def create_data
options = {mime_type: @mime_type}
if @input.nil?
data = VirtualFileData.new(@uri, $stdin)
else
Expand All @@ -202,6 +208,7 @@ def create_data
data = InputData.new(input)
end
end
data.mime_type = @mime_type if @mime_type
data.need_screenshot = @need_screenshot
data.expected_screenshot_size = @expected_screenshot_size
data
Expand Down
42 changes: 42 additions & 0 deletions test/command/test-chupa-text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ def teardown_www_server
],
run_command(@uri, "--uri", virtual_uri))
end

test("--mime-type") do
@html = "<html><body>Hello</body></html>"
assert_equal([
true,
{
"mime-type" => "text/plain",
"size" => @html.bytesize,
"uri" => @uri,
"texts" => [
{
"mime-type" => "text/plain",
"size" => @html.bytesize,
"uri" => @uri,
"body" => @html,
},
],
},
],
run_command(@uri, "--mime-type", "text/plain"))
end
end

sub_test_case("standard input") do
Expand Down Expand Up @@ -205,6 +226,27 @@ def teardown_www_server
],
run_command("--uri", "http://127.0.0.1/hello.txt"))
end

test("--mime-type") do
body = "Hello\n"
@stdin << "Hello\n"
@stdin.rewind
assert_equal([
true,
{
"mime-type" => "text/html",
"size" => body.bytesize,
"texts" => [
{
"mime-type" => "text/html",
"size" => body.bytesize,
"body" => body,
},
],
},
],
run_command("--mime-type", "text/html"))
end
end
end

Expand Down

0 comments on commit b0e8779

Please sign in to comment.