Example of unit testing with Post data

3,030 views
Skip to first unread message

Fábio Gomes

unread,
Mar 29, 2013, 10:40:56 AM3/29/13
to golan...@googlegroups.com
Hi,

Anyone could point me out to some example of a unit test with Post data?

I would like to write a kind of integration test, but without the UI, I would like to do something like:
  1. Fill in some data to be posted
  2. Call the handler func directly (or through an http client if is not possible)
  3. Check if the handler func returned the proper http status code or redirected to the correct url, etc
The problem is that I don't know from where to start, I tried searching for some articles about it, but couldn't find a proper example.

Can anyone point me to an example of this kind of test?

Thanks.


Kyle Lemons

unread,
Mar 31, 2013, 5:53:07 AM3/31/13
to Fábio Gomes, golang-nuts
I don't have any code handy, but you should be able to construct one reasonably easily:

1) Create a url.Values with the form data
2) WriteString the values.Encode to a bytes.Buffer
3) Create an http.Request with NewRequest
4) Set the Content-Type to "application/x-www-form-urlencoded"
5) Create a httputil.ResponseRecorder
6) Call your handler
7) Check recorded response code and body

HTH




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Fábio Gomes

unread,
Apr 2, 2013, 5:40:25 PM4/2/13
to Kyle Lemons, golang-nuts
Thanks! 

It was easier than I expected:

package main 

import (
"net/http"
"net/http/httptest"
"net/url"
"fmt"
)

func myHandler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprint(w, "Hello world");
}

func main() {
recorder := httptest.NewRecorder()
req := &http.Request{
Method: "GET",
URL:    &url.URL{Path: "/"},
Form:   url.Values{},
}

myHandler(recorder, req);

fmt.Printf("Http Status: %v\n", recorder.Code)
println(recorder.Body.String())

}

if you want to execute it: http://play.golang.org/p/dIasXkJR-c

Fábio Gomes

unread,
Apr 2, 2013, 5:42:57 PM4/2/13
to Kyle Lemons, golang-nuts
Also, I've found a very good example here: http://play.golang.org/p/GU2p5deAAk

Most of the code I wrote was based on this example, in my example you can remove all the values from the http request and it will still work, like this:

req := &http.Request{}

Yunge

unread,
Apr 3, 2013, 4:20:05 AM4/3/13
to golan...@googlegroups.com
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,
}
}
Reply all
Reply to author
Forward
0 new messages