Skip to content

Commit

Permalink
tests(proxy) websocket with own nginx (Kong#2737)
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito authored Jul 27, 2017
1 parent 6687565 commit 2435b0d
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 27 deletions.
90 changes: 63 additions & 27 deletions spec/02-integration/05-proxy/08-websockets_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,87 @@ local client = require "resty.websocket.client"
local helpers = require "spec.helpers"
local cjson = require "cjson"

pending("Websockets", function()
-- Pending (2017/06/16)
-- Since sockb.in appears to be offline, we'll need to find a way to test
-- WebSocket proxying support differently.
-- * Use another service
-- * Spawn our own sockb.in instance on Heroku
-- * Compile our test Nginx with the stream module for local testing (ideal)

describe("Websockets", function()
setup(function()
assert(helpers.dao.apis:insert {
name = "ws",
uris = { "/ws" },
uris = { "/up-ws" },
strip_uri = true,
upstream_url = "http://sockb.in"
upstream_url = "http://127.0.0.1:55555/ws",
})

assert(helpers.start_kong())
assert(helpers.start_kong({
nginx_conf = "spec/fixtures/custom_nginx.template",
}))
end)

teardown(function()
helpers.stop_kong()
end)

local function make_request(uri)
local wb = assert(client:new())
assert(wb:connect(uri))
assert(wb:send_text("testing Kong"))
local function open_socket(uri)
local wc = assert(client:new())
assert(wc:connect(uri))
return wc
end

local data = assert(wb:recv_frame())
assert.equal("testing Kong", cjson.decode(data).reqData)
describe("text", function()
local function send_text_and_get_echo(uri)
local payload = { message = "hello websocket" }
local wc = open_socket(uri)

assert(wb:send_close())
assert(wc:send_text(cjson.encode(payload)))
local frame, typ, err = wc:recv_frame()
assert.is_nil(wc.fatal)
assert(frame, err)
assert.equal("text", typ)
assert.same(payload, cjson.decode(frame))

return true
end
assert(wc:send_close())
end

it("works without Kong", function()
assert(make_request("ws://sockb.in"))
end)
it("sends and gets text without Kong", function()
send_text_and_get_echo("ws://127.0.0.1:55555/ws")
end)

it("sends and gets text with Kong", function()
send_text_and_get_echo("ws://" .. helpers.test_conf.proxy_ip ..
":" .. helpers.test_conf.proxy_port .. "/up-ws")
end)

it("works with Kong", function()
assert(make_request("ws://" .. helpers.test_conf.proxy_ip .. ":" .. helpers.test_conf.proxy_port .. "/ws"))
it("sends and gets text with kong under HTTPS", function()
send_text_and_get_echo("wss://" .. helpers.test_conf.proxy_ssl_ip ..
":" .. helpers.test_conf.proxy_ssl_port .. "/up-ws")
end)
end)

it("works with Kong under HTTPS", function()
assert(make_request("wss://" .. helpers.test_conf.proxy_ssl_ip .. ":" .. helpers.test_conf.proxy_ssl_port .. "/ws"))
describe("ping pong", function()
local function send_ping_and_get_pong(uri)
local payload = { message = "give me a pong" }
local wc = open_socket(uri)

assert(wc:send_ping(cjson.encode(payload)))
local frame, typ, err = wc:recv_frame()
assert.is_nil(wc.fatal)
assert(frame, err)
assert.equal("pong", typ)
assert.same(payload, cjson.decode(frame))

assert(wc:send_close())
end

it("plays ping-pong without Kong", function()
send_ping_and_get_pong("ws://127.0.0.1:55555/ws")
end)

it("plays ping-pong with Kong", function()
send_ping_and_get_pong("ws://" .. helpers.test_conf.proxy_ip ..
":" .. helpers.test_conf.proxy_port .. "/up-ws")
end)

it("plays ping-pong with kong under HTTPS", function()
send_ping_and_get_pong("wss://" .. helpers.test_conf.proxy_ssl_ip ..
":" .. helpers.test_conf.proxy_ssl_port .. "/up-ws")
end)
end)
end)
60 changes: 60 additions & 0 deletions spec/fixtures/custom_nginx.template
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,64 @@ http {
}
}
}

server {
listen 55555;

location /ws {
content_by_lua_block {
local server = require "resty.websocket.server"
local wb, err = server:new{
timeout = 5000,
max_payload_len = 65535,
}

if not wb then
ngx.log(ngx.ERR, "failed to open websocket: ", err)
return ngx.exit(444)
end

while true do
local data, typ, err = wb:recv_frame()
if wb.fatal then
ngx.log(ngx.ERR, "failed to receive frame: ", err)
return ngx.exit(444)
end

if data then
if typ == "close" then
break
end

if typ == "ping" then
local bytes, err = wb:send_pong(data)
if not bytes then
ngx.log(ngx.ERR, "failed to send pong: ", err)
return ngx.exit(444)
end

elseif typ == "pong" then
ngx.log(ngx.INFO, "client ponged")

elseif typ == "text" then
local bytes, err = wb:send_text(data)
if not bytes then
ngx.log(ngx.ERR, "failed to send text: ", err)
return ngx.exit(444)
end
end

else
local bytes, err = wb:send_ping()
if not bytes then
ngx.log(ngx.ERR, "failed to send ping: ", err)
return ngx.exit(444)
end
end
end

wb:send_close()
}
}
}
}

0 comments on commit 2435b0d

Please sign in to comment.