Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Jun 5, 2024
1 parent 11152af commit a34c845
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions spec/01-unit/05-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,14 @@ describe("Utils", function()

describe("https_check", function()
local old_ngx
local headers = {}

lazy_setup(function()
old_ngx = ngx
_G.ngx = {
var = {
scheme = nil
scheme = nil,
http_x_forwarded_proto = nil,
},
req = {
get_headers = function() return headers end
}
}
end)

Expand All @@ -100,7 +97,7 @@ describe("Utils", function()

describe("without X-Forwarded-Proto header", function()
lazy_setup(function()
headers["x-forwarded-proto"] = nil
ngx.var.http_x_forwarded_proto = nil
end)

it("should validate an HTTPS scheme", function()
Expand All @@ -124,11 +121,11 @@ describe("Utils", function()
describe("with X-Forwarded-Proto header", function()

lazy_teardown(function()
headers["x-forwarded-proto"] = nil
ngx.var.http_x_forwarded_proto = nil
end)

it("should validate any scheme with X-Forwarded_Proto as HTTPS", function()
headers["x-forwarded-proto"] = "hTTPs" -- check mixed casing for case insensitiveness
ngx.var.http_x_forwarded_proto = "hTTPs" -- check mixed casing for case insensitiveness
ngx.var.scheme = "hTTps"
assert.is.truthy(tools_http.check_https(true, true))
ngx.var.scheme = "hTTp"
Expand All @@ -138,7 +135,7 @@ describe("Utils", function()
end)

it("should validate only https scheme with X-Forwarded_Proto as non-HTTPS", function()
headers["x-forwarded-proto"] = "hTTP"
ngx.var.http_x_forwarded_proto = "hTTP"
ngx.var.scheme = "hTTps"
assert.is.truthy(tools_http.check_https(true, true))
ngx.var.scheme = "hTTp"
Expand All @@ -148,7 +145,7 @@ describe("Utils", function()
end)

it("should return an error with multiple X-Forwarded_Proto headers", function()
headers["x-forwarded-proto"] = { "hTTP", "https" }
ngx.var.http_x_forwarded_proto = "hTTP, https"
ngx.var.scheme = "hTTps"
assert.is.truthy(tools_http.check_https(true, true))
ngx.var.scheme = "hTTp"
Expand All @@ -157,27 +154,27 @@ describe("Utils", function()
end)

it("should not use X-Forwarded-Proto when the client is untrusted", function()
headers["x-forwarded-proto"] = "https"
ngx.var.http_x_forwarded_proto = "https"
ngx.var.scheme = "http"
assert.is_false(tools_http.check_https(false, false))
assert.is_false(tools_http.check_https(false, true))

headers["x-forwarded-proto"] = "https"
ngx.var.http_x_forwarded_proto = "https"
ngx.var.scheme = "https"
assert.is_true(tools_http.check_https(false, false))
assert.is_true(tools_http.check_https(false, true))
end)

it("should use X-Forwarded-Proto when the client is trusted", function()
headers["x-forwarded-proto"] = "https"
ngx.var.http_x_forwarded_proto = "https"
ngx.var.scheme = "http"

-- trusted client but do not allow terminated
assert.is_false(tools_http.check_https(true, false))

assert.is_true(tools_http.check_https(true, true))

headers["x-forwarded-proto"] = "https"
ngx.var.http_x_forwarded_proto = "https"
ngx.var.scheme = "https"
assert.is_true(tools_http.check_https(true, false))
assert.is_true(tools_http.check_https(true, true))
Expand Down

0 comments on commit a34c845

Please sign in to comment.