rhcp 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +116 -0
- data/lib/rhcp/broker.rb +37 -0
- data/lib/rhcp/client/command_param_stub.rb +51 -0
- data/lib/rhcp/client/command_stub.rb +56 -0
- data/lib/rhcp/client/http_broker.rb +86 -0
- data/lib/rhcp/command.rb +120 -0
- data/lib/rhcp/command_param.rb +93 -0
- data/lib/rhcp/dispatching_broker.rb +25 -0
- data/lib/rhcp/http_exporter.rb +157 -0
- data/lib/rhcp/request.rb +93 -0
- data/lib/rhcp/response.rb +62 -0
- data/lib/rhcp/rhcp_exception.rb +6 -0
- data/lib/rhcp.rb +47 -0
- data/test/rhcp/broker_test.rb +29 -0
- data/test/rhcp/client/command_param_stub_test.rb +73 -0
- data/test/rhcp/client/command_stub_test.rb +40 -0
- data/test/rhcp/command_param_test.rb +72 -0
- data/test/rhcp/command_test.rb +136 -0
- data/test/rhcp/dispatching_broker_test.rb +41 -0
- data/test/rhcp/http_exporter_test.rb +113 -0
- data/test/rhcp/http_registry_test.rb +73 -0
- data/test/rhcp/http_test_server.rb +47 -0
- data/test/rhcp/request_test.rb +124 -0
- data/test/rhcp/response_test.rb +45 -0
- metadata +87 -0
@@ -0,0 +1,136 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rhcp'
|
5
|
+
|
6
|
+
class CommandTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def command_method(request, response)
|
9
|
+
first_param = request.get_param_value("first_param")
|
10
|
+
puts "just testing : #{first_param}"
|
11
|
+
first_param.reverse
|
12
|
+
end
|
13
|
+
|
14
|
+
def command_method_with_error(request, response)
|
15
|
+
raise "got a problem here!"
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_construction
|
19
|
+
command = RHCP::Command.new("test", "a command for testing", self.method(:command_method))
|
20
|
+
assert_not_nil command
|
21
|
+
assert_equal "test", command.name
|
22
|
+
assert_equal "a command for testing", command.description
|
23
|
+
assert_not_nil command.params
|
24
|
+
assert_instance_of Hash, command.params
|
25
|
+
assert_equal 0, command.params.size
|
26
|
+
|
27
|
+
command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
|
28
|
+
command.add_param(RHCP::CommandParam.new("second_param", "this is the second param"))
|
29
|
+
assert_equal 2, command.params.size
|
30
|
+
first_param = command.get_param("first_param")
|
31
|
+
assert_not_nil first_param
|
32
|
+
assert_equal "first_param", first_param.name
|
33
|
+
assert_equal "this is the first param", first_param.description
|
34
|
+
assert_raise(RHCP::RhcpException) { command.get_param("does not exist") }
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_execute_request
|
38
|
+
command = RHCP::Command.new("test", "a command for testing", self.method(:command_method))
|
39
|
+
command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
|
40
|
+
request = RHCP::Request.new(command, { "first_param" => [ "the_value" ] })
|
41
|
+
response = command.execute_request(request)
|
42
|
+
assert_equal "the_value".reverse, response.data
|
43
|
+
assert_equal RHCP::Response::Status::OK, response.status
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_execute_error
|
47
|
+
command = RHCP::Command.new("not working", "told you so", self.method(:command_method_with_error))
|
48
|
+
request = RHCP::Request.new(command, {})
|
49
|
+
response = command.execute_request(request)
|
50
|
+
assert_equal RHCP::Response::Status::ERROR, response.status
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_execute
|
54
|
+
command = RHCP::Command.new("test", "a command for testing", self.method(:command_method))
|
55
|
+
command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
|
56
|
+
response = command.execute({"first_param" => "thing"})
|
57
|
+
assert_equal "thing".reverse, response.data
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_duplicate_params
|
61
|
+
command = RHCP::Command.new("test_duplicate", "command for testing param duplicates", lambda {})
|
62
|
+
command.add_param(RHCP::CommandParam.new("first_param", "first param"))
|
63
|
+
assert_raise(RuntimeError) { command.add_param(RHCP::CommandParam.new("first_param", "i am a duplicate")) }
|
64
|
+
end
|
65
|
+
|
66
|
+
# it should not be possible to add more than one default param
|
67
|
+
def test_multiple_default_params
|
68
|
+
command = RHCP::Command.new("test", "command for testing multiple default params", lambda {})
|
69
|
+
command.add_param RHCP::CommandParam.new("first_param", "first param",
|
70
|
+
{
|
71
|
+
:is_default_param => true
|
72
|
+
}
|
73
|
+
)
|
74
|
+
assert_raise(RHCP::RhcpException) {
|
75
|
+
command.add_param RHCP::CommandParam.new("second_param", "second default param",
|
76
|
+
{
|
77
|
+
:is_default_param => true
|
78
|
+
}
|
79
|
+
)
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_get_default_param
|
84
|
+
command = RHCP::Command.new("test_duplicate", "command for testing get_default_param", lambda {})
|
85
|
+
assert_nil command.default_param
|
86
|
+
the_param = RHCP::CommandParam.new("first_param", "first param",
|
87
|
+
{
|
88
|
+
:is_default_param => true
|
89
|
+
}
|
90
|
+
)
|
91
|
+
command.add_param the_param
|
92
|
+
assert_equal the_param, command.default_param
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_listener
|
96
|
+
command = RHCP::Command.new("test_listener", "just some dumb command for the listener", lambda {
|
97
|
+
|req,res|
|
98
|
+
puts "While I think there could be something important to say right now, I just cannot remember what it might have been..."
|
99
|
+
res.result_text = "the test is working."
|
100
|
+
})
|
101
|
+
|
102
|
+
@listener1_fired = false
|
103
|
+
@listener2_fired = false
|
104
|
+
|
105
|
+
RHCP::Command.clear_post_exec_listeners()
|
106
|
+
RHCP::Command.register_post_exec_listener(lambda { |req,res| @listener1_fired = true })
|
107
|
+
RHCP::Command.register_post_exec_listener(lambda { |req,res| @listener2_fired = true })
|
108
|
+
|
109
|
+
command.execute()
|
110
|
+
|
111
|
+
assert @listener1_fired
|
112
|
+
assert @listener2_fired
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_failing_listener
|
116
|
+
command = RHCP::Command.new("test_listener", "just some dumb command for the listener", lambda {
|
117
|
+
|req,res|
|
118
|
+
puts "While I think there could be something important to say right now, I just cannot remember what it might have been..."
|
119
|
+
res.result_text = "the test is working"
|
120
|
+
})
|
121
|
+
|
122
|
+
RHCP::Command.clear_post_exec_listeners()
|
123
|
+
RHCP::Command.register_post_exec_listener(lambda { |req,res| raise "this listener is dying"})
|
124
|
+
|
125
|
+
@listener1_fired = false
|
126
|
+
@listener2_fired = false
|
127
|
+
|
128
|
+
response = command.execute()
|
129
|
+
assert_not_nil response
|
130
|
+
assert_equal "the test is working", response.result_text
|
131
|
+
assert ! @listener1_fired
|
132
|
+
assert ! @listener2_fired
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
#require 'rhcp/dispatching_broker'
|
6
|
+
#require 'rhcp/broker'
|
7
|
+
#require 'rhcp/command'
|
8
|
+
#require 'rhcp/command_param'
|
9
|
+
require 'rhcp'
|
10
|
+
|
11
|
+
class DispatchingBrokerTest < Test::Unit::TestCase
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@broker1 = RHCP::Broker.new()
|
15
|
+
@broker1.register_command RHCP::Command.new("test", "just testing (broker1)", lambda{})
|
16
|
+
@broker1.register_command RHCP::Command.new("test_more", "another fancy command from broker1", lambda{})
|
17
|
+
@broker2 = RHCP::Broker.new()
|
18
|
+
@broker2.register_command RHCP::Command.new("echo", "says hello (broker2)", lambda{})
|
19
|
+
@broker2.register_command RHCP::Command.new("help", "is no help at all (broker2)", lambda{})
|
20
|
+
@broker2.register_command RHCP::Command.new("red button", "don't press it (broker2)", lambda{})
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_add_brokers
|
24
|
+
dispatcher = RHCP::DispatchingBroker.new()
|
25
|
+
assert_not_nil dispatcher
|
26
|
+
assert_equal 0, dispatcher.get_command_list.size
|
27
|
+
dispatcher.add_broker(@broker1)
|
28
|
+
assert_equal 2, dispatcher.get_command_list.size
|
29
|
+
dispatcher.add_broker(@broker2)
|
30
|
+
assert_equal 5, dispatcher.get_command_list.size
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_duplicate_commands
|
34
|
+
dispatcher = RHCP::DispatchingBroker.new()
|
35
|
+
dispatcher.add_broker(@broker1)
|
36
|
+
assert_equal 2, dispatcher.get_command_list.size
|
37
|
+
assert_raise(RHCP::RhcpException) { dispatcher.add_broker(@broker1) }
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,113 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
require 'rhcp'
|
7
|
+
|
8
|
+
class HttpExporterTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
|
11
|
+
def self.suite
|
12
|
+
puts "gonna setup"
|
13
|
+
broker = RHCP::Broker.new()
|
14
|
+
broker.register_command(RHCP::Command.new("test", "just a test command", lambda { |req,res| "testing" }))
|
15
|
+
broker.register_command(
|
16
|
+
RHCP::Command.new("reverse", "reversing input strings",
|
17
|
+
lambda { |req,res| req.get_param_value("input").reverse }
|
18
|
+
).add_param(RHCP::CommandParam.new("input", "the string to reverse", {
|
19
|
+
:lookup_method => lambda { [ "zaphod", "beeblebrox" ] }
|
20
|
+
}))
|
21
|
+
)
|
22
|
+
|
23
|
+
@@broker = broker
|
24
|
+
|
25
|
+
# TODO test other setup options
|
26
|
+
@@exporter = RHCP::HttpExporter.new(broker, :port => 42000)
|
27
|
+
@@exporter.start()
|
28
|
+
super
|
29
|
+
end
|
30
|
+
|
31
|
+
def setup
|
32
|
+
@url = URI.parse("http://localhost:42000")
|
33
|
+
end
|
34
|
+
|
35
|
+
def teardown
|
36
|
+
#@@exporter.stop
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_available
|
40
|
+
res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/") }
|
41
|
+
assert_equal "200", res.code
|
42
|
+
end
|
43
|
+
|
44
|
+
# def test_info
|
45
|
+
# sleep 2
|
46
|
+
# res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/info/index2.html") }
|
47
|
+
#
|
48
|
+
# puts res.body
|
49
|
+
# assert_equal "200", res.code
|
50
|
+
# end
|
51
|
+
|
52
|
+
def test_get_commands
|
53
|
+
res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_commands") }
|
54
|
+
|
55
|
+
assert_equal "200", res.code
|
56
|
+
commands = JSON.parse(res.body)
|
57
|
+
|
58
|
+
# +commands+ is an array => convert it back to a has
|
59
|
+
command_hash = Hash.new()
|
60
|
+
commands.each do |command_string|
|
61
|
+
command = RHCP::Client::CommandStub.reconstruct_from_json(command_string)
|
62
|
+
command_hash[command.name] = command
|
63
|
+
end
|
64
|
+
|
65
|
+
assert_equal [ "test", "reverse" ].sort, command_hash.keys.sort
|
66
|
+
assert_kind_of RHCP::Command, command_hash["reverse"]
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_get_lookup_values
|
70
|
+
res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_lookup_values?command=reverse¶m=input") }
|
71
|
+
puts res.body
|
72
|
+
assert_equal "200", res.code
|
73
|
+
lookups = JSON.parse(res.body)
|
74
|
+
assert_equal [ "zaphod", "beeblebrox" ].sort, lookups.sort
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_get_lookup_values_invalid_command
|
78
|
+
res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_lookup_values?command=do_the_twist") }
|
79
|
+
puts "error response #{res.body}"
|
80
|
+
assert_equal "500", res.code
|
81
|
+
result = JSON.parse(res.body)
|
82
|
+
assert(/^no such command/.match(result[0]))
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_get_lookup_values_without_params
|
86
|
+
res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_lookup_values") }
|
87
|
+
puts "error response #{res.body}"
|
88
|
+
assert_equal "500", res.code
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_get_lookup_values_partial
|
92
|
+
res = Net::HTTP.new(@url.host, @url.port).start { |http| http.get("/rhcp/get_lookup_values?command=reverse¶m=input&partial=zap") }
|
93
|
+
puts res.body
|
94
|
+
assert_equal "200", res.code
|
95
|
+
lookups = JSON.parse(res.body)
|
96
|
+
assert_equal [ "zaphod" ].sort, lookups.sort
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_execute
|
100
|
+
request = RHCP::Request.new(@@broker.get_command("reverse"), {
|
101
|
+
"input" => [ "zaphod" ]
|
102
|
+
})
|
103
|
+
|
104
|
+
res = Net::HTTP.new(@url.host, @url.port).start { |http| http.post("/rhcp/execute", request.to_json) }
|
105
|
+
puts "execute response #{res.body}"
|
106
|
+
assert_equal "200", res.code
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_execute_fails
|
110
|
+
# TODO write test
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
#
|
2
|
+
# To change this template, choose Tools | Templates
|
3
|
+
# and open the template in the editor.
|
4
|
+
|
5
|
+
|
6
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
7
|
+
|
8
|
+
require 'test/unit'
|
9
|
+
require 'rhcp'
|
10
|
+
|
11
|
+
class HttpBrokerTest < Test::Unit::TestCase
|
12
|
+
|
13
|
+
def self.suite
|
14
|
+
# setup an http exporter we can connect to
|
15
|
+
puts "gonna setup"
|
16
|
+
broker = RHCP::Broker.new()
|
17
|
+
@@command_test = RHCP::Command.new("test", "just a test command", lambda { |req,res| "testing" })
|
18
|
+
broker.register_command @@command_test
|
19
|
+
|
20
|
+
@@command_reverse = RHCP::Command.new("reverse", "reversing input strings",
|
21
|
+
lambda { |req,res|
|
22
|
+
req.get_param_value("input").reverse
|
23
|
+
}
|
24
|
+
).add_param(RHCP::CommandParam.new("input", "the string to reverse",
|
25
|
+
{
|
26
|
+
:lookup_method => lambda { [ "zaphod", "beeblebrox" ] }
|
27
|
+
}
|
28
|
+
)
|
29
|
+
)
|
30
|
+
broker.register_command @@command_reverse
|
31
|
+
|
32
|
+
@@exporter = RHCP::HttpExporter.new(broker, :port => 42001)
|
33
|
+
@@exporter.start()
|
34
|
+
super
|
35
|
+
end
|
36
|
+
|
37
|
+
def setup
|
38
|
+
url = URI.parse("http://localhost:42001")
|
39
|
+
@http_broker = RHCP::Client::HttpBroker.new(url)
|
40
|
+
assert_not_nil @http_broker
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_get_commands
|
44
|
+
commands = @http_broker.get_command_list
|
45
|
+
assert_not_nil commands
|
46
|
+
assert 2, commands.size
|
47
|
+
assert @@command_test, commands["test"]
|
48
|
+
assert @@command_reverse, commands["reverse"]
|
49
|
+
puts "command reverse : >>#{commands["reverse"]}<<"
|
50
|
+
end
|
51
|
+
|
52
|
+
# the CommandParam should be wrapped in a way that we can retrieve lookup values
|
53
|
+
def test_get_lookup_values
|
54
|
+
command = @http_broker.get_command_list["reverse"]
|
55
|
+
param = command.get_param("input")
|
56
|
+
assert_equal [ "zaphod", "beeblebrox" ], param.get_lookup_values()
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_execute
|
60
|
+
command = @http_broker.get_command_list["reverse"]
|
61
|
+
assert_instance_of RHCP::Client::CommandStub, command
|
62
|
+
collected_params = {
|
63
|
+
"input" => [ "zaphod" ]
|
64
|
+
}
|
65
|
+
request = RHCP::Request.new(command, collected_params)
|
66
|
+
response = command.execute_request(request)
|
67
|
+
assert_not_nil response
|
68
|
+
assert_equal RHCP::Response::Status::OK, response.status
|
69
|
+
assert_equal "dohpaz", response.data
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
+
|
3
|
+
require 'rhcp'
|
4
|
+
|
5
|
+
require 'net/http'
|
6
|
+
require 'logger'
|
7
|
+
|
8
|
+
$logger = Logger.new($stdout)
|
9
|
+
|
10
|
+
broker = RHCP::Broker.new()
|
11
|
+
broker.register_command(RHCP::Command.new("test", "just a test command", lambda { |req,res| "testing" }))
|
12
|
+
broker.register_command(
|
13
|
+
RHCP::Command.new("reverse", "reversing input strings",
|
14
|
+
lambda { |req,res| req.get_param_value("input").reverse }
|
15
|
+
).add_param(RHCP::CommandParam.new("input", "the string to reverse", {
|
16
|
+
:lookup_method => lambda { [ "zaphod", "beeblebrox" ] }
|
17
|
+
}))
|
18
|
+
)
|
19
|
+
broker.register_command RHCP::Command.new("cook", "cook something nice out of some ingredients",
|
20
|
+
lambda { |req,res|
|
21
|
+
ingredients = req.get_param_value("ingredient").join(" ")
|
22
|
+
puts "cooking something with #{ingredients}"
|
23
|
+
ingredients
|
24
|
+
}
|
25
|
+
).add_param(RHCP::CommandParam.new("ingredient", "something to cook with",
|
26
|
+
{
|
27
|
+
:lookup_method => lambda { [ "mascarpone", "chocolate", "eggs", "butter", "marzipan" ] },
|
28
|
+
:allows_multiple_values => true,
|
29
|
+
:mandatory => true
|
30
|
+
}
|
31
|
+
)
|
32
|
+
)
|
33
|
+
|
34
|
+
exporter = RHCP::HttpExporter.new(broker, :port => 42000)
|
35
|
+
|
36
|
+
trap("INT") {
|
37
|
+
exporter.stop
|
38
|
+
Kernel.exit 0
|
39
|
+
}
|
40
|
+
|
41
|
+
$logger.info "launching http exporter..."
|
42
|
+
exporter.start()
|
43
|
+
while (true) do
|
44
|
+
sleep 30
|
45
|
+
puts "."
|
46
|
+
end
|
47
|
+
$logger.info "exiting"
|
@@ -0,0 +1,124 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rhcp'
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'json'
|
8
|
+
|
9
|
+
class RequestTest < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def test_construction
|
12
|
+
command = RHCP::Command.new("test", "a test", nil)
|
13
|
+
assert_raise(RHCP::RhcpException) { RHCP::Request.new(nil, Hash.new()) }
|
14
|
+
request = RHCP::Request.new(command, Hash.new())
|
15
|
+
assert_not_nil request
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_get_param_value
|
19
|
+
command = RHCP::Command.new("test", "a test", nil)
|
20
|
+
command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
|
21
|
+
command.add_param(RHCP::CommandParam.new("second_param", "this is the second param", { :allows_multiple_values => true }))
|
22
|
+
request = RHCP::Request.new(
|
23
|
+
command,
|
24
|
+
{
|
25
|
+
"first_param" => ["foo"],
|
26
|
+
"second_param" => ["foo", "bar", "baz"]
|
27
|
+
}
|
28
|
+
)
|
29
|
+
assert_not_nil request
|
30
|
+
assert_equal "foo", request.get_param_value("first_param")
|
31
|
+
assert_equal ["foo", "bar", "baz"], request.get_param_value("second_param")
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_has_param_value
|
35
|
+
command = RHCP::Command.new("test", "a test", nil)
|
36
|
+
command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
|
37
|
+
command.add_param(RHCP::CommandParam.new("second_param", "this is the second param", { :allows_multiple_values => true }))
|
38
|
+
request = RHCP::Request.new(
|
39
|
+
command,
|
40
|
+
{
|
41
|
+
"first_param" => ["foo"],
|
42
|
+
"second_param" => ["foo", "bar", "baz"]
|
43
|
+
}
|
44
|
+
)
|
45
|
+
assert request.has_param_value("first_param")
|
46
|
+
assert ! request.has_param_value("third_param")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_invalid_param_name
|
50
|
+
command = RHCP::Command.new("test", "another test", lambda {})
|
51
|
+
command.add_param(RHCP::CommandParam.new("real_param", "this param does exist"))
|
52
|
+
assert_not_nil RHCP::Request.new(command, { "real_param" => [ "value for the real param" ] })
|
53
|
+
assert_raise(RHCP::RhcpException) { RHCP::Request.new(command, {
|
54
|
+
"does_not_exist" => [ "single value for non-existing param" ]
|
55
|
+
}) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_invalid_param_values
|
59
|
+
command = RHCP::Command.new("test", "another test", lambda {})
|
60
|
+
command.add_param(RHCP::CommandParam.new("real_param", "this param does exist"))
|
61
|
+
assert_raise(RHCP::RhcpException) { RHCP::Request.new(command, {
|
62
|
+
"real_param" => [ "value for the real param", "another value for the real param" ]
|
63
|
+
}) }
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_missing_mandatory_param
|
67
|
+
command = RHCP::Command.new("test", "another test", lambda {})
|
68
|
+
command.add_param(RHCP::CommandParam.new("first_param", "this is the first param", { :mandatory => true }))
|
69
|
+
command.add_param(RHCP::CommandParam.new("second_param", "this param is optional", { :mandatory => false }))
|
70
|
+
|
71
|
+
# test1 : we should not have to specify optional parameters
|
72
|
+
assert_not_nil RHCP::Request.new(command, { "first_param" => [ "bla" ]})
|
73
|
+
|
74
|
+
# test2 : but we need to specify mandatory params
|
75
|
+
assert_raise(RHCP::RhcpException) { RHCP::Request.new(command, {})}
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_json
|
79
|
+
command = RHCP::Command.new("request_test", "a test", nil)
|
80
|
+
command.add_param(RHCP::CommandParam.new("first_param", "this is the first param"))
|
81
|
+
command.add_param(RHCP::CommandParam.new("second_param", "this is the second param", { :allows_multiple_values => true }))
|
82
|
+
broker = RHCP::Broker.new()
|
83
|
+
broker.register_command(command)
|
84
|
+
r= RHCP::Request.new(
|
85
|
+
command,
|
86
|
+
{
|
87
|
+
"first_param" => ["foo"],
|
88
|
+
"second_param" => ["foo", "bar", "baz"]
|
89
|
+
}
|
90
|
+
)
|
91
|
+
json = r.to_json
|
92
|
+
puts "request as JSON : >>#{json}<<"
|
93
|
+
assert_not_nil json
|
94
|
+
r2 = RHCP::Request.reconstruct_from_json(broker, json)
|
95
|
+
assert_instance_of RHCP::Request, r2
|
96
|
+
assert_equal r.command, r2.command
|
97
|
+
assert_equal r.param_values, r2.param_values
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_execute
|
101
|
+
command = RHCP::Command.new("request_test", "a test", lambda { |req,res| "jehova" })
|
102
|
+
r= RHCP::Request.new(
|
103
|
+
command
|
104
|
+
)
|
105
|
+
res = r.execute
|
106
|
+
assert_not_nil res
|
107
|
+
assert_equal "jehova", res.data
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_execute_with_params
|
111
|
+
command = RHCP::Command.new("request_test", "a test", lambda { |req,res| "***" + req.get_param_value("first_one") + "***" }
|
112
|
+
).add_param(RHCP::CommandParam.new("first_one", "pole position"))
|
113
|
+
r= RHCP::Request.new(
|
114
|
+
command,
|
115
|
+
{
|
116
|
+
"first_one" => "lucky bastard"
|
117
|
+
}
|
118
|
+
)
|
119
|
+
res = r.execute()
|
120
|
+
assert_not_nil res
|
121
|
+
assert_equal "***lucky bastard***", res.data
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
#
|
2
|
+
# To change this template, choose Tools | Templates
|
3
|
+
# and open the template in the editor.
|
4
|
+
|
5
|
+
|
6
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
7
|
+
|
8
|
+
require 'test/unit'
|
9
|
+
require 'rhcp/response'
|
10
|
+
|
11
|
+
class ResponseTest < Test::Unit::TestCase
|
12
|
+
|
13
|
+
def test_simple
|
14
|
+
response = RHCP::Response.new()
|
15
|
+
assert_not_nil response
|
16
|
+
response.mark_as_error("something went wrong")
|
17
|
+
assert_equal "something went wrong", response.error_text
|
18
|
+
assert_equal "", response.error_detail
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_payload
|
22
|
+
response = RHCP::Response.new()
|
23
|
+
assert_not_nil response
|
24
|
+
response.set_payload("some data")
|
25
|
+
assert_equal "some data", response.data
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_json
|
29
|
+
response = RHCP::Response.new()
|
30
|
+
response.set_payload("some data")
|
31
|
+
response.mark_as_error("something went wrong", "things got really fucked up")
|
32
|
+
|
33
|
+
json = response.to_json()
|
34
|
+
assert_not_nil json
|
35
|
+
r2 = RHCP::Response.reconstruct_from_json(json)
|
36
|
+
#r2 = JSON.parse(json)
|
37
|
+
assert_not_nil r2
|
38
|
+
assert_instance_of RHCP::Response, r2
|
39
|
+
assert_equal RHCP::Response::Status::ERROR, r2.status
|
40
|
+
assert_equal "something went wrong", r2.error_text
|
41
|
+
assert_equal "things got really fucked up", r2.error_detail
|
42
|
+
assert_equal "some data", r2.data
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: rhcp
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.2
|
7
|
+
date: 2008-12-07 00:00:00 +01:00
|
8
|
+
summary: Library for exporting parts of your application using the rhcp protocol
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: philipp@hitchhackers.net
|
12
|
+
homepage: http://hitchhackers.net/projects/rhcp
|
13
|
+
rubyforge_project: rhcp
|
14
|
+
description: The rhcp protocol allows you to register commands along with their parameter descriptions and some metadata that can then be executed by rhcp clients.
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: true
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message: |+
|
29
|
+
|
30
|
+
This is the initial release of rhcp - hope you'll like it.
|
31
|
+
Feel free to send me feedback to rhcp at hitchhackers dot net.
|
32
|
+
|
33
|
+
authors:
|
34
|
+
- Philipp Traeder
|
35
|
+
files:
|
36
|
+
- Rakefile
|
37
|
+
- lib/rhcp
|
38
|
+
- lib/rhcp/client
|
39
|
+
- lib/rhcp/client/http_broker.rb
|
40
|
+
- lib/rhcp/client/command_param_stub.rb
|
41
|
+
- lib/rhcp/client/command_stub.rb
|
42
|
+
- lib/rhcp/command.rb
|
43
|
+
- lib/rhcp/command_param.rb
|
44
|
+
- lib/rhcp/response.rb
|
45
|
+
- lib/rhcp/dispatching_broker.rb
|
46
|
+
- lib/rhcp/broker.rb
|
47
|
+
- lib/rhcp/http_exporter.rb
|
48
|
+
- lib/rhcp/request.rb
|
49
|
+
- lib/rhcp/rhcp_exception.rb
|
50
|
+
- lib/rhcp.rb
|
51
|
+
- test/rhcp
|
52
|
+
- test/rhcp/client
|
53
|
+
- test/rhcp/client/command_param_stub_test.rb
|
54
|
+
- test/rhcp/client/command_stub_test.rb
|
55
|
+
- test/rhcp/command_param_test.rb
|
56
|
+
- test/rhcp/command_test.rb
|
57
|
+
- test/rhcp/response_test.rb
|
58
|
+
- test/rhcp/dispatching_broker_test.rb
|
59
|
+
- test/rhcp/broker_test.rb
|
60
|
+
- test/rhcp/http_test_server.rb
|
61
|
+
- test/rhcp/http_exporter_test.rb
|
62
|
+
- test/rhcp/http_registry_test.rb
|
63
|
+
- test/rhcp/request_test.rb
|
64
|
+
test_files:
|
65
|
+
- test/rhcp/client/command_param_stub_test.rb
|
66
|
+
- test/rhcp/client/command_stub_test.rb
|
67
|
+
- test/rhcp/command_param_test.rb
|
68
|
+
- test/rhcp/command_test.rb
|
69
|
+
- test/rhcp/response_test.rb
|
70
|
+
- test/rhcp/dispatching_broker_test.rb
|
71
|
+
- test/rhcp/broker_test.rb
|
72
|
+
- test/rhcp/http_test_server.rb
|
73
|
+
- test/rhcp/http_exporter_test.rb
|
74
|
+
- test/rhcp/http_registry_test.rb
|
75
|
+
- test/rhcp/request_test.rb
|
76
|
+
rdoc_options: []
|
77
|
+
|
78
|
+
extra_rdoc_files: []
|
79
|
+
|
80
|
+
executables: []
|
81
|
+
|
82
|
+
extensions: []
|
83
|
+
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
dependencies: []
|
87
|
+
|