Skip to content

Commit

Permalink
Merge pull request #1827 from Mashape/feat/websockets
Browse files Browse the repository at this point in the history
feat(proxy) supports websockets
  • Loading branch information
thibaultcha authored Dec 3, 2016
2 parents 711a39b + 1bd0d20 commit a3c6e16
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ upstream kong_upstream {
keepalive ${{UPSTREAM_KEEPALIVE}};
}
map $http_upgrade $upstream_connection {
default keep-alive;
websocket upgrade;
}
map $http_upgrade $upstream_upgrade {
default '';
websocket websocket;
}
server {
server_name kong;
listen ${{PROXY_LISTEN}};
Expand All @@ -89,10 +99,13 @@ server {
kong.access()
}
proxy_http_version 1.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $upstream_host;
proxy_set_header Upgrade $upstream_upgrade;
proxy_set_header Connection $upstream_connection;
proxy_pass_header Server;
proxy_pass $upstream_url;
Expand Down
44 changes: 44 additions & 0 deletions spec/02-integration/05-proxy/04-websockets_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local client = require "resty.websocket.client"
local helpers = require "spec.helpers"
local cjson = require "cjson"

describe("Websockets", function()
setup(function()
assert(helpers.start_kong())

assert(helpers.dao.apis:insert {
request_path = "/ws",
strip_request_path = true,
upstream_url = "http://sockb.in"
})
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 data = assert(wb:recv_frame())
assert.equal("testing Kong", cjson.decode(data).reqData)

assert(wb:send_close())

return true
end

it("works without Kong", function()
assert(make_request("ws://sockb.in"))
end)

it("works with Kong", function()
assert(make_request("ws://"..helpers.test_conf.proxy_ip..":"..helpers.test_conf.proxy_port.."/ws"))
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"))
end)
end)

0 comments on commit a3c6e16

Please sign in to comment.