Skip to content

groyoh/gonzales

Repository files navigation

Gonzales

Build Status GoDoc Coverage Status Go Report Card License

This library is meant to provide convinient methods to quickly build http handlers.

Installation

go get github.com/groyoh/gonzales

API

See godoc reference for detailed API documentation.

Examples

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

License

MIT

Development

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 ./...

About

Fastest way to build Go HTTP handlers 🐁

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages