|
21 | 21 | let(:internal_parser) { RefererParser::Parser.new(fixture('internal.json')) }
|
22 | 22 | let(:combined_parser) { RefererParser::Parser.new([RefererParser::Parser::DefaultFile, fixture('internal.json')]) }
|
23 | 23 | let(:remote_parser) { RefererParser::Parser.new(remote_file) }
|
| 24 | + let(:domain_index) { parser.instance_variable_get(:@domain_index) } |
| 25 | + let(:name_hash) { parser.instance_variable_get(:@name_hash) } |
| 26 | + |
| 27 | + # This gets overridden for different parsers in subsections |
| 28 | + let(:parser) { default_parser } |
24 | 29 |
|
25 | 30 | describe "exceptions" do
|
26 | 31 | it "should raise UnsupportedFormatError" do
|
27 |
| - lambda { default_parser.update(__FILE__) }.should raise_error(RefererParser::UnsupportedFormatError) |
| 32 | + lambda { parser.update(__FILE__) }.should raise_error(RefererParser::UnsupportedFormatError) |
28 | 33 | end
|
29 | 34 |
|
30 | 35 | it "should raise CorruptReferersError with invalid json" do
|
31 |
| - lambda { default_parser.update(fixture('invalid.json')) }.should raise_error(RefererParser::CorruptReferersError) |
| 36 | + lambda { parser.update(fixture('invalid.json')) }.should raise_error(RefererParser::CorruptReferersError) |
32 | 37 | end
|
33 | 38 |
|
34 | 39 | it "should raise CorruptReferersError with invalid yaml" do
|
35 |
| - lambda { default_parser.update(fixture('invalid.yml')) }.should raise_error(RefererParser::CorruptReferersError) |
| 40 | + lambda { parser.update(fixture('invalid.yml')) }.should raise_error(RefererParser::CorruptReferersError) |
36 | 41 | end
|
37 | 42 |
|
38 | 43 | it "should raise CorruptReferersError with valid file with invalid data" do
|
39 |
| - lambda { default_parser.update(fixture('referer-tests.json')) }.should raise_error(RefererParser::CorruptReferersError) |
| 44 | + lambda { parser.update(fixture('referer-tests.json')) }.should raise_error(RefererParser::CorruptReferersError) |
40 | 45 | end
|
41 | 46 |
|
42 | 47 | it "should raise InvalidUriError with insane" do
|
43 |
| - lambda { default_parser.parse('>total gibberish<') }.should raise_error(RefererParser::InvalidUriError) |
| 48 | + lambda { parser.parse('>total gibberish<') }.should raise_error(RefererParser::InvalidUriError) |
44 | 49 | end
|
45 | 50 |
|
46 | 51 | it "should raise InvalidUriError with non http(s)" do
|
47 |
| - lambda { default_parser.parse('ftp://ftp.really.com/whatever.json') }.should raise_error(RefererParser::InvalidUriError) |
| 52 | + lambda { parser.parse('ftp://ftp.really.com/whatever.json') }.should raise_error(RefererParser::InvalidUriError) |
48 | 53 | end
|
49 | 54 | end
|
50 | 55 |
|
51 | 56 | describe "with the default parser" do
|
52 | 57 | it "should have a non-empty domain_index" do
|
53 |
| - default_parser.instance_variable_get(:@domain_index).should_not be_empty |
| 58 | + domain_index.should_not be_empty |
54 | 59 | end
|
55 | 60 |
|
56 | 61 | it "should have a non-empty name_hash" do
|
57 |
| - default_parser.instance_variable_get(:@name_hash).should_not be_empty |
| 62 | + name_hash.should_not be_empty |
58 | 63 | end
|
59 | 64 |
|
60 | 65 | it "should be clearable" do
|
61 |
| - default_parser.clear! |
62 |
| - default_parser.instance_variable_get(:@name_hash).should be_empty |
63 |
| - default_parser.instance_variable_get(:@domain_index).should be_empty |
| 66 | + parser.clear! |
| 67 | + name_hash.should be_empty |
| 68 | + domain_index.should be_empty |
64 | 69 | end
|
65 | 70 |
|
66 | 71 | it "should be updatable" do
|
67 |
| - size = default_parser.instance_variable_get(:@domain_index).size |
68 |
| - default_parser.update(fixture('internal.json')) |
69 |
| - default_parser.instance_variable_get(:@domain_index).size.should > size |
| 72 | + size = domain_index.size |
| 73 | + parser.update(fixture('internal.json')) |
| 74 | + domain_index.size.should > size |
70 | 75 | end
|
71 | 76 | end
|
72 | 77 |
|
73 | 78 | describe "with the internal parser" do
|
| 79 | + let(:parser) { internal_parser } |
| 80 | + |
74 | 81 | it "should have internal mediums only" do
|
75 |
| - internal_parser.instance_variable_get(:@domain_index).each_value do |(arr)| |
| 82 | + domain_index.each_value do |(arr)| |
76 | 83 | path, name_key = arr[0], arr[1]
|
77 |
| - internal_parser.instance_variable_get(:@name_hash)[name_key][:medium].should == 'internal' |
| 84 | + name_hash[name_key][:medium].should == 'internal' |
78 | 85 | end
|
79 | 86 | end
|
80 | 87 | end
|
81 | 88 |
|
82 | 89 | describe "with the remote parser" do
|
| 90 | + let(:parser) { remote_parser } |
| 91 | + |
83 | 92 | # These are combined here to reduce network fetches
|
84 | 93 | it "should have a non-empty domain_index and name_hash" do
|
85 |
| - remote_parser.instance_variable_get(:@domain_index).should_not be_empty |
86 |
| - remote_parser.instance_variable_get(:@name_hash).should_not be_empty |
| 94 | + domain_index.should_not be_empty |
| 95 | + name_hash.should_not be_empty |
87 | 96 | end
|
88 | 97 | end
|
89 | 98 |
|
90 | 99 | describe "sample fixtures" do
|
| 100 | + let(:parser) { combined_parser } |
91 | 101 | # Include our internal data as well
|
92 | 102 | JSON.parse(File.read(File.join(File.dirname(__FILE__), 'fixtures', 'referer-tests.json'))).each do |fixture|
|
93 | 103 | it fixture['spec'] do
|
94 | 104 | parsed_as_string, parsed_as_uri = nil, nil
|
95 |
| - lambda { parsed_as_string = combined_parser.parse(fixture['uri']) }.should_not raise_error |
96 |
| - lambda { parsed_as_uri = combined_parser.parse(URI.parse(fixture['uri'])) }.should_not raise_error |
| 105 | + lambda { parsed_as_string = parser.parse(fixture['uri']) }.should_not raise_error |
| 106 | + lambda { parsed_as_uri = parser.parse(URI.parse(fixture['uri'])) }.should_not raise_error |
97 | 107 |
|
98 | 108 | ['source', 'term', 'known', 'medium'].each do |key|
|
99 | 109 | parsed_as_uri[key.to_sym].should == fixture[key]
|
|
105 | 115 |
|
106 | 116 | describe "general behavior" do
|
107 | 117 | it "should return the better result when the referer contains two or more parameters" do
|
108 |
| - parsed = default_parser.parse("http://search.tiscali.it/?tiscalitype=web&collection=web&q=&key=hello") |
| 118 | + parsed = parser.parse("http://search.tiscali.it/?tiscalitype=web&collection=web&q=&key=hello") |
109 | 119 | parsed[:term].should == "hello"
|
110 | 120 | end
|
111 | 121 |
|
112 | 122 | it "should return the better result when the referer contains same parameters" do
|
113 |
| - parsed = default_parser.parse("http://search.tiscali.it/?tiscalitype=web&collection=web&key=&key=hello") |
| 123 | + parsed = parser.parse("http://search.tiscali.it/?tiscalitype=web&collection=web&key=&key=hello") |
114 | 124 | parsed[:term].should == "hello"
|
115 | 125 | end
|
116 | 126 |
|
117 | 127 | it "should return the normalized domain" do
|
118 |
| - parsed = default_parser.parse("http://it.images.search.YAHOO.COM/images/view;_ylt=A0PDodgQmGBQpn4AWQgdDQx.;_ylu=X3oDMTBlMTQ4cGxyBHNlYwNzcgRzbGsDaW1n?back=http%3A%2F%2Fit.images.search.yahoo.com%2Fsearch%2Fimages%3Fp%3DEarth%2BMagic%2BOracle%2BCards%26fr%3Dmcafee%26fr2%3Dpiv-web%26tab%3Dorganic%26ri%3D5&w=1064&h=1551&imgurl=mdm.pbzstatic.com%2Foracles%2Fearth-magic-oracle-cards%2Fcard-1.png&rurl=http%3A%2F%2Fwww.psychicbazaar.com%2Foracles%2F143-earth-magic-oracle-cards.html&size=2.8+KB&name=Earth+Magic+Oracle+Cards+-+Psychic+Bazaar&p=Earth+Magic+Oracle+Cards&oid=f0a5ad5c4211efe1c07515f56cf5a78e&fr2=piv-web&fr=mcafee&tt=Earth%2BMagic%2BOracle%2BCards%2B-%2BPsychic%2BBazaar&b=0&ni=90&no=5&ts=&tab=organic&sigr=126n355ib&sigb=13hbudmkc&sigi=11ta8f0gd&.crumb=IZBOU1c0UHU") |
| 128 | + parsed = parser.parse("http://it.images.search.YAHOO.COM/images/view;_ylt=A0PDodgQmGBQpn4AWQgdDQx.;_ylu=X3oDMTBlMTQ4cGxyBHNlYwNzcgRzbGsDaW1n?back=http%3A%2F%2Fit.images.search.yahoo.com%2Fsearch%2Fimages%3Fp%3DEarth%2BMagic%2BOracle%2BCards%26fr%3Dmcafee%26fr2%3Dpiv-web%26tab%3Dorganic%26ri%3D5&w=1064&h=1551&imgurl=mdm.pbzstatic.com%2Foracles%2Fearth-magic-oracle-cards%2Fcard-1.png&rurl=http%3A%2F%2Fwww.psychicbazaar.com%2Foracles%2F143-earth-magic-oracle-cards.html&size=2.8+KB&name=Earth+Magic+Oracle+Cards+-+Psychic+Bazaar&p=Earth+Magic+Oracle+Cards&oid=f0a5ad5c4211efe1c07515f56cf5a78e&fr2=piv-web&fr=mcafee&tt=Earth%2BMagic%2BOracle%2BCards%2B-%2BPsychic%2BBazaar&b=0&ni=90&no=5&ts=&tab=organic&sigr=126n355ib&sigb=13hbudmkc&sigi=11ta8f0gd&.crumb=IZBOU1c0UHU") |
119 | 129 | parsed[:domain].should == "images.search.yahoo.com"
|
120 | 130 | end
|
121 | 131 | end
|
| 132 | + |
| 133 | + describe "optimize_index" do |
| 134 | + let(:domains) { ['fnord.com', 'fnord.com', 'fnord.com/path'] } |
| 135 | + |
| 136 | + before do |
| 137 | + parser.add_referer('internal', 'Fnord', domains) |
| 138 | + end |
| 139 | + |
| 140 | + it "should have out of order and duplicate domains before optimization" do |
| 141 | + domain_index['fnord.com'].transpose.first.should == ['/', '/', '/path'] |
| 142 | + end |
| 143 | + |
| 144 | + it "should have out of order domains before optimization" do |
| 145 | + parser.optimize_index! |
| 146 | + domain_index['fnord.com'].transpose.first.should == ['/path', '/'] |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + describe "add_referer" do |
| 151 | + it "should add a referer to the domain_index" do |
| 152 | + domain_index['fnord.com'].should be_nil |
| 153 | + parser.add_referer('internal', 'Fnord', 'fnord.com') |
| 154 | + domain_index['fnord.com'].should_not be_nil |
| 155 | + end |
| 156 | + |
| 157 | + it "should add a referer with multiple domains to the domain_index" do |
| 158 | + domain_index['fnord.com'].should be_nil |
| 159 | + domain_index['boo.com'].should be_nil |
| 160 | + parser.add_referer('internal', 'Fnord', ['fnord.com', 'boo.com']) |
| 161 | + domain_index['fnord.com'].should_not be_nil |
| 162 | + domain_index['boo.com'].should_not be_nil |
| 163 | + end |
| 164 | + |
| 165 | + it "should add a referer to the name_hash" do |
| 166 | + name_hash['fnord.com-internal'].should be_nil |
| 167 | + parser.add_referer('internal', 'Fnord', 'fnord.com') |
| 168 | + name_hash['Fnord-internal'].should_not be_nil |
| 169 | + end |
| 170 | + |
| 171 | + it "should add parameters to the name_hash" do |
| 172 | + parser.add_referer('internal', 'Fnord', 'fnord.com', ['Q', 'q']) |
| 173 | + name_hash['Fnord-internal'][:parameters].should == ['Q', 'q'] |
| 174 | + end |
| 175 | + |
| 176 | + it "should add a single parameter to the name_hash" do |
| 177 | + parser.add_referer('internal', 'Fnord', 'fnord.com', 'q') |
| 178 | + name_hash['Fnord-internal'][:parameters].should == ['q'] |
| 179 | + end |
| 180 | + end |
122 | 181 | end
|
0 commit comments