Land #3789, adds specs for Rex::Proto::Http::Packet::Header

orts
bug/bundler_fix
HD Moore 2014-09-13 00:21:43 -05:00
commit 6a2a85d2c4
5 changed files with 164 additions and 1 deletions

View File

@ -32,7 +32,7 @@ class Packet
Completed = 3
end
require 'rex/proto/http/header'
require 'rex/proto/http/packet/header'
#
# Initializes an instance of an HTTP packet.

View File

@ -0,0 +1,89 @@
require 'spec_helper'
require 'rex/proto/http/packet/header'
describe Rex::Proto::Http::Packet::Header do
it_behaves_like "hash with insensitive keys"
let :original_str do
"POST /foo HTTP/1.0\r\n" \
"Content-Length: 0\r\n" \
"Foo: Bar\r\n" \
"Bar: Baz\r\n" \
"Combine-me: one\r\n" \
"Combine-me: two\r\n" \
"\r\n"
end
describe "#from_s" do
subject(:headers) do
h = described_class.new
h.from_s(original_str)
h
end
it "should create keys and values for each header" do
expect(headers['Foo']).to eq "Bar"
expect(headers['Content-Length']).to eq "0"
end
it "should combine headers" do
expect(headers['Combine-me']).to eq "one, two"
end
context "with folding" do
let :original_str do
"POST /foo HTTP/1.0\r\n" \
"Spaces:\r\n" \
" Bar\r\n" \
"Tabs:\r\n" \
"\tBar\r\n" \
"\r\n"
end
it "should recognize spaces" do
expect(headers['Spaces']).to eq "Bar"
end
it "should recognize tabs" do
expect(headers['Tabs']).to eq "Bar"
end
end
end
describe "#to_s" do
subject(:header_string) do
h = described_class.new
h.from_s(original_str)
h.to_s
end
context "without combining" do
let :original_str do
"POST /foo HTTP/1.0\r\n" \
"Foo: Bar\r\n" \
"Bar: Baz\r\n" \
"\r\n"
end
it "should return the same string" do
expect(header_string).to eq original_str
end
end
context "with combining" do
let :original_str do
"POST /foo HTTP/1.0\r\n" \
"Foo: Bar\r\n" \
"Foo: Baz\r\n" \
"Foo: Bab\r\n" \
"\r\n"
end
it "should produce an equivalent string" do
#pending "who knows"
combined = "Foo: Bar, Baz, Bab\r\n\r\n"
expect(header_string).to eq combined
end
end
end
end

View File

@ -0,0 +1,53 @@
require 'spec_helper'
require 'rex/proto/http/packet'
describe Rex::Proto::Http::Packet do
it_behaves_like "hash with insensitive keys"
describe "#parse" do
let :body do
"Super body"
end
subject do
s = described_class.new
s.parse packet_str
s
end
context "with a request packet" do
let :packet_str do
"GET / HTTP/1.0\r\n" \
"Foo: Bar\r\n" \
"Content-Length: #{body.length}\r\n" \
"\r\n" \
"#{body}"
end
it "should have correct headers" do
subject["foo"].should == "Bar"
subject["Content-Length"].should == body.length.to_s
subject.cmd_string.should == "GET / HTTP/1.0\r\n"
subject.body.should == body
end
end
context "with a response packet" do
let :packet_str do
"HTTP/1.0 200 OK\r\n" \
"Foo: Bar\r\n" \
"Content-Length: #{body.length}\r\n" \
"\r\n" \
"#{body}"
end
it "should have correct headers" do
subject["foo"].should == "Bar"
subject["Content-Length"].should == body.length.to_s
subject.cmd_string.should == "HTTP/1.0 200 OK\r\n"
subject.body.should == body
end
end
end
end

View File

@ -0,0 +1,21 @@
shared_examples_for "hash with insensitive keys" do
it "should store with insensitive key" do
subject["asdf"] = "foo"
subject["ASDF"] = "bar"
subject["asdf"].should == "bar"
subject["ASDF"].should == "bar"
end
it "should fetch with insensitive key" do
subject["foo"] = "bar"
subject["foo"].should == "bar"
subject["Foo"].should == "bar"
subject["FOo"].should == "bar"
subject["FOO"].should == "bar"
subject["fOO"].should == "bar"
subject["fOo"].should == "bar"
subject["FOo"].should == "bar"
subject["Foo"].should == "bar"
end
end