tests

package module
v0.0.0-...-a20e858 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2016 License: MIT Imports: 7 Imported by: 0

README

tests GoDoc Build Status Go Report Card Coverage Status

a test package for testing you web server, only support fasthttp and similar server, such as Gem.

Install

go get github.com/go-gem/tests

Example

import (
	"testing"

	"github.com/go-gem/tests"
	"github.com/valyala/fasthttp"
)

func TestFastHTTP(t *testing.T) {
	contentType := "text/html; charset=utf-8"
	statusCode := fasthttp.StatusBadRequest
	respBody := fasthttp.StatusMessage(fasthttp.StatusBadRequest)

	// Fake server
	srv := &fasthttp.Server{
		Handler: func(ctx *fasthttp.RequestCtx) {
			ctx.SetContentType(contentType)
			ctx.SetStatusCode(statusCode)
			ctx.SetBodyString(respBody)
		},
	}

	// Create a Test instance.
	test := tests.New(srv)

	// Customize request.
	// See Test struct.
	test.Url = "/"

	// Add excepted result.
	test.Expect().
		Status(statusCode).
		Header("Content-Type", contentType).
		Body(respBody)

	// Custom checking function.
	test.Expect().Custom(func(resp fasthttp.Response) error {
		// check response.

		return nil
	})

	// Run test.
	if err := test.Run(); err != nil {
		t.Error(err)
	}
}

Documentation

Overview

Package tests provides simple APIs to test you web server, only support servers which based fasthttp.

Example

import (
	"testing"

	"github.com/go-gem/tests"
	"github.com/valyala/fasthttp"
)

func TestFastHTTP(t *testing.T) {
	contentType := "text/html; charset=utf-8"
	statusCode := fasthttp.StatusBadRequest
	respBody := fasthttp.StatusMessage(fasthttp.StatusBadRequest)

	// Fake server
	srv := &fasthttp.Server{
		Handler: func(ctx *fasthttp.RequestCtx) {
			ctx.SetContentType(contentType)
			ctx.SetStatusCode(statusCode)
			ctx.SetBodyString(respBody)
		},
	}

	// Create a Test instance.
	test := tests.New(srv)

	// Customize request.
	// See Test struct.
	test.Url = "/"

	// Add excepted result.
	test.Expect().
		Status(statusCode).
		Header("Content-Type", contentType).
		Body(respBody)

	// Custom checking function.
	test.Expect().Custom(func(resp fasthttp.Response) error {
		// check response.

		return nil
	})

	// Run test.
	if err := test.Run(); err != nil {
		t.Error(err)
	}
}

Index

Constants

This section is empty.

Variables

View Source
var (

	// DefaultTimeout
	DefaultTimeout = 200 * time.Microsecond
)

Functions

This section is empty.

Types

type Expect

type Expect []Func

Expect a slice of checking functions.

func (*Expect) Body

func (e *Expect) Body(body string) *Expect

Body add expected response body.

func (*Expect) Custom

func (e *Expect) Custom(f Func) *Expect

Custom add custom function.

func (*Expect) Header

func (e *Expect) Header(key, value string) *Expect

Header add expected header.

func (*Expect) Rest

func (e *Expect) Rest() *Expect

Rest reset expected result.

func (*Expect) Status

func (e *Expect) Status(code int) *Expect

Status add expected status code.

type Func

type Func func(resp fasthttp.Response) error

Func customize function to check response.

type Test

type Test struct {
	Timeout time.Duration

	// Request configuration
	Url      string
	Method   string
	Protocol string
	Headers  map[string]string
	Payload  string
	// contains filtered or unexported fields
}

Test struct

func New

func New(server server, args ...string) *Test

New returns a Test instance with default configuration.

func (*Test) Expect

func (t *Test) Expect() *Expect

Expect return expect object.

func (*Test) Run

func (t *Test) Run() (err error)

Run run test and return an error, return nil if everything is ok.

Jump to

Keyboard shortcuts

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