Instantly share code, notes, and snippets.

Embed
What would you like to do?
Old Pusher "webhook-channel-vacated" integration test
AsyncTest.define("webhook-channel-vacated #{params[:cluster_name]}", checks: 4, timeout: params[:timeout] ) { |t|
channel_name = "channel-existence-#{rand(1000000)}"
params[:webhooks]["/#{params[:cluster_name]}/webhook-channel-vacated"]=Proc.new do |post|
next unless channel_name == (post["events"][0]["channel"] rescue nil)
begin
event_name = post["events"][0]["name"]
pattern = {
"time_ms" => matches{|x| x.kind_of?(Integer)},
"events" => [{ "channel" => channel_name, "name" => event_name }]
}
if event_name == "channel_vacated"
t.assert(4, "channel_vacated", pattern == post)
end
rescue => e
t.log "bad webhook reply #{post.to_s}"
t.fail(e.to_s)
end
true
end
# Connect to websocket
uri = params[:ws]
uri.query = "client=integration&protocol=5"
begin
ws = Faye::WebSocket::Client.new(uri.to_s)
rescue EventMachine::ConnectionError => e
t.fail(e.message)
next
end
ws.on(:open) {
ws_ip = AsyncTest.get_websocket_endpoint_ip(ws)
t.log("Websocket endpoint IP #{ws_ip}")
t.ensure { ws.close }
t.assert(1, "onopen #{ws.version}")
}
ws.on(:close) { t.log "webhook-channel-existence disconnected from #{uri.to_s}" }
# Wait for the socket to connect and subscribe to channel
ws.on(:message) do |packet|
message = JSON.parse(packet.data)
case t.incr
when 1
# First subscribe
if message["event"] == "pusher:connection_established"
t.assert(2, "onconnected")
ws.send JSON.generate({
event: "pusher:subscribe",
data: {channel: channel_name}
})
end
when 2
# And once we're subscribed, unsubscribe so that the channel_vacated webhook is send
t.assert(3, "subscribed", message["event"] == "pusher_internal:subscription_succeeded")
ws.send JSON.generate({
event: "pusher:unsubscribe",
data: {channel: channel_name}
})
else
t.error("Ordering error. Expected check 1 or 2")
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment