CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/rex/proto/nuuo/client_request_spec.rb
Views: 11789
1
# -*- coding:binary -*-
2
3
RSpec.describe Rex::Proto::Nuuo::ClientRequest do
4
subject(:client_request) {
5
opts = {
6
'user_session' => user_session,
7
'headers' => headers_hash,
8
'data' => data
9
}
10
described_class.new(opts)
11
}
12
let(:user_session) {nil}
13
let(:headers_hash) {{}}
14
let(:data) {nil}
15
16
describe '#to_s' do
17
context 'given no additional options' do
18
it 'returns a USERLOGIN request' do
19
expect(client_request.to_s).to eq("USERLOGIN NUCM/1.0\r\n\r\n")
20
end
21
end
22
23
context 'given a headers hash' do
24
let(:headers_hash) {{
25
'TestHeader' => 'TestValue',
26
'TestHeader1' => 'TestValue1'
27
}}
28
it 'dumps the headers after the method line' do
29
expect(client_request.to_s).to eq("USERLOGIN NUCM/1.0\r\nTestHeader: TestValue\r\nTestHeader1: TestValue1\r\n\r\n")
30
end
31
end
32
33
context 'given a user_session and User-Session-No header' do
34
let(:user_session) {'0'}
35
let(:headers_hash) {{'User-Session-No' => '1'}}
36
37
it 'prefers the User-Session-No in the headers hash' do
38
expect(client_request.to_s).to eq("USERLOGIN NUCM/1.0\r\nUser-Session-No: 1\r\n\r\n")
39
end
40
end
41
end
42
43
describe '#set_method' do
44
it 'returns the method variable' do
45
expect(client_request.set_method).to eq('USERLOGIN')
46
end
47
end
48
49
describe '#set_proto_version' do
50
it 'returns the protocol and version separated by /' do
51
expect(client_request.set_proto_version).to eq("NUCM/1.0\r\n")
52
end
53
end
54
55
56
describe '#set_header' do
57
58
context 'given no user session number' do
59
let(:user_session) {nil}
60
61
it 'returns an empty header' do
62
expect(client_request.set_header('user_session', 'User-Session-No')).to eq('')
63
end
64
end
65
66
context 'given user session number' do
67
let(:user_session) {'987'}
68
69
it 'returns a User-Session-No header' do
70
expect(client_request.set_header('user_session', 'User-Session-No')).to eq("User-Session-No: 987\r\n")
71
end
72
end
73
74
context 'given a nonexistent key' do
75
it 'returns an empty header' do
76
expect(client_request.set_header('DoesNotExist', 'DoesNotExist')).to eq('')
77
end
78
end
79
80
context 'given a key specified in the headers hash' do
81
let(:user_session) {'987'}
82
let(:headers_hash) {{'User-Session-No' => '1000'}}
83
84
it 'returns an empty header' do
85
expect(client_request.set_header('user_session', 'User-Session-No')).to eq('')
86
end
87
end
88
89
end
90
91
describe '#set_extra_headers' do
92
context 'given an empty headers hash' do
93
it 'returns an empty string' do
94
expect(client_request.set_extra_headers).to eq('')
95
end
96
end
97
98
context 'given a headers hash' do
99
let(:headers_hash) {{
100
'Header' => 'Value',
101
'Another' => 'One'
102
}}
103
104
it 'returns formatted headers' do
105
expect(client_request.set_extra_headers).to eq("Header: Value\r\nAnother: One\r\n")
106
end
107
end
108
end
109
110
describe '#set_body' do
111
context 'given an empty body variable' do
112
it 'returns \r\n' do
113
expect(client_request.set_body).to eq("\r\n")
114
end
115
end
116
117
context 'given body content' do
118
let(:data) {"test data"}
119
120
it 'returns \r\n followed by the body content' do
121
expect(client_request.set_body).to eq("\r\ntest data")
122
end
123
end
124
end
125
126
describe '#set_formatted_header' do
127
let(:name) {'HeaderName'}
128
let(:value) {'HeaderValue'}
129
130
it 'creates a request header' do
131
expect(subject.set_formatted_header(name, value)).to eq("HeaderName: HeaderValue\r\n")
132
end
133
end
134
end
135
136