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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
# coding: utf-8
RSpec.describe HTTP::Request do
let(:proxy) { {} }
let(:headers) { {:accept => "text/html"} }
let(:request_uri) { "http://example.com/foo?bar=baz" }
subject :request do
HTTP::Request.new(
:verb => :get,
:uri => request_uri,
:headers => headers,
:proxy => proxy
)
end
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(:verb => :get, :uri => "example.com/") }.to \
raise_error(HTTP::Request::UnsupportedSchemeError)
end
it "provides a #scheme accessor" do
expect(request.scheme).to eq(:http)
end
it "provides a #verb accessor" do
expect(subject.verb).to eq(:get)
end
it "sets given headers" do
expect(subject["Accept"]).to eq("text/html")
end
describe "Host header" do
subject { request["Host"] }
context "was not given" do
it { is_expected.to eq "example.com" }
context "and request URI has non-standard port" do
let(:request_uri) { "http://example.com:3000/" }
it { is_expected.to eq "example.com:3000" }
end
end
context "was explicitly given" do
before { headers[:host] = "github.com" }
it { is_expected.to eq "github.com" }
end
end
describe "User-Agent header" do
subject { request["User-Agent"] }
context "was not given" do
it { is_expected.to eq HTTP::Request::USER_AGENT }
end
context "was explicitly given" do
before { headers[:user_agent] = "MrCrawly/123" }
it { is_expected.to eq "MrCrawly/123" }
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 do
HTTP::Request.new(
:verb => :post,
:uri => "http://example.com/",
:headers => headers,
:proxy => proxy,
:body => body
)
end
subject(:redirected) { request.redirect "http://blog.example.com/" }
its(:uri) { is_expected.to eq HTTP::URI.parse "http://blog.example.com/" }
its(:verb) { is_expected.to eq request.verb }
its(:body) { is_expected.to eq request.body }
its(:proxy) { is_expected.to eq request.proxy }
it "presets new Host header" do
expect(redirected["Host"]).to eq "blog.example.com"
end
context "with schema-less absolute URL given" do
subject(:redirected) { request.redirect "//another.example.com/blog" }
its(:uri) { is_expected.to eq HTTP::URI.parse "http://another.example.com/blog" }
its(:verb) { is_expected.to eq request.verb }
its(:body) { is_expected.to eq request.body }
its(:proxy) { is_expected.to eq request.proxy }
it "presets new Host header" do
expect(redirected["Host"]).to eq "another.example.com"
end
end
context "with relative URL given" do
subject(:redirected) { request.redirect "/blog" }
its(:uri) { is_expected.to eq HTTP::URI.parse "http://example.com/blog" }
its(:verb) { is_expected.to eq request.verb }
its(:body) { is_expected.to eq request.body }
its(:proxy) { is_expected.to 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 do
HTTP::Request.new(
:verb => :post,
:uri => "http://example.com:8080/",
:headers => headers,
:proxy => proxy,
:body => body
)
end
its(:uri) { is_expected.to eq HTTP::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) { is_expected.to eq HTTP::URI.parse "http://example.com/blog" }
its(:verb) { is_expected.to eq request.verb }
its(:body) { is_expected.to eq request.body }
its(:proxy) { is_expected.to 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 do
HTTP::Request.new(
:verb => :post,
:uri => "http://example.com:8080/",
:headers => headers,
:proxy => proxy,
:body => body
)
end
its(:uri) { is_expected.to eq HTTP::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) { is_expected.to be :get }
end
end
describe "#headline" do
subject(:headline) { request.headline }
it { is_expected.to eq "GET /foo?bar=baz HTTP/1.1" }
context "when URI contains encoded query" do
let(:encoded_query) { "t=1970-01-01T01%3A00%3A00%2B01%3A00" }
let(:request_uri) { "http://example.com/foo/?#{encoded_query}" }
it "does not unencodes query part" do
expect(headline).to eq "GET /foo/?#{encoded_query} HTTP/1.1"
end
end
context "when URI contains non-ASCII path" do
let(:request_uri) { "http://example.com/キョ" }
it "encodes non-ASCII path part" do
expect(headline).to eq "GET /%E3%82%AD%E3%83%A7 HTTP/1.1"
end
end
context "when URI contains fragment" do
let(:request_uri) { "http://example.com/foo#bar" }
it "omits fragment part" do
expect(headline).to eq "GET /foo HTTP/1.1"
end
end
context "with proxy" do
let(:proxy) { {:user => "user", :pass => "pass"} }
it { is_expected.to eq "GET http://example.com/foo?bar=baz HTTP/1.1" }
end
end
end
|