spec/page_spec.rb in spidr-0.5.0 vs spec/page_spec.rb in spidr-0.6.0
- old
+ new
@@ -1,128 +1,164 @@
-require 'spidr/page'
-
require 'spec_helper'
-require 'page_examples'
-require 'helpers/page'
+require 'example_page'
+require 'spidr/page'
+
describe Page do
- describe "html" do
- before(:all) do
- @page = get_page('http://spidr.rubyforge.org/course/start.html')
- end
+ include_context "example Page"
- it_should_behave_like "Page"
+ describe "#initialize" do
+ let(:headers) { {'X-Foo' => 'bar'} }
- it "should be OK" do
- expect(@page).to be_ok
+ it "should set #url" do
+ expect(subject.url).to be url
end
- it "should have a content-type" do
- expect(@page.content_type).to include('text/html')
+ it "should set #headers" do
+ expect(subject.headers).to be == {
+ 'content-type' => [content_type],
+ 'x-foo' => ['bar']
+ }
end
+ end
- it "should be a html page" do
- expect(@page).to be_html
- end
+ describe "method_missing" do
+ let(:headers) { {'X-Foo' => 'bar'} }
- it "should have provide a document" do
- expect(@page.doc.class).to eq(Nokogiri::HTML::Document)
+ it "should provide transparent access to headers" do
+ expect(subject.x_foo).to be == 'bar'
end
- it "should allow searching the document" do
- expect(@page.doc.search('//p').length).to eq(2)
- expect(@page.doc.at('//p[2]').inner_text).to eq('Ready! Set! Go!')
+ context "when the requested header does not exist" do
+ it do
+ expect { subject.x_bar }.to raise_error(NoMethodError)
+ end
end
- it "should have a title" do
- expect(@page.title).to eq('Spidr :: Web-Spider Obstacle Course :: Start')
+ context "when method arguments are also given" do
+ it do
+ expect { subject.x_foo(1) }.to raise_error(NoMethodError)
+ end
end
- it "should have links" do
- expect(@page.links).not_to be_empty
+ context "when a block is also given" do
+ it do
+ expect { subject.x_foo { } }.to raise_error(NoMethodError)
+ end
end
end
- describe "txt" do
- before(:all) do
- @page = get_page('https://www.ruby-lang.org/en/about/license.txt')
- end
+ describe "#body" do
+ context "when there is a body" do
+ let(:body) { %{<html><head><title>example</title></head><body><p>hello</p></body></html>} }
- it_should_behave_like "Page"
-
- it "should be OK" do
- expect(@page).to be_ok
+ it "should return the body text" do
+ expect(subject.body).to be body
+ end
end
- it "should have a content-type" do
- expect(@page.content_type).to include('text/plain')
+ context "when there is no body" do
+ it "should return an empty String" do
+ expect(subject.body).to be == ''
+ end
end
+ end
- it "should be a txt page" do
- expect(@page).to be_txt
- end
+ describe "#doc" do
+ context "when the Content-Type is text/html" do
+ let(:body) { %{<html><head><title>example</title></head><body><p>hello</p></body></html>} }
- it "should not have provide a document" do
- expect(@page.doc).to be_nil
+ it "should parse the body as HTML" do
+ expect(subject.doc).to be_kind_of(Nokogiri::HTML::Document)
+ expect(subject.doc.at('//p').inner_text).to be == 'hello'
+ end
end
- it "should not allow searching the document" do
- expect(@page.search('//p')).to be_empty
- expect(@page.at('//p')).to be_nil
- end
+ context "when the document is application/rss+xml" do
+ let(:content_type) { 'application/rss+xml' }
+ let(:body) do
+ %{<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0"></rss>}
+ end
- it "should not have links" do
- expect(@page.links).to be_empty
+ it "should parse the body as XML" do
+ expect(subject.doc).to be_kind_of(Nokogiri::XML::Document)
+ end
end
- it "should not have a title" do
- expect(@page.title).to be_nil
- end
- end
+ context "when the document is application/atom+xml" do
+ let(:content_type) { 'application/atom+xml' }
+ let(:body) do
+ %{<?xml version="1.0" encoding="UTF-8" ?><feed xmlns="http://www.w3.org/2005/Atom"></feed>}
+ end
- describe "redirects" do
- before(:all) do
- @page = get_page('http://spidr.rubyforge.org/course/start.html')
+ it "should parse the body as XML" do
+ expect(subject.doc).to be_kind_of(Nokogiri::XML::Document)
+ end
end
- before do
- allow(@page).to receive(:body).and_return('<meta HTTP-EQUIV="REFRESH" content="0; url=http://spidr.rubyforge.org/redirected">')
+ context "when the document is text/xml" do
+ let(:content_type) { 'text/xml' }
+ let(:body) do
+ %{<?xml version="1.0" encoding="UTF-8" ?><foo />}
+ end
+
+ it "should parse the body as XML" do
+ expect(subject.doc).to be_kind_of(Nokogiri::XML::Document)
+ end
end
- it "should provide access to page-level redirects" do
- expect(@page.redirects_to).to eq(['http://spidr.rubyforge.org/redirected'])
- end
+ context "when the document is text/xsl" do
+ let(:content_type) { 'text/xsl' }
+ let(:body) do
+ %{<?xml version="1.0" encoding="UTF-8" ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"></xsl:stylesheet>}
+ end
- it "should include meta refresh redirects in the list of links" do
- expect(@page.links).to include('http://spidr.rubyforge.org/redirected')
+ it "should parse the body as XML" do
+ expect(subject.doc).to be_kind_of(Nokogiri::XML::Document)
+ end
end
- end
- describe "cookies" do
- before(:all) do
- @page = get_page('http://twitter.com/login')
+ context "when there is no body" do
+ it "should return an empty String" do
+ expect(subject.doc).to be nil
+ end
end
+ end
- it "should provide access to the raw Cookie" do
- cookie = @page.cookie
+ describe "#search" do
+ context "when there is a document" do
+ let(:body) { %{<html><head><title>example</title></head><body><p>hello</p></body></html>} }
- expect(cookie).not_to be_nil
- expect(cookie).not_to be_empty
+ it "should search the document" do
+ expect(subject.search('//p').inner_text).to be == 'hello'
+ end
end
- it "should provide access to the Cookies" do
- cookies = @page.cookies
-
- expect(cookies).not_to be_empty
+ context "when there is no document" do
+ it "should return an empty Array" do
+ expect(subject.search('//p')).to be == []
+ end
end
+ end
- it "should provide access to the key->value pairs within the Cookie" do
- params = @page.cookie_params
-
- expect(params).not_to be_empty
+ describe "#at" do
+ context "when there is a document" do
+ let(:body) { %{<html><head><title>example</title></head><body><p>hello</p></body></html>} }
- params.each do |key,value|
- expect(key).not_to be_empty
+ it "should search the document for the first matching node" do
+ expect(subject.at('//p').inner_text).to be == 'hello'
end
+ end
+
+ context "when there is no document" do
+ it "should return nil" do
+ expect(subject.at('//p')).to be nil
+ end
+ end
+ end
+
+ describe "#to_s" do
+ it "should return the body" do
+ expect(subject.to_s).to be body
end
end
end