pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/httputil/examples_test.go (about) 1 package httputil 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "fmt" 12 "net/http" 13 ) 14 15 // ////////////////////////////////////////////////////////////////////////////////// // 16 17 func ExampleGetRequestAddr() { 18 r, _ := http.NewRequest("GET", "https://http.cat/200", nil) 19 20 fmt.Println(GetRequestAddr(r)) 21 22 // Output: http.cat 443 23 } 24 25 func ExampleGetRequestHost() { 26 r, _ := http.NewRequest("GET", "https://http.cat/200", nil) 27 28 fmt.Println(GetRequestHost(r)) 29 30 // Output: http.cat 31 } 32 33 func ExampleGetRequestPort() { 34 r, _ := http.NewRequest("GET", "https://http.cat/200", nil) 35 36 fmt.Println(GetRequestPort(r)) 37 38 // Output: 443 39 } 40 41 func ExampleGetRemoteAddr() { 42 r, _ := http.NewRequest("GET", "https://http.cat/200", nil) 43 r.RemoteAddr = "127.0.0.1:19371" 44 45 fmt.Println(GetRemoteAddr(r)) 46 47 // Output: 127.0.0.1 19371 48 } 49 50 func ExampleGetRemoteHost() { 51 r, _ := http.NewRequest("GET", "https://http.cat/200", nil) 52 r.RemoteAddr = "127.0.0.1:19371" 53 54 fmt.Println(GetRemoteHost(r)) 55 56 // Output: 127.0.0.1 57 } 58 59 func ExampleGetRemotePort() { 60 r, _ := http.NewRequest("GET", "https://http.cat/200", nil) 61 r.RemoteAddr = "127.0.0.1:19371" 62 63 fmt.Println(GetRemotePort(r)) 64 65 // Output: 19371 66 } 67 68 func ExampleGetDescByCode() { 69 fmt.Println("200:", GetDescByCode(200)) 70 fmt.Println("404:", GetDescByCode(404)) 71 72 // Output: 73 // 200: OK 74 // 404: Not Found 75 } 76 77 func ExampleIsURL() { 78 url1 := "https://domain.com" 79 url2 := "httpj://domain.com" 80 81 fmt.Printf("%s: %t\n", url1, IsURL(url1)) 82 fmt.Printf("%s: %t\n", url2, IsURL(url2)) 83 84 // Output: 85 // https://domain.com: true 86 // httpj://domain.com: false 87 } 88 89 func ExampleGetPortByScheme() { 90 fmt.Println(GetPortByScheme("https")) 91 // Output: 443 92 }