Skip to content

Commit d37946e

Browse files
committed
feat: update server configuration and testing utilities for improved API testing
1 parent 9cca06a commit d37946e

File tree

5 files changed

+13
-42
lines changed

5 files changed

+13
-42
lines changed

src/__tests__/routes.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ jest.unstable_mockModule('../utils/db.js', () => {
8080
});
8181

8282
// Import app after mocking
83-
const { app } = await import('../index.js');
83+
await import('../index.js');
84+
import { getTestServer } from '@google-cloud/functions-framework/testing';
85+
const app = getTestServer('app');
8486

8587
describe('API Routes', () => {
8688
describe('Health Check', () => {

src/index.js

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import http from 'http';
2-
import url from 'url';
31
import crypto from 'crypto';
42
import functions from '@google-cloud/functions-framework';
53

@@ -85,20 +83,10 @@ const sendJSONResponse = (res, data, statusCode = 200) => {
8583

8684
// Helper function to check if resource is modified
8785
const isModified = (req, etag) => {
88-
const ifNoneMatch = req.headers['if-none-match'];
86+
const ifNoneMatch = req.headers['if-none-match'] || req.get('if-none-match');
8987
return !ifNoneMatch || ifNoneMatch !== `"${etag}"`;
9088
};
9189

92-
// Helper function to parse query parameters
93-
const parseQuery = (queryString) => {
94-
const params = new URLSearchParams(queryString);
95-
const result = {};
96-
for (const [key, value] of params) {
97-
result[key] = value;
98-
}
99-
return result;
100-
};
101-
10290
// Route handler function
10391
const handleRequest = async (req, res) => {
10492
try {
@@ -119,13 +107,8 @@ const handleRequest = async (req, res) => {
119107
return;
120108
}
121109

122-
// Parse URL
123-
const parsedUrl = url.parse(req.url, true);
124-
const pathname = parsedUrl.pathname;
125-
const query = parsedUrl.query;
126-
127-
// Add query to req object for compatibility with existing controllers
128-
req.query = query;
110+
// Parse URL path
111+
const pathname = req.path;
129112

130113
// Route handling
131114
if (pathname === '/' && req.method === 'GET') {
@@ -186,22 +169,8 @@ const handleRequest = async (req, res) => {
186169
}
187170
};
188171

189-
// Create HTTP server
190-
const server = http.createServer(handleRequest);
191-
192-
// Export the server for testing
193-
export { server as app };
194-
195-
// Register with Functions Framework for Cloud Functions
172+
// Register with Functions Framework
196173
functions.http('app', handleRequest);
197174

198-
// For standalone server mode (local development)
199-
// Note: In ES modules, there's no require.main === module equivalent
200-
// We'll use import.meta.url to check if this is the main module
201-
const isMain = import.meta.url === `file://${process.argv[1]}`;
202-
if (isMain) {
203-
const PORT = process.env.PORT || 3000;
204-
server.listen(PORT, () => {
205-
console.log(`Server running on port ${PORT}`);
206-
});
207-
}
175+
// Export for testing using Functions Framework testing utilities
176+
export { handleRequest as app };

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"node": ">=22.0.0"
99
},
1010
"scripts": {
11-
"start": "export DATABASE=tech-report-api-prod && node index.js",
11+
"start": "DATABASE=tech-report-api-prod functions-framework --target=app",
1212
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
1313
"test:live": "bash ../test-api.sh"
1414
},

terraform/modules/run-service/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ variable "available_cpu" {
3333
}
3434
variable "ingress_settings" {
3535
type = string
36-
default = "ALLOW_INTERNAL_AND_GCLB"
36+
default = "ALLOW_ALL"
3737
description = "String value that controls what traffic can reach the function. Allowed values are ALLOW_ALL, ALLOW_INTERNAL_AND_GCLB and ALLOW_INTERNAL_ONLY. Check ingress documentation to see the impact of each settings value. Changes to this field will recreate the cloud function."
3838
}
3939
variable "vpc_connector_egress_settings" {

test-api.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
test_endpoint() {
55
local endpoint=$1
66
local params=$2
7-
local url="http://localhost:3000${endpoint}${params}"
7+
local url="http://localhost:8080${endpoint}${params}"
88

99
echo "Testing endpoint: ${url}"
1010
response=$(curl -s -w "\n%{http_code}" "${url}")
@@ -27,7 +27,7 @@ test_endpoint() {
2727
# Function to test CORS preflight with OPTIONS request
2828
test_cors_preflight() {
2929
local endpoint=$1
30-
local url="http://localhost:3000${endpoint}"
30+
local url="http://localhost:8080${endpoint}"
3131

3232
echo "Testing CORS preflight for: ${url}"
3333

0 commit comments

Comments
 (0)