// assertBookmarks tests the given bookmark jar. func assertBookmarks(b BookmarksJar) { err := b.Save("test1", "http://localhost") ut.AssertNil(err) err = b.Save("test2", "http://127.0.0.1") ut.AssertNil(err) err = b.Save("test1", "http://localhost") ut.AssertNotNil(err) url, err := b.Read("test1") ut.AssertNil(err) ut.AssertEquals("http://localhost", url) url, err = b.Read("test2") ut.AssertEquals("http://127.0.0.1", url) url, err = b.Read("test3") ut.AssertNotNil(err) r := b.Remove("test2") ut.AssertTrue(r) r = b.Remove("test3") ut.AssertFalse(r) r = b.Has("test1") ut.AssertTrue(r) r = b.Has("test4") ut.AssertFalse(r) }
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()) }
func TestBrowserForm(t *testing.T) { ts := setupTestServer(htmlForm, t) 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) v, ok := f.Field("age") ut.AssertTrue(ok) ut.AssertEquals("", v) v, ok = f.Field("ageage") ut.AssertFalse(ok) ut.AssertEquals("", v) f.Input("age", "55") v, ok = f.Field("age") ut.AssertTrue(ok) ut.AssertEquals("55", v) 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()) }
func TestFileExists(t *testing.T) { ut.Run(t) ex := FileExists("./util_test.go") ut.AssertTrue(ex) ex = FileExists("./util.txt") ut.AssertFalse(ex) }
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()) }
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) v, ok := f.Field("age") ut.AssertTrue(ok) ut.AssertEquals("", v) v, ok = f.Field("ageage") ut.AssertFalse(ok) ut.AssertEquals("", v) f.Input("age", "55") v, ok = f.Field("age") ut.AssertTrue(ok) ut.AssertEquals("55", v) 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()) }
func TestBrowserForm2(t *testing.T) { ts := setupTestServer(htmlForm2, t) 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()) }