brynary / webrat

Webrat - Ruby Acceptance Testing for Web applications

This URL has Read+Write access

webrat / spec / private / mechanize / mechanize_session_spec.rb
100644 81 lines (64 sloc) 2.186 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
 
require "webrat/mechanize"
 
describe Webrat::MechanizeSession do
  before :each do
    Webrat.configuration.mode = :mechanize
  end
 
  before(:each) do
    @mech = Webrat::MechanizeSession.new
  end
  
  describe "headers method" do
    it "should return empty headers for a newly initialized session" do
      @mech.headers.should == {}
    end
  end
  
  describe "post" do
    def url
      'http://test.host/users'
    end
    
    def data
      {:user => {:first_name => 'Nancy', :last_name => 'Callahan'}}
    end
    
    def flattened_data
      {'user[first_name]' => 'Nancy', 'user[last_name]' => 'Callahan'}
    end
    
    it "should flatten model post data" do
      mechanize = mock(:mechanize)
      WWW::Mechanize.stub!(:new => mechanize)
      mechanize.should_receive(:post).with(url, flattened_data)
      Webrat::MechanizeSession.new.post(url, data)
    end
  end
  
  describe "#absolute_url" do
    before(:each) do
      @session = Webrat::MechanizeSession.new
      @session.stub!(:current_url).and_return(absolute_url)
    end
    
    def absolute_url
      'http://test.host/users/fred/cabbages'
    end
    
    def rooted_url
      '/users/fred/cabbages'
    end
    
    def relative_url
      '../../wilma'
    end
    
    it "should return unmodified url if prefixed with scheme" do
      @session.absolute_url(absolute_url).should == absolute_url
    end
    
    it "should prefix scheme and hostname if url begins with /" do
      @session.absolute_url(rooted_url).should == absolute_url
    end
    
    it "should resolve sibling URLs relative to current path" do
      @session.absolute_url(relative_url).should == 'http://test.host/users/wilma'
    end
    
    it "should cope with sibling URLs from root of site" do
      @session.stub!(:current_url).and_return('http://test.host')
      @session.absolute_url(relative_url).should == 'http://test.host/wilma'
    end
    
    it "should cope with https" do
      @session.stub!(:current_url).and_return('https://test.host')
      @session.absolute_url(relative_url).should == 'https://test.host/wilma'
    end
  end
end