-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from solenko/master
Allow ignoring of invalid fields
- Loading branch information
Showing
8 changed files
with
148 additions
and
26 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
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,16 @@ | ||
module Vcard | ||
class Configuration | ||
|
||
attr_accessor :raise_on_invalid_line | ||
alias_method :raise_on_invalid_line?, :raise_on_invalid_line | ||
|
||
attr_accessor :ignore_invalid_vcards | ||
alias_method :ignore_invalid_vcards?, :ignore_invalid_vcards | ||
|
||
def initialize | ||
@raise_on_invalid = true | ||
@ignore_invalid_vcard = true | ||
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
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 |
---|---|---|
|
@@ -21,31 +21,31 @@ def test_encode_decode_text() | |
def test_field4 | ||
line = "t;e=a,b: 4 " | ||
part = Field.decode0(line) | ||
assert_equal("4", part[ 3 ]) | ||
assert_equal("4", part[ 4 ]) | ||
end | ||
|
||
def test_field3 | ||
line = "t;e=a,b:4" | ||
part = Field.decode0(line) | ||
assert_equal("4", part[ 3 ]) | ||
assert_equal( {"E" => [ "a","b" ] }, part[ 2 ]) | ||
assert_equal("4", part[ 4 ]) | ||
assert_equal( {"E" => [ "a","b" ] }, part[ 3 ]) | ||
end | ||
|
||
def test_field2 | ||
line = "tel;type=work,voice,msg:+1 313 747-4454" | ||
part = Field.decode0(line) | ||
assert_equal("+1 313 747-4454", part[ 3 ]) | ||
assert_equal( {"TYPE" => [ "work","voice","msg" ] }, part[ 2 ]) | ||
assert_equal("+1 313 747-4454", part[ 4 ]) | ||
assert_equal( {"TYPE" => [ "work","voice","msg" ] }, part[ 3 ]) | ||
end | ||
|
||
def test_field1 | ||
line = 'ORGANIZER;CN="xxxx, xxxx [SC100:370:EXCH]":MAILTO:[email protected]' | ||
parts = Field.decode0(line) | ||
|
||
assert_equal(nil, parts[0]) | ||
assert_equal("ORGANIZER", parts[1]) | ||
assert_equal({ "CN" => [ "xxxx, xxxx [SC100:370:EXCH]" ] }, parts[2]) | ||
assert_equal("MAILTO:[email protected]", parts[3]) | ||
assert_equal(nil, parts[1]) | ||
assert_equal("ORGANIZER", parts[2]) | ||
assert_equal({ "CN" => [ "xxxx, xxxx [SC100:370:EXCH]" ] }, parts[3]) | ||
assert_equal("MAILTO:[email protected]", parts[4]) | ||
end | ||
|
||
=begin this can not be done :-( | ||
|
@@ -67,20 +67,21 @@ def test_case_equiv | |
|
||
def test_field0 | ||
assert_equal("name:", line = Field.encode0(nil, "name")) | ||
assert_equal([ nil, "NAME", {}, ""], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", {}, ""], Field.decode0(line)) | ||
|
||
assert_equal("name:value", line = Field.encode0(nil, "name", {}, "value")) | ||
assert_equal([ nil, "NAME", {}, "value"], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", {}, "value"], Field.decode0(line)) | ||
|
||
assert_equal("name;encoding=B:dmFsdWU=", line = Field.encode0(nil, "name", { "encoding"=>:b64 }, "value")) | ||
assert_equal([ nil, "NAME", { "ENCODING"=>["B"]}, ["value"].pack("m").chomp ], Field.decode0(line)) | ||
assert_equal([ true, nil, "NAME", { "ENCODING"=>["B"]}, ["value"].pack("m").chomp ], Field.decode0(line)) | ||
|
||
line = Field.encode0("group", "name", {}, "value") | ||
assert_equal "group.name:value", line | ||
assert_equal [ "GROUP", "NAME", {}, "value"], Field.decode0(line) | ||
assert_equal [ true, "GROUP", "NAME", {}, "value"], Field.decode0(line) | ||
end | ||
|
||
def test_invalid_fields | ||
def test_invalid_fields_wih_raise_error | ||
Vcard::configuration.raise_on_invalid_line = true | ||
[ | ||
"g.:", | ||
":v", | ||
|
@@ -89,6 +90,16 @@ def test_invalid_fields | |
end | ||
end | ||
|
||
def test_invalid_fields_wihout_raise_error | ||
Vcard.configuration.raise_on_invalid_line = false | ||
[ | ||
"g.:", | ||
":v", | ||
].each do |line| | ||
assert_nothing_raised { Field.decode0(line) } | ||
end | ||
end | ||
|
||
def test_date_encode | ||
assert_equal("DTSTART:20040101\n", Field.create("DTSTART", Date.new(2004, 1, 1) ).to_s) | ||
assert_equal("DTSTART:20040101\n", Field.create("DTSTART", [Date.new(2004, 1, 1)]).to_s) | ||
|
@@ -97,6 +108,7 @@ def test_date_encode | |
def test_field_modify | ||
f = Field.create("name") | ||
|
||
|
||
assert_equal("", f.value) | ||
f.value = "" | ||
assert_equal("", f.value) | ||
|
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 |
---|---|---|
|
@@ -169,12 +169,50 @@ def _test_cons # FIXME | |
end | ||
|
||
def test_bad | ||
# FIXME: this should THROW, it's badly encoded! | ||
Vcard::configuration.raise_on_invalid_line = true | ||
assert_raises(::Vcard::InvalidEncodingError) do | ||
Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n") | ||
end | ||
end | ||
|
||
def test_not_raise_error_if_configured_to_ignore | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = false | ||
assert_nothing_raised do | ||
Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\n\nEND:VCARD\n") | ||
end | ||
end | ||
|
||
def test_ignore_vcards_with_invalid_fields | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = true | ||
src = <<'EOF' | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
KEYencoding=b:this could be | ||
my certificate | ||
EMAIL:[email protected] | ||
END:VCARD | ||
BEGIN:VCARD | ||
VERSION:3.0 | ||
EMAIL:[email protected] | ||
END:VCARD | ||
EOF | ||
|
||
cards = Vcard::Vcard.decode(src) | ||
assert_equal 1, cards.size | ||
end | ||
|
||
def test_ignore_only_invalid_fields | ||
Vcard::configuration.raise_on_invalid_line = false | ||
Vcard::configuration.ignore_invalid_vcards = false | ||
email = '[email protected]' | ||
cards = Vcard::Vcard.decode("BEGIN:VCARD\nVERSION:3.0\nKEYencoding=b:this could be \nmy certificate\nEMAIL:#{email}\n\nEND:VCARD\n") | ||
assert_equal email, cards.first.email | ||
# [BEGIN, VERSION, EMAIL, END].size == 4 | ||
assert_equal 4, cards.first.fields.size | ||
end | ||
|
||
def test_create | ||
card = Vcard::Vcard.create | ||
key = Vcard::DirectoryInfo.decode("key;type=x509;encoding=B:dGhpcyBjb3VsZCBiZSAKbXkgY2VydGlmaWNhdGUK\n")['key'] | ||
|