Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powerwash #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
MAKEFLAGS += --silent
.PHONY=all
.all=dev
.PHONY=dev-image test-image test

default:
echo "No default"

dev:
luarocks install --local http
luarocks install --local lunajson
luarocks install --local busted
dev-image:
echo "todo"

test:
busted spec/
test-image:
docker build -t kong-plugin-path-prefix-test:latest spec/

test: test-image
docker run \
-it \
--rm \
-e KONG_API_URL="http://host.docker.internal:8001" \
-e KONG_PROXY_URL="http://host.docker.internal:8000" \
-v $$(pwd):/usr/src \
kong-plugin-path-prefix-test:latest /usr/src/spec
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
image: postgres:9.5
environment:
- POSTGRES_USER=kong
- POSTGRES_PASSWORD=kongdb
- POSTGRES_DB=kong
kong:
image: kong
Expand All @@ -25,4 +26,4 @@ services:
- KONG_PROXY_ACCESS_LOG=/dev/stdout
- KONG_ADMIN_ACCESS_LOG=/dev/stdout
- KONG_PROXY_ERROR_LOG=/dev/stdout
- KONG_ADMIN_ERROR_LOG=/dev/stdout
- KONG_ADMIN_ERROR_LOG=/dev/stdout
11 changes: 11 additions & 0 deletions spec/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM openresty/openresty:alpine-fat

WORKDIR /usr/src

RUN apk update --no-cache && \
apk add openssl openssl-dev bsd-compat-headers m4
RUN luarocks install --local http 0.2 && \
luarocks install --local lunajson && \
luarocks install --local busted

ENTRYPOINT ["/root/.luarocks/bin/busted"]
6 changes: 5 additions & 1 deletion spec/http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ function _Module.get(url)
request.headers:upsert("Accept", "application/json")
request.headers:upsert(":method", "GET")

local headers, stream = request:go()
local headers, stream, err = request:go()
print('uri')
print(url)
print('err')
print(err)

local body = stream:get_body_as_string()
if headers:get(":status") ~= "200" then
Expand Down
3 changes: 1 addition & 2 deletions spec/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ local function post(path, payload)
request.headers:upsert("Content-Type", "application/json")
request.headers:upsert(":method", "POST")
request:set_body(lunajson.encode(payload))
local headers, stream = request:go()

local headers, stream, err = request:go()
local body = stream:get_body_as_string()
if headers:get(":status") ~= "201" then
return "Kong error: " .. body
Expand Down
16 changes: 9 additions & 7 deletions spec/path-prefix/01-access_service_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local chance = require('spec.chance')
local http = require('spec.http')

describe('access', function()
local kong_proxy_url = os.getenv("KONG_PROXY_URL") or "http://localhost:8000"
local kong_host = string.match(kong_proxy_url, 'http://([%w+.]+)')
local upstream_url = "https://mockbin.org/request"

local path
Expand Down Expand Up @@ -44,29 +46,29 @@ describe('access', function()

describe('attached to a service', function()
it("should rewrite the path to the upstream from /service-with-plugin-route/foobar to /foobar", function()
local body = http.get("http://localhost:8000/service-with-plugin-route/foobar")
local body = http.get(kong_proxy_url .. "/service-with-plugin-route/foobar")

-- localhost is the the value of the x-forwarded-host header, so Mockbin sets it as the host
-- kong_host is the the value of the x-forwarded-host header, so Mockbin sets it as the host
-- /request is the upstream's path
local expected_url = 'http://localhost/request/foobar'
local expected_url = 'http://' .. kong_host .. '/request/foobar'

assert.equal(body.url, expected_url)
end)

it("should rewrite /service-with-plugin-route to the empty string", function()
-- rewriting the entire path is more easily accomplished by setting strip_prefix to true when creating the route
-- but it's worth testing this flow anyway
local body = http.get("http://localhost:8000/service-with-plugin-route")
local body = http.get(kong_proxy_url .. "/service-with-plugin-route")

local expected_url = 'http://localhost/request'
local expected_url = 'http://' .. kong_host .. '/request'

assert.equal(body.url, expected_url)
end)

it("should only make one path replacement", function()
local body = http.get("http://localhost:8000/service-with-plugin-route/service-with-plugin-route")
local body = http.get(kong_proxy_url .. "/service-with-plugin-route/service-with-plugin-route")

local expected_url = 'http://localhost/request/service-with-plugin-route'
local expected_url = 'http://' .. kong_host .. '/request/service-with-plugin-route'

assert.equal(body.url, expected_url)
end)
Expand Down