Ejemplo n.º 1
0
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())
}
Ejemplo n.º 2
0
// 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)
}
Ejemplo n.º 3
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())
}
Ejemplo n.º 4
0
func TestFileExists(t *testing.T) {
	ut.Run(t)

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

	ex = FileExists("./util.txt")
	ut.AssertFalse(ex)
}
Ejemplo n.º 5
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)

	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())
}