-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(start-server-core): return 404 for API routes without GET handler #5758
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
Open
s3847243
wants to merge
2
commits into
TanStack:main
Choose a base branch
from
s3847243:fix/api-404-no-get
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/start-server-core/tests/createStartHandler.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { describe, it, expect, beforeEach } from 'vitest' | ||
| import { createStartHandler } from '../src' | ||
| import { currentHandlers } from './mocks/router-entry' | ||
|
|
||
|
|
||
| const spaFallback = async () => | ||
| new Response('<!doctype html><div>spa</div>', { | ||
| status: 200, | ||
| headers: { 'Content-Type': 'text/html' }, | ||
| }) | ||
|
|
||
| function makeApp() { | ||
| return createStartHandler(async () => await spaFallback()) | ||
| } | ||
| beforeEach(() => { | ||
| Object.keys(currentHandlers).forEach(key => delete currentHandlers[key]) | ||
| }) | ||
|
|
||
| describe('createStartHandler — server route HTTP method handling', function () { | ||
| it('should return 404 JSON for GET when only POST is defined (no SPA fallback)', async function () { | ||
| currentHandlers.POST = () => new Response('ok', { status: 200 }) | ||
| const app = makeApp() | ||
|
|
||
| const res = await app( | ||
| new Request('http://localhost/api/test-no-get', { method: 'GET' }), | ||
| ) | ||
|
|
||
| expect(res.status).toBe(404) | ||
| expect(res.headers.get('content-type')).toMatch(/application\/json/i) | ||
| const txt = await res.text() | ||
| expect(txt).toContain('Not Found') | ||
| expect(txt.toLowerCase().startsWith('<!doctype html>')).toBe(false) | ||
| }) | ||
|
|
||
| it('should return 200 for POST and execute the route handler', async function () { | ||
| currentHandlers.POST = () => new Response('ok', { status: 200 }) | ||
| const app = makeApp() | ||
|
|
||
| const res = await app( | ||
| new Request('http://localhost/api/test-no-get', { method: 'POST' }), | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| expect(await res.text()).toBe('ok') | ||
| }) | ||
|
|
||
| it('should return 404 for HEAD when GET is not defined', async function () { | ||
| currentHandlers.POST = () => new Response('ok', { status: 200 }) | ||
| const app = makeApp() | ||
|
|
||
| const res = await app( | ||
| new Request('http://localhost/api/test-no-get', { method: 'HEAD' }), | ||
| ) | ||
|
|
||
| expect(res.status).toBe(404) | ||
| }) | ||
|
|
||
| it('should use GET handler when HEAD is requested and GET exists', async function () { | ||
| currentHandlers.GET = () => new Response('hello', { status: 200 }) | ||
| const app = makeApp() | ||
|
|
||
| const res = await app( | ||
| new Request('http://localhost/api/has-get', { method: 'HEAD' }), | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| }) | ||
|
|
||
| it('should execute ANY handler for unsupported methods (e.g., PUT)', async function () { | ||
| currentHandlers.ANY = () => new Response('ok-any', { status: 200 }) | ||
| const app = makeApp() | ||
|
|
||
| const res = await app( | ||
| new Request('http://localhost/api/any', { method: 'PUT' }), | ||
| ) | ||
|
|
||
| expect(res.status).toBe(200) | ||
| expect(await res.text()).toBe('ok-any') | ||
| }) | ||
| }) | ||
|
|
||
1 change: 1 addition & 0 deletions
1
packages/start-server-core/tests/mocks/injected-head-scripts.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const injectedHeadScripts = '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { AnyRouter } from '@tanstack/router-core' | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| export let currentHandlers: Record<string, any> = {} | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| function makeFakeRouter(): AnyRouter { | ||
| return { | ||
| rewrite: undefined as any, | ||
| getMatchedRoutes: (_pathname: string) => ({ | ||
| matchedRoutes: [{ options: { server: { middleware: [] } } }], | ||
| foundRoute: { | ||
| options: { | ||
| server: { handlers: currentHandlers }, | ||
| component: undefined, | ||
| }, | ||
| }, | ||
| routeParams: {}, | ||
| }), | ||
|
|
||
| update: () => {}, | ||
| load: async () => {}, | ||
| state: { redirect: null } as any, | ||
| serverSsr: { dehydrate: async () => {} } as any, | ||
| options: {} as any, | ||
| resolveRedirect: (r: any) => r, | ||
| } as unknown as AnyRouter | ||
| } | ||
|
|
||
| export async function getRouter() { | ||
| return makeFakeRouter() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export const startInstance = { | ||
| getOptions: async () => ({ | ||
| requestMiddleware: undefined, | ||
| defaultSsr: undefined, | ||
| serializationAdapters: [], | ||
| }), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| export const tsrStartManifest = () => ({ | ||
| routes: { | ||
| __root__: { | ||
| id: '__root__', | ||
| }, | ||
| }, | ||
| routeTree: { | ||
| id: '__root__', | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.