8058
Ir
irvc / files
irvc / preview_test.go
4.8 KB
1package main 2 3import ( 4 "context" 5 "net" 6 "net/http" 7 "net/http/httptest" 8 "os" 9 "strings" 10 "testing" 11 "time" 12) 13 14func TestFirstLink(t *testing.T) { 15 cases := map[string]string{ 16 "look https://example.com/a?b=1.": "https://example.com/a?b=1", 17 "(see https://example.com/x)": "https://example.com/x", 18 "https://en.wikipedia.org/wiki/Go_(language) neat": "https://en.wikipedia.org/wiki/Go_(language)", 19 "\nno link": "", 20 " then http://b.test/ok": "http://b.test/ok", 21 "first https://one.test second https://two.test": "https://one.test", 22 "just text, ftp://nope.test": "", 23 } 24 for in, want := range cases { 25 if got := firstLink(in); got != want { 26 t.Errorf("firstLink(%q) = %q, want %q", in, got, want) 27 } 28 } 29} 30 31func TestPublicIP(t *testing.T) { 32 for ip, want := range map[string]bool{ 33 "8.8.8.8": true, "2606:4700::1111": true, 34 "127.0.0.1": false, "::1": false, "10.1.2.3": false, "192.168.0.9": false, 35 "172.16.5.5": false, "169.254.169.254": false, "100.64.0.1": false, 36 "0.0.0.0": false, "fe80::1": false, "fd00::1": false, "::ffff:127.0.0.1": false, 37 } { 38 if got := publicIP(net.ParseIP(ip)); got != want { 39 t.Errorf("publicIP(%s) = %v, want %v", ip, got, want) 40 } 41 } 42} 43 44func TestPreviewFetch(t *testing.T) { 45 png := []byte("\x89PNG\r\n\x1a\n" + strings.Repeat("x", 64)) 46 mux := http.NewServeMux() 47 mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) { 48 w.Header().Set("Content-Type", "text/html; charset=utf-8") 49 w.Write([]byte(`<html><head><title>ignored</title> 50 <meta property="og:title" content=" Hello World "> 51 <meta property="og:description" content="A & B"> 52 <meta name="og:site_name" content="Example"> 53 <meta property="og:image" content="/pic.png"> 54 </head><body><meta property="og:title" content="late"></body></html>`)) 55 }) 56 mux.HandleFunc("/pic.png", func(w http.ResponseWriter, r *http.Request) { 57 w.Header().Set("Content-Type", "image/png") 58 w.Write(png) 59 }) 60 mux.HandleFunc("/plain", func(w http.ResponseWriter, r *http.Request) { 61 w.Header().Set("Content-Type", "text/html") 62 w.Write([]byte(`<html><head><title>no og</title></head><body>hi</body></html>`)) 63 }) 64 mux.HandleFunc("/file", func(w http.ResponseWriter, r *http.Request) { 65 w.Header().Set("Content-Type", "application/zip") 66 w.Write([]byte("PK")) 67 }) 68 mux.HandleFunc("/huge", func(w http.ResponseWriter, r *http.Request) { 69 w.Header().Set("Content-Type", "text/html") 70 w.Write([]byte("<html><head>")) 71 // An endless head with no og tags: reading must stop at the cap. 72 junk := []byte(strings.Repeat("<!-- x -->", 1000)) 73 for { 74 if _, err := w.Write(junk); err != nil { 75 return 76 } 77 } 78 }) 79 srv := httptest.NewServer(mux) 80 defer srv.Close() 81 82 dir := t.TempDir() 83 p := newPreviewer(dir, true) 84 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 85 defer cancel() 86 87 card := p.fetch(ctx, srv.URL+"/page") 88 if card == nil || card.Kind != kindCard || card.Title != "Hello World" || 89 card.Description != "A & B" || card.Site != "Example" { 90 t.Fatalf("bad card: %+v", card) 91 } 92 if !strings.HasPrefix(card.Image, "/api/media/") || !strings.HasSuffix(card.Image, ".png") { 93 t.Fatalf("thumbnail should be cached locally, got %q", card.Image) 94 } 95 if _, err := os.Stat(dir + "/" + strings.TrimPrefix(card.Image, "/api/media/")); err != nil { 96 t.Fatalf("cached image missing: %v", err) 97 } 98 99 for path, kind := range map[string]string{"/pic.png": kindImage, "/file": kindOther, "/plain": kindNone, "/huge": kindNone} { 100 if got := p.fetch(ctx, srv.URL+path); got == nil || got.Kind != kind { 101 t.Errorf("%s: got %+v, want kind %s", path, got, kind) 102 } 103 } 104 105 if got := newPreviewer(dir, false).fetch(ctx, srv.URL+"/page"); got != nil { 106 t.Errorf("loopback must be blocked by default, got %+v", got) 107 } 108} 109 110// Meta values are decoded to plain text: single and double encoded entities 111// both come out as the characters they stand for, and markup stays literal. 112func TestParseOpenGraphEntities(t *testing.T) { 113 doc := `<head> 114 <meta property="og:title" content="Tom &amp; Jerry ’s "best""> 115 <meta property="og:description" content="1 < 2 && <b>bold</b>"> 116 <meta property="og:site_name" content="Plain & simple"> 117 <meta property="og:image" content="a�bc"> 118 </head>` 119 og := parseOpenGraph(strings.NewReader(doc), "text/html; charset=utf-8") 120 for key, want := range map[string]string{ 121 "title": `Tom & Jerry ’s "best"`, 122 "description": "1 < 2 && <b>bold</b>", 123 "site_name": "Plain & simple", 124 } { 125 if og[key] != want { 126 t.Errorf("og:%s = %q, want %q", key, og[key], want) 127 } 128 } 129 if strings.ContainsAny(og["image"], "\x00\x07") { 130 t.Errorf("control characters survived: %q", og["image"]) 131 } 132}