Skip to content

Commit a3d3fd5

Browse files
committed
tests: local mock http server for http regress tests
1 parent 825c6ea commit a3d3fd5

File tree

6 files changed

+327
-20
lines changed

6 files changed

+327
-20
lines changed

nix/checks.nix

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,60 @@
163163
which
164164
getkey-script
165165
supabase-groonga
166+
python3
167+
netcat
166168
];
167169
}
168170
''
169171
set -e
170172
173+
# Start HTTP mock server for http extension tests using portable locking
174+
HTTP_MOCK_PORT=8889
175+
PID_FILE="/tmp/http-mock-server-$HTTP_MOCK_PORT.pid"
176+
177+
# Function to start mock server with simple lock mechanism
178+
start_mock_server() {
179+
# Try to acquire lock by creating PID file atomically
180+
if (set -C; echo $$ > "$PID_FILE") 2>/dev/null; then
181+
# We got the lock, start the server
182+
echo "Starting HTTP mock server on port $HTTP_MOCK_PORT for tests..."
183+
${pkgs.python3}/bin/python3 ${./tests/http-mock-server.py} $HTTP_MOCK_PORT &
184+
HTTP_MOCK_PID=$!
185+
echo $HTTP_MOCK_PID > "$PID_FILE"
186+
187+
# Clean up on exit
188+
trap "kill $HTTP_MOCK_PID 2>/dev/null || true; rm -f '$PID_FILE'" EXIT
189+
190+
# Wait for server to be ready
191+
sleep 2
192+
193+
# Keep this process alive to maintain the lock
194+
wait $HTTP_MOCK_PID
195+
rm -f "$PID_FILE"
196+
else
197+
# Lock exists, check if process is still running
198+
if [ -f "$PID_FILE" ]; then
199+
existing_pid=$(cat "$PID_FILE" 2>/dev/null || echo "")
200+
if [ -n "$existing_pid" ] && kill -0 "$existing_pid" 2>/dev/null; then
201+
echo "HTTP mock server already running with PID $existing_pid, reusing it..."
202+
return 0
203+
else
204+
echo "Stale PID file found, removing and retrying..."
205+
rm -f "$PID_FILE"
206+
sleep 1
207+
start_mock_server # Retry once
208+
return $?
209+
fi
210+
fi
211+
fi
212+
}
213+
214+
# Start the server in background
215+
start_mock_server &
216+
217+
# Give server time to start
218+
sleep 3
219+
171220
#First we need to create a generic pg cluster for pgtap tests and run those
172221
export GRN_PLUGINS_DIR=${pkgs.supabase-groonga}/lib/groonga/plugins
173222
PGTAP_CLUSTER=$(mktemp -d)

nix/packages/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
dbmate-tool = pkgs.callPackage ./dbmate-tool.nix { inherit (self.supabase) defaults; };
3636
docs = pkgs.callPackage ./docs.nix { };
3737
supabase-groonga = pkgs.callPackage ./groonga { };
38+
http-mock-server = pkgs.callPackage ./http-mock-server.nix { };
3839
local-infra-bootstrap = pkgs.callPackage ./local-infra-bootstrap.nix { };
3940
migrate-tool = pkgs.callPackage ./migrate-tool.nix { psql_15 = self'.packages."psql_15/bin"; };
4041
overlayfs-on-package = pkgs.callPackage ./overlayfs-on-package.nix { };

nix/packages/http-mock-server.nix

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
pkgs,
3+
lib,
4+
stdenv,
5+
}:
6+
7+
stdenv.mkDerivation {
8+
pname = "http-mock-server";
9+
version = "1.0.0";
10+
11+
src = ../tests/http-mock-server.py;
12+
13+
nativeBuildInputs = with pkgs; [
14+
python3
15+
makeWrapper
16+
];
17+
18+
dontUnpack = true;
19+
20+
installPhase = ''
21+
mkdir -p $out/bin
22+
cp $src $out/bin/http-mock-server.py
23+
chmod +x $out/bin/http-mock-server.py
24+
25+
# Create a wrapper script
26+
makeWrapper ${pkgs.python3}/bin/python3 $out/bin/http-mock-server \
27+
--add-flags "$out/bin/http-mock-server.py"
28+
'';
29+
30+
meta = with lib; {
31+
description = "Simple HTTP mock server for testing";
32+
license = licenses.mit;
33+
platforms = platforms.all;
34+
};
35+
}

nix/tests/expected/http.out

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Test for http extension
22
-- Basic HTTP functionality tests
33
-- Test basic HTTP GET request
4-
SELECT status FROM http_get('http://httpbingo.org/get');
4+
SELECT status FROM http_get('http://localhost:8889/get');
55
status
66
--------
77
200
@@ -11,7 +11,7 @@ SELECT status FROM http_get('http://httpbingo.org/get');
1111
SELECT status, content_type
1212
FROM http((
1313
'GET',
14-
'http://httpbingo.org/headers',
14+
'http://localhost:8889/headers',
1515
ARRAY[http_header('User-Agent', 'pg_http_test')],
1616
NULL,
1717
NULL
@@ -23,7 +23,7 @@ FROM http((
2323

2424
-- Test HTTP POST request with JSON body
2525
SELECT status FROM http_post(
26-
'http://httpbingo.org/post',
26+
'http://localhost:8889/post',
2727
'{"test": "data"}',
2828
'application/json'
2929
);
@@ -34,7 +34,7 @@ SELECT status FROM http_post(
3434

3535
-- Test HTTP PUT request
3636
SELECT status FROM http_put(
37-
'http://httpbingo.org/put',
37+
'http://localhost:8889/put',
3838
'{"update": "data"}',
3939
'application/json'
4040
);
@@ -44,15 +44,15 @@ SELECT status FROM http_put(
4444
(1 row)
4545

4646
-- Test HTTP DELETE request
47-
SELECT status FROM http_delete('http://httpbingo.org/delete');
47+
SELECT status FROM http_delete('http://localhost:8889/delete');
4848
status
4949
--------
5050
200
5151
(1 row)
5252

5353
-- Test HTTP PATCH request
5454
SELECT status FROM http_patch(
55-
'http://httpbingo.org/patch',
55+
'http://localhost:8889/patch',
5656
'{"patch": "data"}',
5757
'application/json'
5858
);
@@ -62,15 +62,15 @@ SELECT status FROM http_patch(
6262
(1 row)
6363

6464
-- Test HTTP HEAD request
65-
SELECT status FROM http_head('http://httpbingo.org/get');
65+
SELECT status FROM http_head('http://localhost:8889/get');
6666
status
6767
--------
6868
200
6969
(1 row)
7070

7171
-- Test response headers parsing
7272
WITH response AS (
73-
SELECT * FROM http_get('http://httpbingo.org/response-headers?Content-Type=text/plain')
73+
SELECT * FROM http_get('http://localhost:8889/response-headers?Content-Type=text/plain')
7474
)
7575
SELECT
7676
status,
@@ -86,7 +86,7 @@ FROM response;
8686
-- This should complete successfully with reasonable timeout
8787
SELECT status FROM http((
8888
'GET',
89-
'http://httpbingo.org/delay/1',
89+
'http://localhost:8889/delay/1',
9090
ARRAY[]::http_header[],
9191
'application/json',
9292
2000 -- 2 second timeout
@@ -97,7 +97,7 @@ SELECT status FROM http((
9797
(1 row)
9898

9999
-- Test URL encoding
100-
SELECT status FROM http_get('http://httpbingo.org/anything?param=value%20with%20spaces&another=123');
100+
SELECT status FROM http_get('http://localhost:8889/anything?param=value%20with%20spaces&another=123');
101101
status
102102
--------
103103
200

0 commit comments

Comments
 (0)