Beispiel #1
0
func TestScripts(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprint(w, htmlPage1)
	}))
	defer ts.Close()

	bow := NewBrowser()
	err := bow.Open(ts.URL)
	ut.AssertNil(err)

	scripts := bow.Scripts()
	ut.AssertEquals(2, len(scripts))
	ut.AssertEquals("http://godoc.org/-/site.js", scripts[0].URL.String())
	ut.AssertEquals("text/javascript", scripts[0].Type)

	ut.AssertEquals(ts.URL+"/jquery.min.js", scripts[1].URL.String())
	ut.AssertEquals("text/javascript", scripts[1].Type)

	buff := &bytes.Buffer{}
	l, err := scripts[0].Download(buff)
	ut.AssertNil(err)
	ut.AssertGreaterThan(0, buff.Len())
	ut.AssertEquals(int(l), buff.Len())
}
Beispiel #2
0
func TestImages(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprint(w, htmlPage1)
	}))
	defer ts.Close()

	bow := NewBrowser()
	err := bow.Open(ts.URL)
	ut.AssertNil(err)

	images := bow.Images()
	ut.AssertEquals(2, len(images))
	ut.AssertEquals("imgur-image", images[0].ID)
	ut.AssertEquals("http://i.imgur.com/HW4bJtY.jpg", images[0].URL.String())
	ut.AssertEquals("", images[0].Alt)
	ut.AssertEquals("It's a...", images[0].Title)

	ut.AssertEquals("", images[1].ID)
	ut.AssertEquals(ts.URL+"/Cxagv.jpg", images[1].URL.String())
	ut.AssertEquals("A picture", images[1].Alt)
	ut.AssertEquals("", images[1].Title)

	buff := &bytes.Buffer{}
	l, err := images[0].Download(buff)
	ut.AssertNil(err)
	ut.AssertGreaterThan(0, buff.Len())
	ut.AssertEquals(int(l), buff.Len())
}
Beispiel #3
0
func TestBrowserForm(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			fmt.Fprint(w, htmlForm)
		} else {
			r.ParseForm()
			fmt.Fprint(w, r.Form.Encode())
		}
	}))
	defer ts.Close()

	bow := &Browser{}
	bow.headers = make(http.Header, 10)
	bow.history = jar.NewMemoryHistory()

	err := bow.Open(ts.URL)
	ut.AssertNil(err)

	f, err := bow.Form("[name='default']")
	ut.AssertNil(err)

	f.Input("age", "55")
	f.Input("gender", "male")
	err = f.Click("submit2")
	ut.AssertNil(err)
	ut.AssertContains("age=55", bow.Body())
	ut.AssertContains("gender=male", bow.Body())
	ut.AssertContains("submit2=submitted2", bow.Body())
}
Beispiel #4
0
func TestGet(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		if req.URL.Path == "/page1" {
			fmt.Fprint(w, htmlPage1)
		} else if req.URL.Path == "/page2" {
			fmt.Fprint(w, htmlPage2)
		}
	}))
	defer ts.Close()

	bow := NewBrowser()

	err := bow.Open(ts.URL + "/page1")
	ut.AssertNil(err)
	ut.AssertEquals("Surf Page 1", bow.Title())
	ut.AssertContains("<p>Hello, Surf!</p>", bow.Body())

	err = bow.Open(ts.URL + "/page2")
	ut.AssertNil(err)
	ut.AssertEquals("Surf Page 2", bow.Title())

	ok := bow.Back()
	ut.AssertTrue(ok)
	ut.AssertEquals("Surf Page 1", bow.Title())

	ok = bow.Back()
	ut.AssertFalse(ok)
	ut.AssertEquals("Surf Page 1", bow.Title())
}
Beispiel #5
0
func TestFileExists(t *testing.T) {
	ut.Run(t)

	ex := FileExists("./util_test.go")
	ut.AssertTrue(ex)

	ex = FileExists("./util.txt")
	ut.AssertFalse(ex)
}
Beispiel #6
0
func TestFileBookmarks(t *testing.T) {
	ut.Run(t)

	b, err := NewFileBookmarks("./bookmarks.json")
	ut.AssertNil(err)
	defer func() {
		err = os.Remove("./bookmarks.json")
	}()
	assertBookmarks(b)
}
Beispiel #7
0
func TestDownload(t *testing.T) {
	ut.Run(t)

	out := &bytes.Buffer{}
	u, _ := url.Parse("http://i.imgur.com/HW4bJtY.jpg")
	asset := NewImageAsset(u, "", "", "")
	l, err := DownloadAsset(asset, out)
	ut.AssertNil(err)
	ut.AssertGreaterThan(0, int(l))
	ut.AssertEquals(int(l), out.Len())
}
Beispiel #8
0
func setupTestServer(html string, t *testing.T) *httptest.Server {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			fmt.Fprint(w, html)
		} else {
			r.ParseForm()
			fmt.Fprint(w, r.Form.Encode())
		}
	}))

	return ts
}
Beispiel #9
0
func TestUserAgent(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprint(w, req.UserAgent())
	}))
	defer ts.Close()

	bow := NewBrowser()
	bow.SetUserAgent("Testing/1.0")
	err := bow.Open(ts.URL)
	ut.AssertNil(err)
	ut.AssertEquals("Testing/1.0", bow.Body())
}
Beispiel #10
0
// TestHeadersSet
// See: https://github.com/headzoo/surf/pull/19
func TestHeadersBug19(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprint(w, req.Header.Get("X-Testing"))
	}))
	defer ts.Close()

	bow := NewBrowser()
	bow.AddRequestHeader("X-Testing", "Testing-1")
	bow.AddRequestHeader("X-Testing", "Testing-2")
	err := bow.Open(ts.URL)
	ut.AssertNil(err)
	ut.AssertContains("Testing-2", bow.Body())
}
Beispiel #11
0
func TestHead(t *testing.T) {
	ut.Run(t)
	var r *http.Request

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		r = req
	}))
	defer ts.Close()

	bow := NewBrowser()

	err := bow.Head(ts.URL + "/page1")
	ut.AssertNil(err)
	ut.AssertNotNil(r)
}
Beispiel #12
0
func TestDownloadContentType(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		b := bytes.NewBufferString("Hello")
		fmt.Fprint(w, b)
	}))
	defer ts.Close()

	bow := NewBrowser()
	bow.Open(ts.URL)

	buff := &bytes.Buffer{}
	bow.Download(buff)
	ut.AssertEquals("Hello", buff.String())
}
Beispiel #13
0
func TestBrowserForm2(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "GET" {
			fmt.Fprint(w, htmlForm2)
		} else {
			r.ParseForm()
			fmt.Fprint(w, r.Form.Encode())
		}
	}))
	defer ts.Close()

	bow := &Browser{}
	bow.headers = make(http.Header, 10)
	bow.history = jar.NewMemoryHistory()

	err := bow.Open(ts.URL)
	ut.AssertNil(err)

	f, err := bow.Form("[name='default']")
	ut.AssertNil(err)

	err = f.Input("age", "54")
	ut.AssertNil(err)
	err = f.Input("agee", "54")
	ut.AssertNotNil(err)

	err = f.CheckBox("music", []string{"rock", "fusion"})
	ut.AssertNil(err)

	err = f.CheckBox("music2", []string{"rock", "fusion"})
	ut.AssertNotNil(err)

	err = f.Click("submit2")

	ut.AssertNil(err)
	ut.AssertContains("company=none", bow.Body())
	ut.AssertContains("age=54", bow.Body())
	ut.AssertContains("gender=male", bow.Body())

	ut.AssertFalse(strings.Contains(bow.Body(), "music=jazz"))
	ut.AssertContains("music=rock", bow.Body())
	ut.AssertContains("music=fusion", bow.Body())

	ut.AssertContains("hobby=Dance", bow.Body())
	ut.AssertContains("city=NY", bow.Body())
	ut.AssertContains("submit2=submitted2", bow.Body())
}
Beispiel #14
0
func TestDownload(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprint(w, req.UserAgent())
	}))
	defer ts.Close()

	bow := NewBrowser()
	bow.Open(ts.URL)

	buff := &bytes.Buffer{}
	l, err := bow.Download(buff)
	ut.AssertNil(err)
	ut.AssertGreaterThan(0, int(l))
	ut.AssertEquals(int(l), buff.Len())
}
Beispiel #15
0
func TestPost(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		if req.Method == "POST" {
			w.WriteHeader(200)
			w.Write([]byte("OK"))
		} else {
			w.WriteHeader(500)
			w.Write([]byte("ERROR"))
		}
	}))
	defer ts.Close()

	bow := NewBrowser()
	bow.Post(ts.URL, "application/x-www-form-urlencoded", nil)
	ut.AssertEquals(200, bow.StatusCode())
}
Beispiel #16
0
func TestClick(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/" {
			fmt.Fprint(w, htmlPage1)
		} else if r.URL.Path == "/page2" {
			fmt.Fprint(w, htmlPage1)
		}
	}))
	defer ts.Close()

	bow := NewBrowser()
	err := bow.Open(ts.URL)
	ut.AssertNil(err)

	err = bow.Click("a:contains('click')")
	ut.AssertNil(err)
	ut.AssertContains("<p>Hello, Surf!</p>", bow.Body())
}
Beispiel #17
0
func TestLinks(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprint(w, htmlPage1)
	}))
	defer ts.Close()

	bow := NewBrowser()
	err := bow.Open(ts.URL)
	ut.AssertNil(err)

	links := bow.Links()
	ut.AssertEquals(2, len(links))
	ut.AssertEquals("", links[0].ID)
	ut.AssertEquals(ts.URL+"/page2", links[0].URL.String())
	ut.AssertEquals("click", links[0].Text)
	ut.AssertEquals("page3", links[1].ID)
	ut.AssertEquals(ts.URL+"/page3", links[1].URL.String())
	ut.AssertEquals("no clicking", links[1].Text)
}
Beispiel #18
0
func TestBookmarks(t *testing.T) {
	ut.Run(t)
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		fmt.Fprint(w, htmlPage1)
	}))
	defer ts.Close()

	bookmarks := jar.NewMemoryBookmarks()
	bow := NewBrowser()
	bow.SetBookmarksJar(bookmarks)

	bookmarks.Save("test1", ts.URL)
	bow.OpenBookmark("test1")
	ut.AssertEquals("Surf Page 1", bow.Title())
	ut.AssertContains("<p>Hello, Surf!</p>", bow.Body())

	err := bow.Bookmark("test2")
	ut.AssertNil(err)
	bow.OpenBookmark("test2")
	ut.AssertEquals("Surf Page 1", bow.Title())
}
Beispiel #19
0
func TestMemoryHistory(t *testing.T) {
	ut.Run(t)
	stack := NewMemoryHistory()

	page1 := &State{}
	stack.Push(page1)
	ut.AssertEquals(1, stack.Len())
	ut.AssertEquals(page1, stack.Top())

	page2 := &State{}
	stack.Push(page2)
	ut.AssertEquals(2, stack.Len())
	ut.AssertEquals(page2, stack.Top())

	page := stack.Pop()
	ut.AssertEquals(page, page2)
	ut.AssertEquals(1, stack.Len())
	ut.AssertEquals(page1, stack.Top())

	page = stack.Pop()
	ut.AssertEquals(page, page1)
	ut.AssertEquals(0, stack.Len())
}
Beispiel #20
0
func TestDownloadAsync(t *testing.T) {
	ut.Run(t)

	ch := make(AsyncDownloadChannel, 1)
	u1, _ := url.Parse("http://i.imgur.com/HW4bJtY.jpg")
	u2, _ := url.Parse("http://i.imgur.com/HkPOzEH.jpg")
	asset1 := NewImageAsset(u1, "", "", "")
	asset2 := NewImageAsset(u2, "", "", "")
	out1 := &bytes.Buffer{}
	out2 := &bytes.Buffer{}

	queue := 2
	DownloadAssetAsync(asset1, out1, ch)
	DownloadAssetAsync(asset2, out2, ch)

	for {
		select {
		case result := <-ch:
			ut.AssertGreaterThan(0, int(result.Size))
			if result.Asset == asset1 {
				ut.AssertEquals(int(result.Size), out1.Len())
			} else if result.Asset == asset2 {
				ut.AssertEquals(int(result.Size), out2.Len())
			} else {
				t.Failed()
			}
			queue--
			if queue == 0 {
				goto COMPLETE
			}
		}
	}

COMPLETE:
	close(ch)
	ut.AssertEquals(0, queue)
}
Beispiel #21
0
func TestChrome(t *testing.T) {
	ut.Run(t)
	ut.AssertEquals("Mozilla/5.0 (Windows NT 6.3; x64) Chrome/37.0.2049.0 Safari/537.36", Chrome())
}
Beispiel #22
0
func TestCreate(t *testing.T) {
	ut.Run(t)

	ut.AssertEquals(fmt.Sprintf("Surf/%s (%s %s; %s)", Version, osName(), osVersion(), runtime.Version()), Create())

	Name = "Chrome"
	Version = "37.0.2049.0"
	OSName = "Ubuntu"
	OSVersion = "14.04"
	Comments = []string{"X11", "like Gecko"}
	ut.AssertEquals("Mozilla/5.0 (Ubuntu 14.04; X11; like Gecko) Chrome/37.0.2049.0 Safari/537.36", Create())

	Name = "Chrome"
	Version = ""
	OSName = "Ubuntu"
	OSVersion = "14.04"
	Comments = []string{}
	ut.AssertEquals("Mozilla/5.0 (Ubuntu 14.04) Chrome/37.0.2049.0 Safari/537.36", Create())

	Name = "Firefox"
	Version = "31.0"
	Comments = []string{"x64"}
	ut.AssertEquals("Mozilla/5.0 (Ubuntu 14.04; x64; rv:31.0) Gecko/20100101 Firefox/31.0", Create())

	Name = "MSIE"
	Version = "10.6"
	OSName = "Windows NT"
	OSVersion = "6.1"
	Comments = []string{"WOW64"}
	ut.AssertEquals("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/5.0; .NET CLR 3.5.30729)", Create())

	Name = "Opera"
	Version = "12.14"
	OSName = "Windows NT"
	OSVersion = "6.1"
	Comments = []string{"en"}
	ut.AssertEquals("Opera/9.80 (Windows NT 6.1; U; en) Presto/2.9.181 Version/12.14", Create())

	Name = "Safari"
	Version = "6.0"
	OSName = "Intel Mac OS X"
	OSVersion = "10_6_8"
	Comments = []string{}
	ut.AssertEquals("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Safari/8536.25", Create())

	Name = "AOL"
	Version = "9.7"
	OSName = "Windows NT"
	OSVersion = "6.1"
	Comments = []string{}
	ut.AssertEquals("Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLBuild 4343.19; Windows NT 6.1; WOW64; Trident/5.0; FunWebProducts)", Create())

	Name = "Konqueror"
	Version = "4.9"
	OSName = "Linux"
	OSVersion = "3.5"
	Comments = []string{}
	ut.AssertEquals("Mozilla/5.0 (compatible; Konqueror/4.0; Linux) KHTML/4.0.3 (like Gecko)", Create())

	Name = "Netscape"
	Version = "9.0.0.6"
	OSName = "Windows NT"
	OSVersion = "6.1"
	Comments = []string{"en-US"}
	ut.AssertEquals("Mozilla/5.0 (Windows NT; U; Windows NT 6.1; rv:1.9.2.4; en-US) Gecko/20070321 Netscape/9.0.0.6", Create())

	Name = "Lynx"
	Version = "2.8.8dev.3"
	OSName = "Linux"
	OSVersion = "3.5"
	Comments = []string{}
	ut.AssertEquals("Lynx/2.8.8dev.3 libwww-FM/2.14 SSL-MM/1.4.1", Create())
}
Beispiel #23
0
func TestMSIE(t *testing.T) {
	ut.Run(t)
	ut.AssertEquals("Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.3; x64; Trident/5.0; .NET CLR 3.5.30729)", MSIE())
}
Beispiel #24
0
func TestOpera(t *testing.T) {
	ut.Run(t)
	ut.AssertEquals("Opera/9.80 (Windows NT 6.3; U; x64) Presto/2.9.181 Version/12.14", Opera())
}
Beispiel #25
0
func TestSafari(t *testing.T) {
	ut.Run(t)
	ut.AssertEquals("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Safari/8536.25", Safari())
}
Beispiel #26
0
func TestAOL(t *testing.T) {
	ut.Run(t)
	ut.AssertEquals("Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLBuild 4343.19; Windows NT 6.3; WOW64; Trident/5.0; FunWebProducts; x64)", AOL())
}
Beispiel #27
0
func TestMemoryBookmarks(t *testing.T) {
	ut.Run(t)

	b := NewMemoryBookmarks()
	assertBookmarks(b)
}
Beispiel #28
0
func TestFirefox(t *testing.T) {
	ut.Run(t)
	ut.AssertEquals("Mozilla/5.0 (Windows NT 6.3; x64; rv:31.0) Gecko/20100101 Firefox/31.0", Firefox())
}