testsvr

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

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

Go to latest
Published: Oct 12, 2020 License: MIT Imports: 2 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

testsvr

Build Status GitHub license Doc Go Report Card

Description

Make httptest generated test server aware of -v of go test, and reuse that code for stand alone mock server

Why created

Interacting with external services becomes almost an requirement for modern Web service development. Golang has an awesome httptest standard library making it really easy to start and close mock server in test code. However, this mock server, which is supposed to be a part of test code, is not aware of -v of go test, and this leads to verbose output if you put logging in mock server handlers. This tiny little library makes httptest generated mock server aware of -v, keeping your test output sane and clean by default, detailed and comprehensive if needed.

Installation

go get -u github.com/achiku/testsvr

Example

Full example is in https://github.com/achiku/testsvr/tree/master/example

mock server code
package example

import (
	"fmt"
	"net/http"

	"github.com/achiku/testsvr"
)

// DefaultHandlerMap default url and handler map
var DefaultHandlerMap = map[string]testsvr.CreateHandler{
	"/hello":   hello,
	"/goodbye": goodbye,
}

func hello(logger testsvr.Logger) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		logger.Logf("RawURL: %s", r.URL)
		logger.Logf("Header: %s", r.Header)
		name := r.URL.Query().Get("name")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "hello! %s.", name)
	}
}

func goodbye(logger testsvr.Logger) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		logger.Logf("RawURL: %s", r.URL)
		logger.Logf("Header: %s", r.Header)
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "goodbye!")
	}
}
client test code
func TestClientHello(t *testing.T) {
	s := httptest.NewServer(testsvr.NewMux(DefaultHandlerMap, t))
	defer s.Close()

	testData := []struct {
		name   string
		status int
	}{
		{"moqada", http.StatusOK},
		{"8maki", http.StatusOK},
		{"achiku", http.StatusOK},
	}
	c := NewClient(s.URL)
	for _, d := range testData {
		status, resp, err := c.Hello(d.name)
		if err != nil {
			t.Fatal(err)
		}
		if status != d.status {
			t.Errorf("want %d got %d", http.StatusOK, status)
		}
		expResp := fmt.Sprintf("hello! %s.", d.name)
		if resp != expResp {
			t.Errorf("want %s got %s", expResp, resp)
		}
	}
}

When you don't want verbose output.

$ go test
PASS
ok      github.com/achiku/testsvr/example       0.016s

When you want detailed output.

$ go test -v
=== RUN   TestNewClient
--- PASS: TestNewClient (0.00s)
=== RUN   TestClientHello
--- PASS: TestClientHello (0.00s)
        mock_server.go:18: RawURL: /hello?name=moqada
        mock_server.go:19: Header: map[User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]
        mock_server.go:18: RawURL: /hello?name=8maki
        mock_server.go:19: Header: map[User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]
        mock_server.go:18: RawURL: /hello?name=achiku
        mock_server.go:19: Header: map[User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]
=== RUN   TestClientHelloError
--- PASS: TestClientHelloError (0.00s)
        client_test.go:51: something went wrong
        client_test.go:58: something went wrong
PASS
ok      github.com/achiku/testsvr/example       0.018s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMux

func NewMux(hm URLHandlerMap, l Logger) *http.ServeMux

NewMux creates mux for test/dev server

func NewServer

func NewServer(hm URLHandlerMap, port string) *http.Server

NewServer creates dev server

Types

type CreateHandler

type CreateHandler func(Logger) http.HandlerFunc

CreateHandler creates handlers for test/dev server

type Logger

type Logger interface {
	Logf(format string, args ...interface{})
	Log(args ...interface{})
}

Logger inerface Logf

type URLHandlerMap

type URLHandlerMap map[string]CreateHandler

URLHandlerMap url and handler

Directories

Path Synopsis
cmd/devserver command

Jump to

Keyboard shortcuts

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