This library is meant to provide convinient methods to quickly build http handlers.
go get github.com/groyoh/gonzales
See godoc reference for detailed API documentation.
Gonzales can be useful when mocking APIs in test using the httptest
package:
package gonzales_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/groyoh/gonzales"
qt "github.com/frankban/quicktest"
)
type Repository struct {
Slug string `json:"slug"`
}
type GithubClient struct {
http.Client
BaseURL string
}
func (c *GithubClient) Repositories() ([]Repository, error) {
var repos []Repository
resp, err := http.Get(c.BaseURL + "/repositories")
if err != nil {
return nil, err
}
err = json.NewDecoder(resp.Body).Decode(&repos)
return repos, err
}
func TestGithubClient_Repositories(t *testing.T) {
c := qt.New(t)
g := gonzales.Body(`[{"slug":"gonzales"}]`)
s := httptest.NewServer(g)
client := GithubClient{BaseURL: s.URL}
repos, err := client.Repositories()
c.Assert(err, qt.IsNil)
c.Assert(len(repos), qt.Equals, 1)
c.Assert(repos[0].Slug, qt.Equals, "gonzales")
}
You may also use Gonzales to build static handlers:
package main
import (
"net/http"
"github.com/groyoh/gonzales"
)
func main() {
g := gonzales.Header("Foo", "Bar").
Status(404).
Body("Not found").
MirrorHeader("Foo", "Bar")
http.Handle("/", g)
http.ListenAndServe(":8000", nil)
}
Clone this repository:
git clone https://github.com/groyoh/gonzales.git && cd gonzales
Install dependencies:
go get -u -t ./...
Run tests:
go test ./...
Lint code:
golint ./...