Skip to content

fix(scan): updating entrypoint on 409 #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
299 changes: 298 additions & 1 deletion packages/scan/src/DefaultDiscoveries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('DefaultDiscoveries', () => {
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
Expand All @@ -67,12 +68,13 @@ describe('DefaultDiscoveries', () => {
expect(result).toEqual({ id: entryPointId });
});

it('should handle 409 conflict with location header redirect', async () => {
it('should handle 409 conflict by updating existing entry point with PUT', async () => {
const redirectLocation = `/api/v2/projects/${projectId}/entry-points/${entryPointId}`;
const conflictResponse = new Response(null, {
status: 409,
headers: { location: redirectLocation }
});
const putResponse = new Response(null, { status: 204 });
const finalResponse = new Response(JSON.stringify({ id: entryPointId }));

when(
Expand All @@ -86,6 +88,7 @@ describe('DefaultDiscoveries', () => {
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
Expand All @@ -97,6 +100,29 @@ describe('DefaultDiscoveries', () => {
)
).thenResolve(conflictResponse);

when(
mockedApiClient.request(
redirectLocation,
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'PUT',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(putResponse);

when(mockedApiClient.request(redirectLocation)).thenResolve(
finalResponse
);
Expand Down Expand Up @@ -127,6 +153,7 @@ describe('DefaultDiscoveries', () => {
},
body: JSON.stringify({
repeaterId,
authObjectId: getTarget.auth,
request: {
method: getTarget.method,
url: getTarget.url,
Expand All @@ -142,5 +169,275 @@ describe('DefaultDiscoveries', () => {

expect(result).toEqual({ id: entryPointId });
});

it('should get existing entry point even if PUT fails', async () => {
const redirectLocation =
'https://example.com/api/v2/projects/123/entry-points/456';
const conflictResponse = new Response(null, {
status: 409,
headers: new Headers({ location: redirectLocation })
});
const putFailResponse = new Response('Internal Server Error', {
status: 500,
statusText: 'Internal Server Error'
});

when(
mockedApiClient.request(
`/api/v2/projects/${projectId}/entry-points`,
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(conflictResponse);

when(
mockedApiClient.request(
redirectLocation,
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'PUT',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(putFailResponse);

const result = discoveries.createEntrypoint(testTarget, repeaterId);

await expect(result).rejects.toThrow(
`Failed to update existing entrypoint at ${redirectLocation}: Internal Server Error`
);
});

it('should throw error when POST fails with non-409 status', async () => {
const errorResponse = new Response('Bad Request', {
status: 400,
statusText: 'Bad Request'
});

when(
mockedApiClient.request(
`/api/v2/projects/${projectId}/entry-points`,
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(errorResponse);

const result = discoveries.createEntrypoint(testTarget, repeaterId);

await expect(result).rejects.toThrow(
'Failed to create entrypoint: Bad Request'
);
});

it('should throw error when receiving 409 conflict without location header', async () => {
const conflictResponse = new Response('Conflict', {
status: 409,
statusText: 'Conflict'
});

when(
mockedApiClient.request(
`/api/v2/projects/${projectId}/entry-points`,
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(conflictResponse);

const result = discoveries.createEntrypoint(testTarget, repeaterId);

await expect(result).rejects.toThrow(
'Failed to create entrypoint: Conflict'
);
});

it('should throw error when GET request fails after successful PUT', async () => {
const redirectLocation = `/api/v2/projects/${projectId}/entry-points/${entryPointId}`;
const conflictResponse = new Response(null, {
status: 409,
headers: { location: redirectLocation }
});
const putResponse = new Response(null, { status: 204 });
const getFailResponse = new Response('Not Found', {
status: 404,
statusText: 'Not Found'
});

when(
mockedApiClient.request(
`/api/v2/projects/${projectId}/entry-points`,
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(conflictResponse);

when(
mockedApiClient.request(
redirectLocation,
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'PUT',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(putResponse);

when(mockedApiClient.request(redirectLocation)).thenResolve(
getFailResponse
);

const result = discoveries.createEntrypoint(testTarget, repeaterId);

await expect(result).rejects.toThrow(
'Failed to create entrypoint: Not Found'
);
});

it('should throw error when 409 has location header but no location value', async () => {
const headers = new Headers();
headers.set('location', '');
const conflictResponse = new Response('Conflict', {
status: 409,
statusText: 'Conflict',
headers
});

when(
mockedApiClient.request(
`/api/v2/projects/${projectId}/entry-points`,
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(conflictResponse);

const putResponse = new Response(null, { status: 204 });
when(
mockedApiClient.request(
'',
deepEqual({
signal: anyOfClass(AbortSignal),
method: 'PUT',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
repeaterId,
authObjectId: testTarget.auth,
request: {
method: testTarget.method,
url: testTarget.url,
headers: testTarget.headers,
body: await testTarget.text()
}
})
})
)
).thenResolve(putResponse);

const finalResponse = new Response(JSON.stringify({ id: entryPointId }));
when(mockedApiClient.request('')).thenResolve(finalResponse);

const result = await discoveries.createEntrypoint(testTarget, repeaterId);

expect(result).toEqual({ id: entryPointId });
});
});
});
Loading