Class: Rex::Proto::Http::Response::UnitTest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/rex/proto/http/response.rb.ut.rb

Constant Summary collapse

Klass =
Rex::Proto::Http::Response

Instance Method Summary collapse

Instance Method Details

#test_chunkedObject



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
# File 'lib/rex/proto/http/response.rb.ut.rb', line 73

def test_chunked
	h = Klass.new

	h.headers['Foo']     = 'Fishing'
	h.headers['Chicken'] = 47
	h.auto_cl = false
	h.transfer_chunked = true


	assert_equal(
		"HTTP/1.1 200 OK\r\n" +
		"Transfer-Encoding: chunked\r\n" +
		"Foo: Fishing\r\n" +
		"Chicken: 47\r\n\r\n0\r\n\r\n", h.to_s, 'chunked w/o body'
		)

	srand(0)
	h.body = Rex::Text.rand_text_alphanumeric(100)
	assert_equal(
		"HTTP/1.1 200 OK\r\n" +
		"Transfer-Encoding: chunked\r\n" +
		"Foo: Fishing\r\n" +
		"Chicken: 47\r\n\r\n" +
		"5\r\nsv1AD\r\n7\r\n7DnJTVy\r\n5\r\nkXGYY\r\n5\r\nM6Bmn\r\n4\r\nXuYR\r\n5\r\nlZNIJ\r\n5\r\nUzQzF\r\n9\r\nPvASjYxzd\r\n5\r\nTTOng\r\n4\r\nBJ5g\r\n8\r\nfK0XjLy3\r\n6\r\nciAAk1\r\n6\r\nFmo0RP\r\n1\r\nE\r\n2\r\npq\r\n6\r\n6f4BBn\r\n4\r\np5jm\r\n1\r\n3\r\n6\r\nLuSbAO\r\n1\r\nj\r\n2\r\n1M\r\n3\r\n5qU\r\n0\r\n\r\n",
		h.to_s, 'random chunk sizes'
		)

	h.chunk_max_size = 1
	h.body = 'hi mom'
	assert_equal(
		"HTTP/1.1 200 OK\r\n" +
		"Transfer-Encoding: chunked\r\n" +
		"Foo: Fishing\r\n" +
		"Chicken: 47\r\n\r\n" +
		"1\r\nh\r\n1\r\ni\r\n1\r\n \r\n1\r\nm\r\n1\r\no\r\n1\r\nm\r\n0\r\n\r\n",
		h.to_s, '1 byte chunks'
		)

	h.chunk_min_size = 2
	assert_equal(
		"HTTP/1.1 200 OK\r\n" +
		"Transfer-Encoding: chunked\r\n" +
		"Foo: Fishing\r\n" +
		"Chicken: 47\r\n\r\n" +
		"2\r\nhi\r\n2\r\n m\r\n2\r\nom\r\n0\r\n\r\n",
		h.to_s, '2 byte chunks'
		)

	h = Klass.new(200, 'OK', '1.0')
	h.body = 'hi mom'
	h.auto_cl = false
	h.transfer_chunked = true
	assert_raise(Rex::RuntimeError, 'chunked encoding via 1.0') {
		h.to_s
	}

end

#test_deflateObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rex/proto/http/response.rb.ut.rb', line 13

def test_deflate
	h = Klass.new
	h.body = 'hi mom'
	h.compress = 'deflate'
	assert_equal(
		"HTTP/1.1 200 OK\r\n" +
		"Content-Length: 14\r\n" +
		"Content-Encoding: deflate\r\n\r\n" +
		"x\234\313\310T\310\315\317\005\000\a\225\002;",
		h.to_s, 'deflate'
		)
end

#test_from_sObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rex/proto/http/response.rb.ut.rb', line 131

def test_from_s
	h = Klass.new

	h.from_s(
		"HTTP/1.0 404 File not found\r\n" +
		"Lucifer: Beast\r\n" +
		"HoHo: Satan\r\n" +
		"Eat: Babies\r\n" +
		"\r\n")

	assert_equal(404, h.code)
	assert_equal('File not found', h.message)
	assert_equal('1.0', h.proto)
	assert_equal("HTTP/1.0 404 File not found\r\n", h.cmd_string)
	h.code = 470
	assert_equal("HTTP/1.0 470 File not found\r\n", h.cmd_string)
	assert_equal('Babies', h['Eat'])
end

#test_gzipObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rex/proto/http/response.rb.ut.rb', line 26

def test_gzip
	h = Klass.new
	h.body = 'hi mom'
	h.compress = 'gzip'
	srand(0)

	response = h.to_s

	http_header = response.slice!(0,63)

	assert_equal(
		"HTTP/1.1 200 OK\r\n" +
		"Content-Length: 26\r\n"+
		"Content-Encoding: gzip\r\n\r\n",
		http_header, 'http headers'
		)

	assert_equal("\x1f\x8b\x08\x00", response.slice!(0,4), 'gzip headers')

	# skip the next 6 bytes as it is host & time specific (zlib's example gun does, so why not us too?)
	response.slice!(0,6)

	assert_equal("\xcb\xc8\x54\xc8\xcd\xcf\x05\x00\x68\xa4\x1c\xf0\x06\x00\x00\x00", response, 'gzip data')
end

#test_to_sObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rex/proto/http/response.rb.ut.rb', line 52

def test_to_s
	h = Klass.new

	h.headers['Foo']     = 'Fishing'
	h.headers['Chicken'] = 47

	assert_equal(
		"HTTP/1.1 200 OK\r\n" +
		"Content-Length: 0\r\n" +
		"Foo: Fishing\r\n" +
		"Chicken: 47\r\n\r\n", h.to_s, 'to_s w/o body')

	h.body = 'hi mom'
	assert_equal(
		"HTTP/1.1 200 OK\r\n" +
		"Content-Length: 6\r\n" +
		"Foo: Fishing\r\n" +
		"Chicken: 47\r\n\r\nhi mom", h.to_s, 'to_s w/ body')
end