Skip to content

Commit 6a2a85d

Browse files
author
HD Moore
committed
Land rapid7#3789, adds specs for Rex::Proto::Http::Packet::Header
orts
2 parents 83bf220 + f68628c commit 6a2a85d

File tree

5 files changed

+164
-1
lines changed

5 files changed

+164
-1
lines changed

lib/rex/proto/http/packet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module ParseState
3232
Completed = 3
3333
end
3434

35-
require 'rex/proto/http/header'
35+
require 'rex/proto/http/packet/header'
3636

3737
#
3838
# Initializes an instance of an HTTP packet.
File renamed without changes.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
require 'spec_helper'
3+
require 'rex/proto/http/packet/header'
4+
5+
describe Rex::Proto::Http::Packet::Header do
6+
7+
it_behaves_like "hash with insensitive keys"
8+
9+
let :original_str do
10+
"POST /foo HTTP/1.0\r\n" \
11+
"Content-Length: 0\r\n" \
12+
"Foo: Bar\r\n" \
13+
"Bar: Baz\r\n" \
14+
"Combine-me: one\r\n" \
15+
"Combine-me: two\r\n" \
16+
"\r\n"
17+
end
18+
19+
describe "#from_s" do
20+
subject(:headers) do
21+
h = described_class.new
22+
h.from_s(original_str)
23+
h
24+
end
25+
26+
it "should create keys and values for each header" do
27+
expect(headers['Foo']).to eq "Bar"
28+
expect(headers['Content-Length']).to eq "0"
29+
end
30+
31+
it "should combine headers" do
32+
expect(headers['Combine-me']).to eq "one, two"
33+
end
34+
35+
context "with folding" do
36+
let :original_str do
37+
"POST /foo HTTP/1.0\r\n" \
38+
"Spaces:\r\n" \
39+
" Bar\r\n" \
40+
"Tabs:\r\n" \
41+
"\tBar\r\n" \
42+
"\r\n"
43+
end
44+
it "should recognize spaces" do
45+
expect(headers['Spaces']).to eq "Bar"
46+
end
47+
it "should recognize tabs" do
48+
expect(headers['Tabs']).to eq "Bar"
49+
end
50+
end
51+
52+
end
53+
54+
describe "#to_s" do
55+
subject(:header_string) do
56+
h = described_class.new
57+
h.from_s(original_str)
58+
h.to_s
59+
end
60+
61+
context "without combining" do
62+
let :original_str do
63+
"POST /foo HTTP/1.0\r\n" \
64+
"Foo: Bar\r\n" \
65+
"Bar: Baz\r\n" \
66+
"\r\n"
67+
end
68+
69+
it "should return the same string" do
70+
expect(header_string).to eq original_str
71+
end
72+
end
73+
context "with combining" do
74+
let :original_str do
75+
"POST /foo HTTP/1.0\r\n" \
76+
"Foo: Bar\r\n" \
77+
"Foo: Baz\r\n" \
78+
"Foo: Bab\r\n" \
79+
"\r\n"
80+
end
81+
it "should produce an equivalent string" do
82+
#pending "who knows"
83+
combined = "Foo: Bar, Baz, Bab\r\n\r\n"
84+
expect(header_string).to eq combined
85+
end
86+
end
87+
end
88+
89+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
require 'spec_helper'
3+
require 'rex/proto/http/packet'
4+
5+
describe Rex::Proto::Http::Packet do
6+
it_behaves_like "hash with insensitive keys"
7+
8+
describe "#parse" do
9+
let :body do
10+
"Super body"
11+
end
12+
subject do
13+
s = described_class.new
14+
s.parse packet_str
15+
16+
s
17+
end
18+
context "with a request packet" do
19+
let :packet_str do
20+
"GET / HTTP/1.0\r\n" \
21+
"Foo: Bar\r\n" \
22+
"Content-Length: #{body.length}\r\n" \
23+
"\r\n" \
24+
"#{body}"
25+
end
26+
27+
it "should have correct headers" do
28+
subject["foo"].should == "Bar"
29+
subject["Content-Length"].should == body.length.to_s
30+
subject.cmd_string.should == "GET / HTTP/1.0\r\n"
31+
subject.body.should == body
32+
end
33+
end
34+
35+
context "with a response packet" do
36+
let :packet_str do
37+
"HTTP/1.0 200 OK\r\n" \
38+
"Foo: Bar\r\n" \
39+
"Content-Length: #{body.length}\r\n" \
40+
"\r\n" \
41+
"#{body}"
42+
end
43+
44+
it "should have correct headers" do
45+
subject["foo"].should == "Bar"
46+
subject["Content-Length"].should == body.length.to_s
47+
subject.cmd_string.should == "HTTP/1.0 200 OK\r\n"
48+
subject.body.should == body
49+
end
50+
end
51+
52+
end
53+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
shared_examples_for "hash with insensitive keys" do
2+
it "should store with insensitive key" do
3+
subject["asdf"] = "foo"
4+
subject["ASDF"] = "bar"
5+
6+
subject["asdf"].should == "bar"
7+
subject["ASDF"].should == "bar"
8+
end
9+
it "should fetch with insensitive key" do
10+
subject["foo"] = "bar"
11+
12+
subject["foo"].should == "bar"
13+
subject["Foo"].should == "bar"
14+
subject["FOo"].should == "bar"
15+
subject["FOO"].should == "bar"
16+
subject["fOO"].should == "bar"
17+
subject["fOo"].should == "bar"
18+
subject["FOo"].should == "bar"
19+
subject["Foo"].should == "bar"
20+
end
21+
end

0 commit comments

Comments
 (0)