Skip to content

Commit 21cf2c3

Browse files
refactor(tests): replace numeric HTTP status codes with descriptive constants
Improves readability and maintainability by using `StatusCodes` from `http-status-codes` package instead of hardcoded numeric values (e.g., 200, 401) in integration tests.
1 parent a3cfac3 commit 21cf2c3

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"eslint-plugin-n": "^14.0.0",
2626
"eslint-plugin-prettier": "^5.0.0-alpha.1",
2727
"gts": "5.3.0",
28+
"http-status-codes": "^2.3.0",
2829
"mocha": "^10.2.0",
2930
"nunjucks": "^3.2.4",
3031
"prettier": "^3.0.3",

run/service-auth/test/receive.test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const {expect} = require('chai');
1818
const axios = require('axios');
1919
const {execSync} = require('child_process');
2020
const {v4: uuidv4} = require('uuid');
21+
const {StatusCodes} = require('http-status-codes');
2122

22-
const INVALID_TOKEN = 'invalid-token';
23+
const TEST_INVALID_TOKEN = 'test-invalid-token';
2324
const PROJECT_ID = process.env.GOOGLE_CLOUD_PROJECT;
2425
const REGION = 'us-central1';
2526

@@ -71,25 +72,25 @@ describe('receiveRequestAndParseAuthHeader sample (Cloud Run integration)', func
7172
},
7273
});
7374

74-
expect(res.status).to.equal(200);
75+
expect(res.status).to.equal(StatusCodes.OK);
7576
expect(res.data).to.match(/^Hello, .@.\.\w+!\n$/);
7677
});
7778

7879
it('should respond with anonymous if no token is provided', async () => {
7980
const res = await axios.get(serviceUrl);
80-
expect(res.status).to.equal(200);
81+
expect(res.status).to.equal(StatusCodes.OK);
8182
expect(res.data).to.equal('Hello, anonymous user.\n');
8283
});
8384

84-
it('should respond with 401 if token is invalid', async () => {
85+
it('should respond with unoauthorized if token is invalid', async () => {
8586
try {
8687
await axios.get(serviceUrl, {
8788
headers: {
88-
Authorization: `Bearer ${INVALID_TOKEN}`,
89+
Authorization: `Bearer ${TEST_INVALID_TOKEN}`,
8990
},
9091
});
9192
} catch (err) {
92-
expect(err.response.status).to.equal(401);
93+
expect(err.response.status).to.equal(StatusCodes.UNAUTHORIZED);
9394
expect(err.response.data).to.include('Invalid token');
9495
}
9596
});

0 commit comments

Comments
 (0)