Obsolete
/
ipcd
Archived
3
0
Fork 0

Adding some tests on websockets.

master
Philippe PITTOLI 2020-01-16 17:43:02 +01:00
parent 6a871e8267
commit 7d554b0e67
4 changed files with 159 additions and 0 deletions

View File

@ -34,4 +34,10 @@ targets:
websocketc:
main: src/websocketc.cr
# tests
test-ws-pong:
main: tests/pong.cr
test-closing:
main: tests/closing.cr
license: ISC

24
tests/closing.cr Normal file
View File

@ -0,0 +1,24 @@
require "option_parser"
require "./test-ws"
uri = "ws://localhost:1234/pong.JSON"
OptionParser.parse do |parser|
parser.on "-u uri", "--uri uri", "URI" do |opturi|
uri = opturi
end
parser.on "-h", "--help", "Show this help" do
puts parser
exit 0
end
end
begin
tws = TestWS.new uri
puts "connection done: closing it"
tws.close
tws.read
rescue e
puts "Exception: #{e}"
end

72
tests/pong.cr Normal file
View File

@ -0,0 +1,72 @@
require "option_parser"
require "./test-ws"
uri = "ws://localhost:1234/pong.JSON"
rounds = 5
OptionParser.parse do |parser|
parser.on "-r rounds", "--rounds rounds", "Rounds" do |r|
rounds = r.to_i
end
parser.on "-u uri", "--uri uri", "URI" do |opturi|
uri = opturi
end
parser.on "-h", "--help", "Show this help" do
puts parser
exit 0
end
end
def multiple_messages_and_responses(tws : TestWS, data)
tws.send 1, data
tws.send 2, data
tws.send 3, data
tws.send 4, data
tws.send 5, data
response = tws.read
puts "1. received a message: #{response}"
pp! response
response = tws.read
puts "2. received a message: #{response}"
pp! response
response = tws.read
puts "3. received a message: #{response}"
pp! response
response = tws.read
puts "4. received a message: #{response}"
pp! response
response = tws.read
puts "5. received a message: #{response}"
pp! response
end
def single_message_and_response(tws : TestWS, data)
tws.send 1, data
response = tws.read
pp! response
end
begin
tws = TestWS.new uri
# puts "connection done: sending pong"
data = STDIN.gets_to_end
data = data.chomp
rounds.times do |i|
single_message_and_response tws, "#{data} #{i}"
end
# puts "closing the connection"
tws.close
tws.read
rescue e
puts "Exception: #{e}"
end

57
tests/test-ws.cr Normal file
View File

@ -0,0 +1,57 @@
require "http/web_socket"
require "../src/colors"
require "../src/utils"
require "../src/ws"
require "json"
class TestWS
property ws : WebSocket
property is_json : Bool
def initialize(uri : String)
@ws = WebSocket.new(URI.parse(uri))
@is_json = uri.ends_with? ".JSON"
@ws.on_close do |socket|
raise "socket is closing"
end
end
def read
m = @ws.read
if m.nil?
raise "empty message"
end
# remove ping messages, they are not application-relevent
while m.is_a?(HTTP::WebSocket::Ping)
puts "received a ping message, skipping"
m = @ws.read
if m.nil?
raise "empty message"
end
end
m
end
def send(type : Int32, data : String | Slice)
m = to_message type, data
# quick hack to send json messages
if @is_json
json_message = data.chomp
final_json_message = "{ \"mtype\": #{type}, \"payload\": \"#{json_message}\" }"
# puts "message: #{final_json_message}"
m = final_json_message
end
@ws.send m
end
def close
@ws.close
end
end