File: example_test.go

package info (click to toggle)
golang-golang-x-net 1%3A0.0%2Bgit20211209.491a49a%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 7,020 kB
  • sloc: asm: 18; makefile: 16
file content (67 lines) | stat: -rw-r--r-- 1,885 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package idna_test

import (
	"fmt"

	"golang.org/x/net/idna"
)

func ExampleProfile() {
	// Raw Punycode has no restrictions and does no mappings.
	fmt.Println(idna.ToASCII(""))
	fmt.Println(idna.ToASCII("*.GÖPHER.com"))
	fmt.Println(idna.Punycode.ToASCII("*.GÖPHER.com"))

	// Rewrite IDN for lookup.
	fmt.Println(idna.Lookup.ToASCII(""))
	fmt.Println(idna.Lookup.ToASCII("www.GÖPHER.com"))

	// Convert an IDN to ASCII for registration purposes.
	// This reports an error if the input was illformed.
	fmt.Println(idna.Registration.ToASCII("www.GÖPHER.com"))
	fmt.Println(idna.Registration.ToASCII("www.göpher.com"))

	// Output:
	//  <nil>
	// *.xn--GPHER-1oa.com <nil>
	// *.xn--GPHER-1oa.com <nil>
	//  <nil>
	// www.xn--gpher-jua.com <nil>
	// www.xn--GPHER-1oa.com idna: disallowed rune U+0047
	// www.xn--gpher-jua.com <nil>
}

func ExampleNew() {
	var p *idna.Profile

	// Raw Punycode has no restrictions and does no mappings.
	p = idna.New()
	fmt.Println(p.ToASCII("*.faß.com"))

	// Do mappings. Note that star is not allowed in a DNS lookup.
	p = idna.New(
		idna.MapForLookup(),
		idna.Transitional(true)) // Map ß -> ss
	fmt.Println(p.ToASCII("*.faß.com"))

	// Lookup for registration. Also does not allow '*'.
	p = idna.New(idna.ValidateForRegistration())
	fmt.Println(p.ToUnicode("*.faß.com"))

	// Set up a profile maps for lookup, but allows wild cards.
	p = idna.New(
		idna.MapForLookup(),
		idna.Transitional(true),      // Map ß -> ss
		idna.StrictDomainName(false)) // Set more permissive ASCII rules.
	fmt.Println(p.ToASCII("*.faß.com"))

	// Output:
	// *.xn--fa-hia.com <nil>
	// *.fass.com idna: disallowed rune U+002A
	// *.faß.com idna: disallowed rune U+002A
	// *.fass.com <nil>
}