Skip to content

Instantly share code, notes, and snippets.

@laouji
Last active August 29, 2015 14:25
Show Gist options
  • Save laouji/f2b77f7a9fa1ab107210 to your computer and use it in GitHub Desktop.
Save laouji/f2b77f7a9fa1ab107210 to your computer and use it in GitHub Desktop.
goji httptest example
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/zenazn/goji/web"
)
type SubtestFunc func(*httptest.Server)
func Subtest(t *testing.T, f SubtestFunc) {
mux := web.New()
mux.Post("/", index)
srv := httptest.NewServer(mux)
defer srv.Close()
//run test
f(srv)
}
type RootIndexResponseJSON struct {
Text string `json:"text"`
}
func TestRoot(t *testing.T) {
Subtest(t, func(srv *httptest.Server) {
formData := url.Values{}
formData.Set("token", "dummy")
res, err := http.PostForm(srv.URL + "/", formData)
if err != nil {
t.Fatalf("got error: %v", err)
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var responseJSON RootIndexResponseJSON
decoder.Decode(&responseJSON)
if (len([]rune(responseJSON.Text)) == 0) {
t.Fatalf("expected 'text' parameter with length greater than 0: got '%v'", responseJSON.Text)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment