| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io/ioutil" |
| 6 | "log" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func equal(u, v []string) bool { |
| 15 | if len(u) != len(v) { |
| 16 | return false |
| 17 | } |
| 18 | for i, _ := range u { |
| 19 | if u[i] != v[i] { |
| 20 | return false |
| 21 | } |
| 22 | } |
| 23 | return true |
| 24 | } |
| 25 | |
| 26 | func readFile(fname string, t *testing.T) string { |
| 27 | b, err := ioutil.ReadFile(fname) |
| 28 | if err != nil { |
| 29 | t.Fatal("readFile", err) |
| 30 | } |
| 31 | return string(b) |
| 32 | } |
| 33 | |
| 34 | func TestDefectiveScriptParser(t *testing.T) { |
| 35 | if l := len(findScriptSrc(`<!DOCTYPE HTML> |
| 36 | <html> |
| 37 | <body> |
| 38 | |
| 39 | <video width="320" height="240" controls="controls"> |
| 40 | <source src="movie.mp4" type="video/mp4" /> |
| 41 | <source src="movie.ogg" type="video/ogg" /> |
| 42 | <source src="movie.webm" type="video/webm" /> |
| 43 | Your browser does not support the video tag. |
| 44 | </video> |
| 45 | |
| 46 | </body> |
| 47 | </html>`)); l != 0 { |
| 48 | t.Fail() |
| 49 | } |
| 50 | urls := findScriptSrc(readFile("w3schools.html", t)) |
| 51 | if !equal(urls, []string{"http://partner.googleadservices.com/gampad/google_service.js", |
| 52 | "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"}) { |
| 53 | t.Error("w3schools.html", "src scripts are not recognized", urls) |
| 54 | } |
| 55 | urls = findScriptSrc(readFile("jquery_homepage.html", t)) |
| 56 | if !equal(urls, []string{"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", |
| 57 | "http://code.jquery.com/jquery-1.4.2.min.js", |
| 58 | "http://static.jquery.com/files/rocker/scripts/custom.js", |
| 59 | "http://static.jquery.com/donate/donate.js"}) { |
| 60 | t.Error("jquery_homepage.html", "src scripts are not recognized", urls) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func proxyWithLog() (*http.Client, *bytes.Buffer) { |
| 65 | proxy := NewJqueryVersionProxy() |
| 66 | proxyServer := httptest.NewServer(proxy) |
| 67 | buf := new(bytes.Buffer) |
| 68 | proxy.Logger = log.New(buf, "", 0) |
| 69 | proxyUrl, _ := url.Parse(proxyServer.URL) |
| 70 | tr := &http.Transport{Proxy: http.ProxyURL(proxyUrl)} |
| 71 | client := &http.Client{Transport: tr} |
| 72 | return client, buf |
| 73 | } |
| 74 | |
| 75 | func get(t *testing.T, server *httptest.Server, client *http.Client, url string) { |
| 76 | resp, err := client.Get(server.URL + url) |
| 77 | if err != nil { |
| 78 | t.Fatal("cannot get proxy", err) |
| 79 | } |
| 80 | ioutil.ReadAll(resp.Body) |
| 81 | resp.Body.Close() |
| 82 | } |
| 83 | |
| 84 | func TestProxyServiceTwoVersions(t *testing.T) { |
| 85 | var fs = httptest.NewServer(http.FileServer(http.Dir("."))) |
| 86 | defer fs.Close() |
| 87 | |
| 88 | client, buf := proxyWithLog() |
| 89 | |
| 90 | get(t, fs, client, "/w3schools.html") |
| 91 | get(t, fs, client, "/php_man.html") |
| 92 | if buf.String() != "" && |
| 93 | !strings.Contains(buf.String(), " uses jquery ") { |
| 94 | t.Error("shouldn't warn on a single URL", buf.String()) |
| 95 | } |
| 96 | get(t, fs, client, "/jquery1.html") |
| 97 | warnings := buf.String() |
| 98 | if !strings.Contains(warnings, "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js") || |
| 99 | !strings.Contains(warnings, "jquery.1.4.js") || |
| 100 | !strings.Contains(warnings, "Contradicting") { |
| 101 | t.Error("contradicting jquery versions (php_man.html, w3schools.html) does not issue warning", warnings) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestProxyService(t *testing.T) { |
| 106 | var fs = httptest.NewServer(http.FileServer(http.Dir("."))) |
| 107 | defer fs.Close() |
| 108 | |
| 109 | client, buf := proxyWithLog() |
| 110 | |
| 111 | get(t, fs, client, "/jquery_homepage.html") |
| 112 | warnings := buf.String() |
| 113 | if !strings.Contains(warnings, "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js") || |
| 114 | !strings.Contains(warnings, "http://code.jquery.com/jquery-1.4.2.min.js") || |
| 115 | !strings.Contains(warnings, "Contradicting") { |
| 116 | t.Error("contradicting jquery versions does not issue warning") |
| 117 | } |
| 118 | } |