Skip to content

Commit ff7cce5

Browse files
fix(http): status 400 to 404 are allowed
1 parent 6417ae3 commit ff7cce5

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

examples/javascript/src/lib/api-service.mjs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import { URL } from "url";
44
const headers = { 'Content-Type': 'application/json' }
55
const defaultHost = "https://api.iheartjane.com"
66

7+
function validStatus(status) {
8+
return (status >= 200 && status <= 299) ||
9+
(status >= 400 || status <= 404);
10+
}
11+
712
const authenticateClient = async () => {
813
const clientId = process.env.JANE_CLIENT_ID || ''
914
const clientSecret = process.env.JANE_CLIENT_SECRET || ''
1015
const apiUrl = process.env.JANE_API_URL || defaultHost
1116

1217
const resp = await request({
13-
// Prevent exceptions when requests have status code different from 2xx
14-
validateStatus: false,
18+
validateStatus: validStatus,
1519
method: 'post',
1620
url: `${apiUrl}/oauth/token`,
1721
data: {
@@ -29,14 +33,11 @@ const authenticateClient = async () => {
2933
const makeRequest = async (options) => {
3034
const apiUrl = process.env.JANE_API_URL || defaultHost
3135

32-
if (!apiUrl) {
33-
throw Error('No JANE_API_URL configured')
34-
}
35-
3636
const url = new URL(options.path, apiUrl);
3737

3838
try {
3939
const response = await request({
40+
validateStatus: validStatus,
4041
url: url.toString(),
4142
method: options.method || 'get',
4243
headers: {...headers, 'Authorization': `Bearer ${options.token}`},

examples/javascript/src/lib/api-service.spec.mjs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ describe('api service', () => {
2626

2727
await apiService.get('/api/path', token)
2828

29-
expect(requestMock).toHaveBeenCalledWith({
30-
url: 'https://test.localhost/api/path',
31-
method: 'get',
32-
headers: {
33-
'Content-Type': 'application/json',
34-
'Authorization': `Bearer ${token}`
35-
}
36-
})
29+
expect(requestMock).toHaveBeenCalledWith(
30+
expect.objectContaining({
31+
url: 'https://test.localhost/api/path',
32+
method: 'get',
33+
headers: {
34+
'Content-Type': 'application/json',
35+
'Authorization': `Bearer ${token}`
36+
}
37+
})
38+
)
3739
})
3840

3941
test('it returns statusCode, statusMessage, and body', async () => {
@@ -60,15 +62,17 @@ describe('api service', () => {
6062

6163
await apiService.post('/api/path', data, token)
6264

63-
expect(requestMock).toHaveBeenCalledWith({
64-
url: 'https://test.localhost/api/path',
65-
method: 'post',
66-
headers: {
67-
'Content-Type': 'application/json',
68-
'Authorization': `Bearer ${token}`
69-
},
70-
data: JSON.stringify(data),
71-
})
65+
expect(requestMock).toHaveBeenCalledWith(
66+
expect.objectContaining({
67+
url: 'https://test.localhost/api/path',
68+
method: 'post',
69+
headers: {
70+
'Content-Type': 'application/json',
71+
'Authorization': `Bearer ${token}`
72+
},
73+
data: JSON.stringify(data),
74+
})
75+
)
7276
})
7377

7478
test('it returns statusCode, statusMessage, and body', async () => {
@@ -77,15 +81,17 @@ describe('api service', () => {
7781

7882
const response = await apiService.post('/api/path', data)
7983

80-
expect(requestMock).toHaveBeenCalledWith({
81-
url: 'https://test.localhost/api/path',
82-
method: 'post',
83-
headers: {
84-
'Content-Type': 'application/json',
85-
'Authorization': `Bearer ${token}`
86-
},
87-
data: JSON.stringify(data),
88-
})
84+
expect(requestMock).toHaveBeenCalledWith(
85+
expect.objectContaining({
86+
url: 'https://test.localhost/api/path',
87+
method: 'post',
88+
headers: {
89+
'Content-Type': 'application/json',
90+
'Authorization': `Bearer ${token}`
91+
},
92+
data: JSON.stringify(data),
93+
})
94+
)
8995
})
9096
})
9197
})

0 commit comments

Comments
 (0)