deno/tests/unit_node/url_test.ts
denobot 49fa02d988
Some checks are pending
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
fix(ext/node): url.domainToASCII returns empty string for invalid domains (#31219)
## Summary

This PR fixes the behavior of `url.domainToASCII` to match Node.js by
returning an empty string when an invalid domain is passed, instead of
throwing an error.

## Changes

- Modified `op_node_idna_domain_to_ascii` in `ext/node/ops/idna.rs` to
return an empty string on error instead of propagating the error
- Updated documentation in `ext/node/polyfills/internal/idna.ts` to
clarify this behavior
- Added spec test in `tests/specs/node/url_domain_to_ascii/` to verify
the fix

## Test Plan

### Manual Testing
Tested with the example from the issue:
```javascript
import url from 'node:url';
console.log(url.domainToASCII('xn--iñvalid.com')); // Returns empty string instead of throwing
```

Also tested with various valid and invalid domains to ensure
compatibility:
- Valid domains: `example.com` → `example.com`
- Valid unicode: `münchen.de` → `xn--mnchen-3ya.de`
- Invalid punycode: `xn--iñvalid.com` → `` (empty string)
- Invalid domain: `xn--` → `` (empty string)

### Automated Tests
Added spec test that verifies the behavior matches Node.js for both
valid and invalid domains.

## Closes

Fixes #31133

---

_This PR was autogenerated and may require review and adjustments._

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Daniel Rahmanto <daniel.rahmanto@gmail.com>
2025-12-03 21:57:53 +07:00

14 lines
497 B
TypeScript

// Copyright 2018-2025 the Deno authors. MIT license.
import { domainToASCII } from "node:url";
import { assertEquals } from "@std/assert/equals";
Deno.test({
name: "[node/url] domainToASCII",
fn() {
assertEquals(domainToASCII("example.com"), "example.com");
assertEquals(domainToASCII("[::1]"), "[::1]");
assertEquals(domainToASCII("münchen.de"), "xn--mnchen-3ya.de");
// Invalid domain returns empty string
assertEquals(domainToASCII("xn--iñvalid.com"), "");
},
});