Best K6 code snippet using html.Href
escape_test.go
Source:escape_test.go
...1072 `<a HREF='http:`,1073 context{state: stateURL, delim: delimSingleQuote, urlPart: urlPartPreQuery, attr: attrURL},1074 },1075 {1076 `<a Href='/`,1077 context{state: stateURL, delim: delimSingleQuote, urlPart: urlPartPreQuery, attr: attrURL},1078 },1079 {1080 `<a href='"`,1081 context{state: stateURL, delim: delimSingleQuote, urlPart: urlPartPreQuery, attr: attrURL},1082 },1083 {1084 `<a href="'`,1085 context{state: stateURL, delim: delimDoubleQuote, urlPart: urlPartPreQuery, attr: attrURL},1086 },1087 {1088 `<a href=''`,1089 context{state: stateURL, delim: delimSingleQuote, urlPart: urlPartPreQuery, attr: attrURL},1090 },...
icon.go
Source:icon.go
1package bitwarden2import (3 "encoding/json"4 "errors"5 "io"6 "io/ioutil"7 "net"8 "net/http"9 "sort"10 "strconv"11 "strings"12 "time"13 "github.com/cozy/cozy-stack/pkg/config/config"14 "golang.org/x/net/html"15)16const (17 cacheTTL = 7 * 24 * time.Hour // 1 week18 maxSize = 100000 // 100kb19)20var iconClient = &http.Client{21 Timeout: 10 * time.Second,22}23// Icon is a simple struct with a content-type and the content of an icon.24type Icon struct {25 Mime string `json:"mime"`26 Body []byte `json:"body"`27}28// GetIcon returns an icon for the given domain.29func GetIcon(domain string) (*Icon, error) {30 if err := validateDomain(domain); err != nil {31 return nil, err32 }33 cache := config.GetConfig().CacheStorage34 key := "bw-icons:" + domain35 if data, ok := cache.Get(key); ok {36 if len(data) == 0 {37 return nil, errors.New("No icon")38 }39 icon := &Icon{}40 if err := json.Unmarshal(data, icon); err != nil {41 return nil, err42 }43 return icon, nil44 }45 icon, err := fetchIcon(domain)46 if err != nil {47 cache.Set(key, nil, cacheTTL)48 } else {49 if data, err := json.Marshal(icon); err == nil {50 cache.Set(key, data, cacheTTL)51 }52 }53 return icon, err54}55func validateDomain(domain string) error {56 if domain == "" || len(domain) > 255 || strings.Contains(domain, "..") {57 return errors.New("Unauthorized domain")58 }59 for _, c := range domain {60 if c == ' ' || !strconv.IsPrint(c) {61 return errors.New("Invalid domain")62 }63 }64 if _, _, err := net.ParseCIDR(domain + "/24"); err == nil {65 return errors.New("IP address are not authorized")66 }67 return nil68}69func fetchIcon(domain string) (*Icon, error) {70 if html, err := getPage(domain); err == nil {71 candidates := getCandidateIcons(domain, html)72 html.Close()73 for _, candidate := range candidates {74 if icon, err := downloadIcon(candidate); err == nil {75 return icon, nil76 }77 }78 }79 return downloadFavicon(domain)80}81func getPage(domain string) (io.ReadCloser, error) {82 req, err := http.NewRequest(http.MethodGet, "https://"+domain, nil)83 if err != nil {84 return nil, err85 }86 res, err := iconClient.Do(req)87 if err != nil {88 return nil, err89 }90 if res.StatusCode != http.StatusOK {91 res.Body.Close()92 return nil, errors.New("Not status OK")93 }94 ct := strings.ToLower(res.Header.Get("Content-Type"))95 if !strings.Contains(ct, "text/html") {96 res.Body.Close()97 return nil, errors.New("Not html")98 }99 return res.Body, nil100}101func getCandidateIcons(domain string, r io.Reader) []string {102 tokenizer := html.NewTokenizer(r)103 candidates := make(map[string]int)104 // Consider only the first 1000 tokens, as the candidates icons must be in105 // the <head>, and it avoid reading the whole html page.106 for i := 0; i < 1000; i++ {107 switch tokenizer.Next() {108 case html.ErrorToken:109 // End of the document, we're done110 break111 case html.StartTagToken, html.SelfClosingTagToken:112 t := tokenizer.Token()113 if u, p := getLinkIcon(domain, t); p >= 0 {114 candidates[u] = p115 }116 }117 }118 sorted := make([]string, 0, len(candidates))119 for k := range candidates {120 sorted = append(sorted, k)121 }122 sort.SliceStable(sorted, func(i, j int) bool {123 return candidates[sorted[i]] > candidates[sorted[j]]124 })125 return sorted126}127// getLinkIcon returns the href and the priority for the link.128// -1 means that it is not a suitable icon link.129// Higher priority is better.130func getLinkIcon(domain string, t html.Token) (string, int) {131 if strings.ToLower(t.Data) != "link" {132 return "", -1133 }134 isIcon := false135 href := ""136 priority := 100137 for _, attr := range t.Attr {138 switch strings.ToLower(attr.Key) {139 case "rel":140 vals := strings.Split(strings.ToLower(attr.Val), " ")141 for _, val := range vals {142 if val == "icon" || val == "apple-touch-icon" {143 isIcon = true144 if val == "icon" {145 priority += 10146 }147 }148 }149 case "href":150 href = attr.Val151 if strings.HasSuffix(href, ".png") {152 priority += 2153 }154 case "sizes":155 w, h := parseSizes(attr.Val)156 if w != h {157 priority -= 100158 } else if w == 32 {159 priority += 400160 } else if w == 64 {161 priority += 300162 } else if w >= 24 && w <= 128 {163 priority += 200164 } else if w == 16 {165 priority += 100166 }167 }168 }169 if !isIcon || href == "" {170 return "", -1171 }172 if !strings.Contains(href, "://") {173 href = strings.TrimPrefix(href, "./")174 href = strings.TrimPrefix(href, "/")175 href = "https://" + domain + "/" + href176 }177 return href, priority178}179func parseSizes(val string) (int, int) {180 parts := strings.Split(val, "x")181 if len(parts) != 2 {182 return 0, 0183 }184 w, err := strconv.Atoi(parts[0])185 if err != nil {186 return 0, 0187 }188 h, err := strconv.Atoi(parts[1])189 if err != nil {190 return 0, 0191 }192 return w, h193}194func downloadFavicon(domain string) (*Icon, error) {195 icon, err := downloadIcon("https://" + domain + "/favicon.ico")196 if err == nil {197 return icon, nil198 }199 // Try again200 time.Sleep(1 * time.Second)201 return downloadIcon("https://" + domain + "/favicon.ico")202}203func downloadIcon(u string) (*Icon, error) {204 req, err := http.NewRequest(http.MethodGet, u, nil)205 if err != nil {206 return nil, err207 }208 res, err := iconClient.Do(req)209 if err != nil {210 return nil, err211 }212 defer res.Body.Close()213 if res.StatusCode != http.StatusOK {214 return nil, errors.New("Not status OK")215 }216 b, err := ioutil.ReadAll(res.Body)217 if err != nil {218 return nil, err219 }220 if len(b) > maxSize {221 return nil, errors.New("Max size exceeded")222 }223 ico := Icon{224 Mime: res.Header.Get("Content-Type"),225 Body: b,226 }227 if strings.Split(ico.Mime, "/")[0] != "image" {228 return nil, errors.New("Invalid mime-type")229 }230 return &ico, nil231}...
icon_test.go
Source:icon_test.go
1package bitwarden2import (3 "strings"4 "testing"5 "github.com/stretchr/testify/assert"6)7func TestValidateDomain(t *testing.T) {8 err := validateDomain("")9 assert.Equal(t, err.Error(), "Unauthorized domain")10 err = validateDomain("foo bar baz")11 assert.Equal(t, err.Error(), "Invalid domain")12 err = validateDomain("192.168.0.1")13 assert.Equal(t, err.Error(), "IP address are not authorized")14 err = validateDomain("example.com")15 assert.NoError(t, err)16}17func TestCandidateIcons(t *testing.T) {18 html := `<html><head><title>Foo</title></head><body><h1>Foo</h1></body></html>`19 candidates := getCandidateIcons("example.com", strings.NewReader(html))20 assert.Len(t, candidates, 0)21 html = `<!DOCTYPE html>22<html lang="fr">23<head>24<meta charset="utf-8">25<title>Accueil - LinuxFr.org</title>26<style type="text/css">header#branding h1 { background-image: url(/images/logos/linuxfr2_gouttes.png) }</style>27<link rel="stylesheet" href="/assets/application-e25e004c56c9986c6dc2b7d54b8640425a021edaa68e2948eff1c8dbc668caa0.css" />28<link rel="shortcut icon" type="image/x-icon" href="/favicon.png" />29<link rel="alternate" type="application/atom+xml" title="Flux Atom des dépêches" href="/news.atom" />30</head>31<body class="logged admin" id="home-index">32...33</body>34</html>35`36 candidates = getCandidateIcons("linuxfr.org", strings.NewReader(html))37 assert.Len(t, candidates, 1)38 assert.Equal(t, candidates[0], "https://linuxfr.org/favicon.png")39 html = `<!DOCTYPE html>40 <html>41 <head>42 <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">43 <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32">44 <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16">45 </head>46 <body>47 ...48 </body>49 </html>`50 candidates = getCandidateIcons("example.com", strings.NewReader(html))51 assert.Len(t, candidates, 3)52 assert.Equal(t, candidates[0], "https://example.com/favicon-32x32.png")53 assert.Equal(t, candidates[1], "https://example.com/favicon-16x16.png")54 assert.Equal(t, candidates[2], "https://example.com/apple-touch-icon.png")55 html = `<!DOCTYPE html>56<html>57<head>58<link rel="apple-touch-icon" href="https://static.example.org/apple-touch-icon.png" />59<link rel="icon" type="image/png" href="./images/favicon.png">60</head>61<body>62...63</body>64</html>`65 candidates = getCandidateIcons("example.com", strings.NewReader(html))66 assert.Len(t, candidates, 2)67 assert.Equal(t, candidates[0], "https://example.com/images/favicon.png")68 assert.Equal(t, candidates[1], "https://static.example.org/apple-touch-icon.png")69}...
Href
Using AI Code Generation
1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 defer resp.Body.Close()7 body, err := ioutil.ReadAll(resp.Body)8 if err != nil {9 fmt.Println(err)10 }11 doc, err := html.Parse(strings.NewReader(string(body)))12 if err != nil {13 fmt.Println(err)14 }15 var f func(*html.Node)16 f = func(n *html.Node) {17 if n.Type == html.ElementNode && n.Data == "a" {18 for _, a := range n.Attr {19 if a.Key == "href" {20 fmt.Println(a.Val)21 }22 }23 }24 for c := n.FirstChild; c != nil; c = c.NextSibling {25 f(c)26 }27 }28 f(doc)29}
Href
Using AI Code Generation
1import (2func main() {3 if err != nil {4 panic(err)5 }6 for _, n := range nodes {7 fmt.Println(htmlquery.SelectAttr(n, "href"))8 }9}
Href
Using AI Code Generation
1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 doc.Find("a").Each(func(index int, item *goquery.Selection) {7 link, _ := item.Attr("href")8 fmt.Println(link)9 })10}
Href
Using AI Code Generation
1import (2func main() {3 doc := js.Global.Get("document")4 a := doc.Call("createElement", "a")5 fmt.Println(a.Get("href").String())6}7import (8func main() {9 doc := js.Global.Get("document")10 a := doc.Call("createElement", "a")11 fmt.Println(a.Get("href").String())12}13import (14func main() {15 doc := js.Global.Get("document")16 a := doc.Call("createElement", "a")17 fmt.Println(a.Get("href").String())18}19import (20func main() {21 doc := js.Global.Get("document")22 a := doc.Call("createElement", "a")23 fmt.Println(a.Get("href").String())24}25import (26func main() {27 doc := js.Global.Get("document")28 a := doc.Call("createElement", "a")29 fmt.Println(a.Get("href").String())30}31import (32func main() {33 doc := js.Global.Get("document")34 a := doc.Call("createElement", "a")35 fmt.Println(a.Get("href").String())36}37import (
Href
Using AI Code Generation
1import (2func main() {3 defer resp.Body.Close()4 body, _ := ioutil.ReadAll(resp.Body)5 doc, _ := html.Parse(strings.NewReader(string(body)))6 forEachNode(doc, startElement, endElement)7}8func startElement(n *html.Node) {9 if n.Type == html.ElementNode {10 fmt.Printf("%v11 }12}13func endElement(n *html.Node) {14 if n.Type == html.ElementNode {15 fmt.Printf("%v16 }17}18func forEachNode(n *html.Node, pre, post func(n *html.Node)) {19 if pre != nil {20 pre(n)21 }22 for c := n.FirstChild; c != nil; c = c.NextSibling {23 forEachNode(c, pre, post)24 }25 if post != nil {26 post(n)27 }28}
Href
Using AI Code Generation
1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 doc.Find("a").Each(func(index int, element *goquery.Selection) {7 link, _ := element.Attr("href")8 fmt.Println(link)9 })10}
Href
Using AI Code Generation
1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 doc, err := goquery.NewDocumentFromReader(res.Body)7 if err != nil {8 log.Fatal(err)9 }10 doc.Find(".main-menu li a").Each(func(i int, s *goquery.Selection) {11 link, _ := linkTag.Attr("href")12 fmt.Printf("Review %d: %s13 })14}
Href
Using AI Code Generation
1import (2const (3func main() {4 resp, err := http.Get(url)5 if err != nil {6 log.Fatal(err)7 }8 defer resp.Body.Close()9 doc, err := html.Parse(resp.Body)10 if err != nil {11 log.Fatal(err)12 }13 links := make(map[string]struct{})14 wg.Add(1)15 go func() {16 defer wg.Done()17 visit(links, doc)18 }()19 wg.Wait()20 for link := range links {21 fmt.Println(link)22 }23}24func visit(links map[string]struct{}, n *html.Node) {25 if n.Type == html.ElementNode && n.Data == "a" {26 for _, a := range n.Attr {27 if a.Key == "href" {28 link, err := n.Href()29 if err != nil {30 log.Fatal(err)31 }32 if strings.HasPrefix(link, "/") {33 }34 links[link] = struct{}{}35 }36 }37 }38 for c := n.FirstChild; c != nil; c = c.NextSibling {39 visit(links, c)40 }41}
Href
Using AI Code Generation
1import (2func main() {3 doc, err := html.Parse(strings.NewReader(htmlDoc))4 if err != nil {5 fmt.Println(err)6 }7 hrefs := Href(doc)8 fmt.Println(hrefs)9}10import (11func main() {12 doc, err := html.Parse(strings.NewReader(htmlDoc))13 if err != nil {14 fmt.Println(err)15 }16 hrefs := Href(doc)17 fmt.Println(hrefs)18}19import (20func main() {21 doc, err := html.Parse(strings.NewReader(htmlDoc))22 if err != nil {23 fmt.Println(err)24 }25 hrefs := Href(doc)26 fmt.Println(hrefs)27}28import (29func main() {30 doc, err := html.Parse(strings.NewReader(htmlDoc))31 if err != nil {32 fmt.Println(err)33 }34 hrefs := Href(doc)35 fmt.Println(hrefs)36}37import (38func main() {39 doc, err := html.Parse(strings.NewReader(htmlDoc))40 if err != nil {41 fmt.Println(err)42 }43 hrefs := Href(doc)44 fmt.Println(hrefs)45}46import (47func main() {48 doc, err := html.Parse(strings.NewReader(htmlDoc))49 if err != nil {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!