Hi,
I made a little web page test frame in my own "framework", it's not good enough to publish, but maybe it should inspire you, it works like this:
(run the server app first!)
// Test signup
hc = NewHttpClient("/signup").Get()
//fmt.Println("hc.Jar:", hc.Jar) // cookie jar
hc.T(t).ExpectPage("signup")
hc.SetFormToken().T(t) // form token, to avoid duplicate post or do some validation
hc.Set("FirstName", testFirstName)
hc.Set("LastName", testLastName)
hc.Set("Email", testEmail)
hc.Set("Password", testPassword)
hc.Set("Password2", testPassword) // Add right confirm password
hc.PostForm().T(t).CheckRedirect("/user")
// get "/user" page, use same HttpClient, because need returned cookie
ht := hc.Url("/user").Get().T(t)
ht.Expect("#FirstName", testFirstName, "get first name")
ht.Expect("#LastName", testLastName, "get last name")
ht.Expect("#Email", testEmail, "get email")
Some type define:
type HttpClient struct {
*http.Client
*http.Request
Resp *http.Response
Doc *goquery.Document
url string
bodyType string // for Post
urlValues url.Values // Post form
userAgent string // for crawler
Err error
}
type HttpTest struct {
t *testing.T
*HttpClient
}
func (hc *HttpClient) T(t *testing.T) *HttpTest {
CheckError(t, hc.Err)
return &HttpTest{
t: t,
HttpClient: hc,
}
}