| @@ -6,6 +6,7 @@ class DocsScraperTest < MiniTest::Spec · 5 unmodified lines |
| 6 | self.type = 'scraper' | 6 | self.type = 'scraper' |
| 7 | self.base_url = 'http://example.com/' | 7 | self.base_url = 'http://example.com/' |
| 8 | self.root_path = '/root' | 8 | self.root_path = '/root' |
| | 9 | self.initial_paths = ['/initial'] |
| 9 | self.html_filters = Docs::FilterStack.new | 10 | self.html_filters = Docs::FilterStack.new |
| 10 | self.text_filters = Docs::FilterStack.new | 11 | self.text_filters = Docs::FilterStack.new |
| 11 | end | 12 | end |
| @@ -37,6 +38,12 @@ class DocsScraperTest < MiniTest::Spec · 25 unmodified lines |
| 37 | assert_equal Scraper.root_path, subclass.root_path | 38 | assert_equal Scraper.root_path, subclass.root_path |
| 38 | end | 39 | end |
| 39 | | 40 | |
| | 41 | it "duplicates .initial_paths" do |
| | 42 | stub(Scraper).initial_paths { ['path'] } |
| | 43 | assert_equal Scraper.initial_paths, subclass.initial_paths |
| | 44 | refute_same Scraper.initial_paths, subclass.initial_paths |
| | 45 | end |
| | 46 | |
| 40 | it "duplicates .options" do | 47 | it "duplicates .options" do |
| 41 | stub(Scraper).options { { test: [] } } | 48 | stub(Scraper).options { { test: [] } } |
| 42 | assert_equal Scraper.options, subclass.options | 49 | assert_equal Scraper.options, subclass.options |
| @@ -112,6 +119,29 @@ class DocsScraperTest < MiniTest::Spec · 69 unmodified lines |
| 112 | end | 119 | end |
| 113 | end | 120 | end |
| 114 | | 121 | |
| | 122 | describe "#initial_urls" do |
| | 123 | let :initial_urls do |
| | 124 | scraper.initial_urls |
| | 125 | end |
| | 126 | |
| | 127 | it "returns a frozen, memoized Array" do |
| | 128 | assert_instance_of Array, initial_urls |
| | 129 | assert initial_urls.frozen? |
| | 130 | assert_same initial_urls, scraper.initial_urls |
| | 131 | end |
| | 132 | |
| | 133 | it "includes the #root_url" do |
| | 134 | assert_includes initial_urls, scraper.root_url.to_s |
| | 135 | end |
| | 136 | |
| | 137 | it "includes the .initial_paths converted to urls" do |
| | 138 | stub(Scraper).base_url { 'http://example.com/' } |
| | 139 | stub(Scraper).initial_paths { ['one', '/two'] } |
| | 140 | assert_includes initial_urls, 'http://example.com/one' |
| | 141 | assert_includes initial_urls, 'http://example.com/two' |
| | 142 | end |
| | 143 | end |
| | 144 | |
| 115 | describe "#build_page" do | 145 | describe "#build_page" do |
| 116 | before do | 146 | before do |
| 117 | stub(scraper).handle_response | 147 | stub(scraper).handle_response |
| @@ -171,8 +201,8 @@ class DocsScraperTest < MiniTest::Spec · 53 unmodified lines |
| 171 | Proc.new {} | 201 | Proc.new {} |
| 172 | end | 202 | end |
| 173 | | 203 | |
| 174 | it "requests the root url" do | 204 | it "requests the #initial_urls" do |
| 175 | mock(scraper).request_all(scraper.root_url.to_s) | 205 | mock(scraper).request_all(scraper.initial_urls) |
| 176 | scraper.build_pages(&block) | 206 | scraper.build_pages(&block) |
| 177 | end | 207 | end |
| 178 | | 208 | |
| @@ -181,12 +211,14 @@ class DocsScraperTest < MiniTest::Spec · 2 unmodified lines |
| 181 | scraper.build_pages(&block) | 211 | scraper.build_pages(&block) |
| 182 | assert scraper.last_instrumentation | 212 | assert scraper.last_instrumentation |
| 183 | assert_equal 'running.scraper', scraper.last_instrumentation[:event] | 213 | assert_equal 'running.scraper', scraper.last_instrumentation[:event] |
| 184 | assert_includes scraper.last_instrumentation[:payload][:urls], scraper.root_url.to_s | 214 | assert_equal scraper.initial_urls, scraper.last_instrumentation[:payload][:urls] |
| 185 | end | 215 | end |
| 186 | | 216 | |
| 187 | context "when the response is processable" do | 217 | context "when the response is processable" do |
| 188 | before do | 218 | before do |
| 189 | stub(scraper).request_all.with_any_args { |*args| @returned = args.last.call(response) } | 219 | stub(scraper).request_all do |urls, block| |
| | 220 | urls.each { |url| @next_urls ||= block.call(response) } |
| | 221 | end |
| 190 | stub(scraper).handle_response(response) { processed_response } | 222 | stub(scraper).handle_response(response) { processed_response } |
| 191 | end | 223 | end |
| 192 | | 224 | |
| @@ -203,7 +235,7 @@ class DocsScraperTest < MiniTest::Spec · 10 unmodified lines |
| 203 | | 235 | |
| 204 | it "requests nothing more" do | 236 | it "requests nothing more" do |
| 205 | scraper.build_pages(&block) | 237 | scraper.build_pages(&block) |
| 206 | assert_nil @returned | 238 | assert_nil @next_urls |
| 207 | end | 239 | end |
| 208 | | 240 | |
| 209 | it "doesn't instrument 'queued'" do | 241 | it "doesn't instrument 'queued'" do |
| @@ -223,14 +255,14 @@ class DocsScraperTest < MiniTest::Spec · 13 unmodified lines |
| 223 | | 255 | |
| 224 | it "requests the urls" do | 256 | it "requests the urls" do |
| 225 | scraper.build_pages(&block) | 257 | scraper.build_pages(&block) |
| 226 | assert_equal urls, @returned | 258 | assert_equal urls, @next_urls |
| 227 | end | 259 | end |
| 228 | | 260 | |
| 229 | it "doesn't request the same url twice irrespective of case" do | 261 | it "doesn't request the same url twice irrespective of case" do |
| 230 | stub(Scraper).root_path { 'PATH' } | 262 | stub(Scraper).root_path { 'PATH' } |
| 231 | processed_response[:internal_urls] = [scraper.root_url.to_s.swapcase] | 263 | processed_response[:internal_urls] = scraper.initial_urls.map(&:swapcase) |
| 232 | scraper.build_pages(&block) | 264 | scraper.build_pages(&block) |
| 233 | assert_empty @returned | 265 | assert_empty @next_urls |
| 234 | end | 266 | end |
| 235 | | 267 | |
| 236 | it "instruments 'queued'" do | 268 | it "instruments 'queued'" do |
| @@ -280,6 +312,10 @@ class DocsScraperTest < MiniTest::Spec · 43 unmodified lines |
| 280 | assert_equal '/root', options[:root_path] | 312 | assert_equal '/root', options[:root_path] |
| 281 | end | 313 | end |
| 282 | | 314 | |
| | 315 | it "includes .initial_paths" do |
| | 316 | assert_equal ['/initial'], options[:initial_paths] |
| | 317 | end |
| | 318 | |
| 283 | context "when #root_path? is false" do | 319 | context "when #root_path? is false" do |
| 284 | before do | 320 | before do |
| 285 | stub(scraper).root_path? { false } | 321 | stub(scraper).root_path? { false } |
| @@ -300,15 +336,27 @@ class DocsScraperTest < MiniTest::Spec · 14 unmodified lines |
| 300 | assert_includes options[:only], '/' | 336 | assert_includes options[:only], '/' |
| 301 | end | 337 | end |
| 302 | | 338 | |
| | 339 | it "adds .initial_paths" do |
| | 340 | assert_includes options[:only], '/initial' |
| | 341 | end |
| | 342 | |
| 303 | it "doesn't modify the array in place" do | 343 | it "doesn't modify the array in place" do |
| 304 | assert_equal ['/path'], Scraper.options[:only] | 344 | assert_equal ['/path'], Scraper.options[:only] |
| 305 | end | 345 | end |
| 306 | end | 346 | end |
| 307 | | 347 | |
| 308 | context "and :only_patterns is an array" do | 348 | context "and :only_patterns is an array" do |
| 309 | it "assigns ['', '/'] to :only" do | 349 | before do |
| 310 | stub(Scraper).options { { only_patterns: [] } } | 350 | stub(Scraper).options { { only_patterns: [] } } |
| 311 | assert_equal ['', '/'], options[:only] | 351 | end |
| | 352 | |
| | 353 | it "adds ['', '/'] to :only" do |
| | 354 | assert_includes options[:only], '' |
| | 355 | assert_includes options[:only], '/' |
| | 356 | end |
| | 357 | |
| | 358 | it "adds .initial_paths to :only" do |
| | 359 | assert_includes options[:only], '/initial' |
| 312 | end | 360 | end |
| 313 | end | 361 | end |
| 314 | end | 362 | end |
| @@ -348,9 +396,9 @@ class DocsScraperTest < MiniTest::Spec · 33 unmodified lines |
| 348 | end | 396 | end |
| 349 | | 397 | |
| 350 | context "and :only_patterns is an array" do | 398 | context "and :only_patterns is an array" do |
| 351 | it "assigns [.root_path] to :only" do | 399 | it "adds .root_path to :only" do |
| 352 | stub(Scraper).options { { only_patterns: [] } } | 400 | stub(Scraper).options { { only_patterns: [] } } |
| 353 | assert_equal ['/root'], options[:only] | 401 | assert_includes options[:only], '/root' |
| 354 | end | 402 | end |
| 355 | end | 403 | end |
| 356 | end | 404 | end |