File: request_spec.rb

package info (click to toggle)
ruby-http 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 404 kB
  • ctags: 213
  • sloc: ruby: 2,397; makefile: 7
file content (106 lines) | stat: -rw-r--r-- 3,553 bytes parent folder | download
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'spec_helper'

describe HTTP::Request do
  it 'includes HTTP::Headers::Mixin' do
    expect(described_class).to include HTTP::Headers::Mixin
  end

  it 'requires URI to have scheme part' do
    expect { HTTP::Request.new(:get, 'example.com/') }.to \
      raise_error(HTTP::Request::UnsupportedSchemeError)
  end

  it 'provides a #scheme accessor' do
    request = HTTP::Request.new(:get, 'http://example.com/')
    expect(request.scheme).to eq(:http)
  end

  describe 'headers' do
    subject { HTTP::Request.new(:get, 'http://example.com/', :accept => 'text/html') }

    it 'sets explicit headers' do
      expect(subject['Accept']).to eq('text/html')
    end

    it 'sets implicit headers' do
      expect(subject['Host']).to eq('example.com')
    end

    it 'provides a #verb accessor' do
      expect(subject.verb).to eq(:get)
    end

    it 'provides a #method accessor that outputs a deprecation warning and returns the verb' do
      warning = capture_warning do
        expect(subject.method).to eq(subject.verb)
      end
      expect(warning).to match(/\[DEPRECATION\] HTTP::Request#method is deprecated\. Use #verb instead\. For Object#method, use #__method__\.$/)
    end

    it 'provides a #__method__ method that delegates to Object#method' do
      expect(subject.__method__(:verb)).to be_a(Method)
    end
  end

  describe '#redirect' do
    let(:headers)   { {:accept => 'text/html'} }
    let(:proxy)     { {:proxy_username => 'douglas', :proxy_password => 'adams'} }
    let(:body)      { 'The Ultimate Question' }
    let(:request)   { HTTP::Request.new(:post, 'http://example.com/', headers, proxy, body) }

    subject(:redirected) { request.redirect 'http://blog.example.com/' }

    its(:uri)     { should eq URI.parse 'http://blog.example.com/' }

    its(:verb)    { should eq request.verb }
    its(:body)    { should eq request.body }
    its(:proxy)   { should eq request.proxy }

    it 'presets new Host header' do
      expect(redirected['Host']).to eq 'blog.example.com'
    end

    context 'with relative URL given' do
      subject(:redirected) { request.redirect '/blog' }

      its(:uri)     { should eq URI.parse 'http://example.com/blog' }

      its(:verb)    { should eq request.verb }
      its(:body)    { should eq request.body }
      its(:proxy)   { should eq request.proxy }

      it 'keeps Host header' do
        expect(redirected['Host']).to eq 'example.com'
      end

      context 'with original URI having non-standard port' do
        let(:request) { HTTP::Request.new(:post, 'http://example.com:8080/', headers, proxy, body) }
        its(:uri)     { should eq URI.parse 'http://example.com:8080/blog' }
      end
    end

    context 'with relative URL that misses leading slash given' do
      subject(:redirected) { request.redirect 'blog' }

      its(:uri)     { should eq URI.parse 'http://example.com/blog' }

      its(:verb)    { should eq request.verb }
      its(:body)    { should eq request.body }
      its(:proxy)   { should eq request.proxy }

      it 'keeps Host header' do
        expect(redirected['Host']).to eq 'example.com'
      end

      context 'with original URI having non-standard port' do
        let(:request) { HTTP::Request.new(:post, 'http://example.com:8080/', headers, proxy, body) }
        its(:uri)     { should eq URI.parse 'http://example.com:8080/blog' }
      end
    end

    context 'with new verb given' do
      subject { request.redirect 'http://blog.example.com/', :get }
      its(:verb) { should be :get }
    end
  end
end