-
Notifications
You must be signed in to change notification settings - Fork 12k
fix(@angular/build): support Vite allowedHosts
option for development server
#29466
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
Changes from all commits
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
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
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
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 |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
*/ | ||
|
||
import { lastValueFrom, mergeMap, take, timeout } from 'rxjs'; | ||
import { URL } from 'url'; | ||
import { get, IncomingMessage, RequestOptions } from 'node:http'; | ||
import { text } from 'node:stream/consumers'; | ||
import { | ||
BuilderHarness, | ||
BuilderHarnessExecutionOptions, | ||
|
@@ -41,3 +42,49 @@ export async function executeOnceAndFetch<T>( | |
), | ||
); | ||
} | ||
|
||
/** | ||
* Executes the builder and then immediately performs a GET request | ||
* via the Node.js `http` builtin module. This is useful for cases | ||
* where the `fetch` API is limited such as testing different `Host` | ||
* header values with the development server. | ||
* The `fetch` based alternative is preferred otherwise. | ||
* | ||
* @param harness A builder harness instance. | ||
* @param url The URL string to get. | ||
* @param options An options object. | ||
* @returns | ||
*/ | ||
export async function executeOnceAndGet<T>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider: Would it be worth sharing the "executeOnce" part of these two functions and refactoring that into a shared utility so the major difference is the |
||
harness: BuilderHarness<T>, | ||
url: string, | ||
options?: Partial<BuilderHarnessExecutionOptions> & { request?: RequestOptions }, | ||
): Promise<BuilderHarnessExecutionResult & { response?: IncomingMessage; content?: string }> { | ||
return lastValueFrom( | ||
harness.execute().pipe( | ||
timeout(30000), | ||
mergeMap(async (executionResult) => { | ||
let response = undefined; | ||
let content = undefined; | ||
if (executionResult.result?.success) { | ||
let baseUrl = `${executionResult.result.baseUrl}`; | ||
baseUrl = baseUrl[baseUrl.length - 1] === '/' ? baseUrl : `${baseUrl}/`; | ||
const resolvedUrl = new URL(url, baseUrl); | ||
|
||
response = await new Promise<IncomingMessage>((resolve) => | ||
get(resolvedUrl, options?.request ?? {}, resolve), | ||
); | ||
|
||
if (response.statusCode === 200) { | ||
content = await text(response); | ||
} | ||
|
||
response.resume(); | ||
} | ||
|
||
return { ...executionResult, response, content }; | ||
}), | ||
take(1), | ||
), | ||
); | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/angular/build/src/builders/dev-server/tests/options/allowed-hosts_spec.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,80 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { executeDevServer } from '../../index'; | ||
import { executeOnceAndGet } from '../execute-fetch'; | ||
import { describeServeBuilder } from '../jasmine-helpers'; | ||
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; | ||
|
||
const FETCH_HEADERS = Object.freeze({ Host: 'example.com' }); | ||
|
||
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => { | ||
describe('option: "allowedHosts"', () => { | ||
beforeEach(async () => { | ||
setupTarget(harness); | ||
|
||
// Application code is not needed for these tests | ||
await harness.writeFile('src/main.ts', ''); | ||
}); | ||
|
||
it('does not allow an invalid host when option is not present', async () => { | ||
harness.useTarget('serve', { | ||
...BASE_OPTIONS, | ||
}); | ||
|
||
const { result, response } = await executeOnceAndGet(harness, '/', { | ||
request: { headers: FETCH_HEADERS }, | ||
}); | ||
|
||
expect(result?.success).toBeTrue(); | ||
expect(response?.statusCode).toBe(403); | ||
}); | ||
|
||
it('does not allow an invalid host when option is an empty array', async () => { | ||
harness.useTarget('serve', { | ||
...BASE_OPTIONS, | ||
allowedHosts: [], | ||
}); | ||
|
||
const { result, response } = await executeOnceAndGet(harness, '/', { | ||
request: { headers: FETCH_HEADERS }, | ||
}); | ||
|
||
expect(result?.success).toBeTrue(); | ||
expect(response?.statusCode).toBe(403); | ||
}); | ||
|
||
it('allows a host when specified in the option', async () => { | ||
harness.useTarget('serve', { | ||
...BASE_OPTIONS, | ||
allowedHosts: ['example.com'], | ||
}); | ||
|
||
const { result, content } = await executeOnceAndGet(harness, '/', { | ||
request: { headers: FETCH_HEADERS }, | ||
}); | ||
|
||
expect(result?.success).toBeTrue(); | ||
expect(content).toContain('<title>'); | ||
}); | ||
|
||
it('allows a host when option is true', async () => { | ||
harness.useTarget('serve', { | ||
...BASE_OPTIONS, | ||
allowedHosts: true, | ||
}); | ||
|
||
const { result, content } = await executeOnceAndGet(harness, '/', { | ||
request: { headers: FETCH_HEADERS }, | ||
}); | ||
|
||
expect(result?.success).toBeTrue(); | ||
expect(content).toContain('<title>'); | ||
}); | ||
}); | ||
}); |
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
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
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
Oops, something went wrong.
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.