hhth

package module
v0.0.0-...-08d6f0e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2015 License: MIT Imports: 6 Imported by: 0

README

hhth

Circle CI Coverage Status

hhth is httpHandler test helper library of the golang.

Install

go get github.com/kyokomi/hhth

Example

Handler
package example

import "net/http"

func init() {
	http.HandleFunc("/hoge", hogeHandler)
	http.HandleFunc("/hoge.json", hogeJSONHandler)
}

func hogeHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed)))
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Header().Add("Content-Type", "text/plain; charset=utf-8")
	w.Write([]byte("hoge"))
}

func hogeJSONHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		w.Write([]byte(http.StatusText(http.StatusMethodNotAllowed)))
		return
	}

	w.WriteHeader(http.StatusOK)
	w.Header().Add("Content-Type", "application/json; charset=UTF-8")
	w.Write([]byte(`{"name": "hoge", "age": 20}`))
}
Get Test
package example_test

import (
	"net/http"
	"testing"

	"github.com/kyokomi/hhth"
)

func TestHogeHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)
	hhtHelper.SetTestCase(
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("text/plain; charset=utf-8"),
	)
	
	resp := hhtHelper.Get("/hoge")
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	if resp.String() != "hoge" {
		t.Errorf("error response body hoge != %s", resp.String())
	}
}
JSON Parse
package example_test

import (
	"net/http"
	"testing"

	"github.com/kyokomi/hhth"
)

func TestJSONParse(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)
	hhtHelper.SetTestCase(
		hhth.TestCaseStatusCode(http.StatusOK),
		hhth.TestCaseContentType("application/json; charset=UTF-8"),
	)
	
	var resp map[string]interface{}
	if err := hhtHelper.Get("/hoge.json").JSON(&resp); err != nil {
		t.Errorf("error %s", err)
	}

	if resp["name"].(string) != "hoge" {
		t.Errorf("error json response name != %s", resp["name"])
	}

	if resp["age"].(float64) != 20 {
		t.Errorf("error json response age != %s", resp["age"])
	}
}
Custom TestCase
package example_test

import (
    "fmt"
	"net/http"
	"testing"

	"github.com/kyokomi/hhth"
)

func TestOptionsHogeHandler(t *testing.T) {
	hhtHelper := hhth.New(http.DefaultServeMux)
	hhtHelper.SetTestCase(
		hhth.TestCaseStatusCode(http.StatusNoContent),
		hhth.HandlerTestCaseFunc(func(resp hhth.Response) error {
			r, _ := resp.Result()
			if r.Header().Get("Allow") != "GET,HEAD,PUT,POST,DELETE" {
				return fmt.Errorf("allow header error %s", r.Header().Get("Allow"))
			}
			return nil
		}),
	)

	resp := hhtHelper.Options("/hoge")
	if resp.Error() != nil {
		t.Errorf("error %s", resp.Error())
	}
	fmt.Println(resp.String())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPHandlerTestHelper

type HTTPHandlerTestHelper interface {
	SetHeader(key, value string)
	SetForm(key, value string)

	AddTestCase(testCases ...HandlerTestCase)
	SetTestCase(testCases ...HandlerTestCase)
	AddTestCaseFunc(testCases ...HandlerTestCaseFunc)
	SetTestCaseFunc(testCaseFunc ...HandlerTestCaseFunc)

	// method
	Get(urlStr string, testCases ...HandlerTestCaseFunc) Response
	Head(urlStr string, testCases ...HandlerTestCaseFunc) Response
	Delete(urlStr string, testCases ...HandlerTestCaseFunc) Response
	Options(urlStr string, testCases ...HandlerTestCaseFunc) Response
	Put(urlStr string, bodyType string, body io.Reader, testCases ...HandlerTestCaseFunc) Response
	Post(urlStr string, bodyType string, body io.Reader, testCases ...HandlerTestCaseFunc) Response
}

func New

func New(handler http.Handler) HTTPHandlerTestHelper

type HandlerTestCase

type HandlerTestCase interface {
	Execute(resp Response) error
}

func TestCaseContentLength

func TestCaseContentLength(contentLength int) HandlerTestCase

func TestCaseContentType

func TestCaseContentType(contentType string) HandlerTestCase

func TestCaseStatusCode

func TestCaseStatusCode(statusCode int) HandlerTestCase

type HandlerTestCaseFunc

type HandlerTestCaseFunc func(resp Response) error

func (HandlerTestCaseFunc) Execute

func (f HandlerTestCaseFunc) Execute(resp Response) error

Execute calls f(resp) error.

type Response

type Response interface {
	Error() error
	String() string
	Result() (*httptest.ResponseRecorder, error)
	JSON(v interface{}) error
}

func NewErrorResponse

func NewErrorResponse(err error) Response

func NewResponse

func NewResponse(resp *httptest.ResponseRecorder) Response

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL