Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sure we can validate HARs from stdin.

  • Loading branch information...
commit 7c1c84f43887a7c52987c1c63fc6bf45d7204e5c 1 parent 3c85248
@jarib authored
Showing with 31 additions and 9 deletions.
  1. +13 −9 lib/har/viewer.rb
  2. +6 −0 spec/har/viewer_spec.rb
  3. +12 −0 spec/spec_helper.rb
View
22 lib/har/viewer.rb
@@ -10,17 +10,17 @@ def initialize(args)
@running = false
@options = parse(args)
- if @options[:validate]
- args = validate(args)
- end
-
if args == ["-"]
progress("Reading HAR from stdin...") {
@har = Archive.from_file $stdin
}
+
+ validate_if_needed @har
else
+ hars = validate_if_needed(args)
+
progress("Merging HARs...") {
- @har = Archive.by_merging args
+ @har = Archive.by_merging hars
}
end
end
@@ -33,12 +33,16 @@ def show
private
- def validate(hars)
+ def validate_if_needed(hars)
+ return hars unless @options[:validate]
+
progress("Validating archives...") {
- hars = hars.map { |path| Archive.from_file(path) }
- hars.each { |h| h.validate! }
+ Array(hars).map { |har|
+ har = har.kind_of?(Archive) ? har : Archive.from_file(har)
+ har.validate!
- hars
+ har
+ }
}
end
View
6 spec/har/viewer_spec.rb
@@ -26,6 +26,12 @@ module HAR
lambda { Viewer.new(["--validate", *all_hars]) }.should raise_error(ValidationError)
end
+ it "validates HARs read from stdin if asked to" do
+ with_stdin_replaced_by(har_path("bad")) do
+ lambda { Viewer.new(["--validate", "-"]) }.should raise_error(ValidationError)
+ end
+ end
+
it "has a merged archive" do
Viewer.new(good_hars).har.should be_kind_of(Archive)
end
View
12 spec/spec_helper.rb
@@ -34,6 +34,18 @@ def google_path
def good_hars
all_hars.reject { |e| e =~ /bad/ }
end
+
+ def with_stdin_replaced_by(file_path)
+ stdin = $stdin
+ begin
+ File.open(file_path, "r") { |io|
+ $stdin = io
+ yield
+ }
+ ensure
+ $stdin = stdin
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.