Skip to content

Commit 2817b43

Browse files
samugibungle
authored andcommitted
tests(*): integration tests
1 parent de639d4 commit 2817b43

File tree

5 files changed

+180
-3
lines changed

5 files changed

+180
-3
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: integration_tests
2+
on: [push, pull_request]
3+
4+
jobs:
5+
test:
6+
strategy:
7+
fail-fast: false
8+
matrix:
9+
luaVersion: ["luajit-openresty"]
10+
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Setup environment
16+
run: docker build dev/ -t resty-session
17+
18+
- name: Run tests
19+
run: docker run -v $PWD:/test -w /test resty-session bash -c "luarocks make && make prove"

.github/workflows/unit_tests.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ jobs:
1616
run: docker build dev/ -t resty-session
1717

1818
- name: Run tests
19-
run: docker run -v $PWD:/test -w /test resty-session bash -c "luarocks make && busted --exclude-tags=noci"
19+
run: docker run -v $PWD:/test -w /test resty-session bash -c "luarocks make && make unit"

Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
lint:
44
@luacheck -q ./lib
55

6-
test:
6+
unit:
7+
busted --exclude-tags=noci
8+
9+
unit-all:
710
busted
811

12+
prove:
13+
prove
14+
915
docs:
1016
ldoc .

dev/Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
FROM openresty/openresty:1.21.4.1-focal
22

33
ENV DEBIAN_FRONTEND noninteractive
4+
ENV TEST_NGINX_BINARY openresty
45

56
USER root
6-
RUN apt-get update && apt-get install -y gcc git
7+
RUN apt-get update && apt-get install -y gcc git cpanminus
78

89
RUN git clone https://github.com/Olivine-Labs/busted
910
RUN cd busted && luarocks make
@@ -15,3 +16,5 @@ RUN luarocks install lua-resty-redis-cluster
1516
RUN luarocks install inspect
1617
RUN luarocks install lua_pack
1718
RUN luarocks install LuaCov
19+
20+
RUN cpanm --notest Test::Nginx

t/01-cookies.t

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
use Test::Nginx::Socket;
2+
repeat_each(2);
3+
4+
$ENV{TEST_NGINX_NXSOCK} ||= html_dir();
5+
plan tests => repeat_each() * blocks() * 3 + 4;
6+
7+
run_tests();
8+
9+
__DATA__
10+
11+
=== TEST 1: session cookie is returned
12+
--- http_config
13+
init_by_lua_block {
14+
require("resty.session").init({
15+
storage = "cookie",
16+
})
17+
}
18+
--- config
19+
location = /test {
20+
content_by_lua_block {
21+
local session = require "resty.session".new()
22+
local ok = session:save()
23+
if ok then
24+
ngx.say("yay")
25+
end
26+
}
27+
}
28+
29+
--- request
30+
GET /test
31+
--- response_body
32+
yay
33+
--- response_headers_like
34+
Set-Cookie: .*session=.+;.*
35+
--- error_code: 200
36+
--- no_error_log
37+
[error]
38+
39+
40+
=== TEST 2: remember cookie is returned when remember=true
41+
--- http_config
42+
init_by_lua_block {
43+
require("resty.session").init({
44+
remember = true,
45+
storage = "cookie",
46+
})
47+
}
48+
--- config
49+
location = /test {
50+
content_by_lua_block {
51+
local session = require "resty.session".new()
52+
local ok = session:save()
53+
54+
if ok then
55+
ngx.say("yay")
56+
end
57+
}
58+
}
59+
60+
--- request
61+
GET /test
62+
--- response_body
63+
yay
64+
--- response_headers_like
65+
Set-Cookie: .*remember=.+;.*
66+
--- error_code: 200
67+
--- no_error_log
68+
[error]
69+
70+
71+
=== TEST 3: session.open() opens a session from a valid cookie and data is
72+
extracted correctly
73+
--- http_config
74+
init_by_lua_block {
75+
require("resty.session").init({
76+
secret = "RaJKp8UQW1",
77+
storage = "cookie",
78+
audience = "my_application",
79+
idling_timeout = 0,
80+
rolling_timeout = 0,
81+
absolute_timeout = 0,
82+
})
83+
}
84+
--- config
85+
location = /test {
86+
content_by_lua_block {
87+
local session = require "resty.session".open()
88+
local sub = session:get_subject()
89+
local aud = session:get_audience()
90+
local quote = session:get("quote")
91+
92+
ngx.say(sub .. "|" .. aud .. "|" .. quote)
93+
}
94+
}
95+
96+
--- request
97+
GET /test
98+
--- more_headers
99+
Cookie: session=AQAAS3ZGU0k8tUKsWSci9Fb6PM5xbm469FlR5g_B5HWZ6KYGSOZjAAAAAABcAABTCuHjqpE7B6Ux7m4GCylZAAAAzcWnTvzG51whooR_4QQwDgGdMOOa5W7tG4JWiDFU3zuYLFzakWEi-y-ogrwTpnt24zQXP_uJK7r5lMPNzRSMJM9H1a_MIegzEMm-QSgVRaoZVJq3Oo; Path=/; SameSite=Lax; HttpOnly
100+
--- response_body
101+
Lua Fan|my_application|Lorem ipsum dolor sit amet
102+
--- error_code: 200
103+
--- no_error_log
104+
[error]
105+
106+
107+
=== TEST 4: clear_request_cookie() clears session Cookie from request to
108+
upstream
109+
--- http_config
110+
init_by_lua_block {
111+
require("resty.session").init({
112+
secret = "RaJKp8UQW1",
113+
storage = "cookie",
114+
audience = "my_application",
115+
idling_timeout = 0,
116+
rolling_timeout = 0,
117+
absolute_timeout = 0,
118+
})
119+
}
120+
121+
server {
122+
listen unix:/$TEST_NGINX_NXSOCK/nginx.sock;
123+
124+
location /t {
125+
content_by_lua_block {
126+
local headers = ngx.req.get_headers()
127+
ngx.say("session_cookie: [", tostring(headers["Cookie"]), "]")
128+
}
129+
}
130+
}
131+
132+
--- config
133+
location = /test {
134+
access_by_lua_block {
135+
local session = require "resty.session".open()
136+
session:clear_request_cookie()
137+
}
138+
proxy_pass http://unix:/$TEST_NGINX_NXSOCK/nginx.sock;
139+
}
140+
141+
--- request
142+
GET /test
143+
--- more_headers
144+
Cookie: session=AQAAS3ZGU0k8tUKsWSci9Fb6PM5xbm469FlR5g_B5HWZ6KYGSOZjAAAAAABcAABTCuHjqpE7B6Ux7m4GCylZAAAAzcWnTvzG51whooR_4QQwDgGdMOOa5W7tG4JWiDFU3zuYLFzakWEi-y-ogrwTpnt24zQXP_uJK7r5lMPNzRSMJM9H1a_MIegzEMm-QSgVRaoZVJq3Oo; Path=/; SameSite=Lax; HttpOnly
145+
--- response_body
146+
session_cookie: [Path=/; SameSite=Lax; HttpOnly]
147+
--- error_code: 200
148+
--- no_error_log
149+
[error]

0 commit comments

Comments
 (0)