go-http-mock

module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2023 License: Apache-2.0

README

ci

go mod version GoDoc goreportcard

GitHub license codecov GitHub latest SemVer tag) GitHub release)

for what

  • this project used to http test case mock for golang

Contributing

Contributor Covenant GitHub contributors

We welcome community contributions to this project.

Please read Contributor Guide for more information on how to get started.

请阅读有关 贡献者指南 以获取更多如何入门的信息

depends

in go mod project

# warning use private git host must set
# global set for once
# add private git host like github.com to evn GOPRIVATE
$ go env -w GOPRIVATE='github.com'
# use ssh proxy
# set ssh-key to use ssh as http
$ git config --global url."git@github.com:".insteadOf "https://github.com/"
# or use PRIVATE-TOKEN
# set PRIVATE-TOKEN as gitlab or gitea
$ git config --global http.extraheader "PRIVATE-TOKEN: {PRIVATE-TOKEN}"
# set this rep to download ssh as https use PRIVATE-TOKEN
$ git config --global url."ssh://github.com/".insteadOf "https://github.com/"

# before above global settings
# test version info
$ git ls-remote -q https://github.com/sinlov-go/go-http-mock.git

# test depends see full version
$ go list -mod readonly -v -m -versions github.com/sinlov-go/go-http-mock
# or use last version add go.mod by script
$ echo "go mod edit -require=$(go list -mod=readonly -m -versions github.com/sinlov-go/go-http-mock | awk '{print $1 "@" $NF}')"
$ echo "go mod vendor"

Features

  • support gin mock test
  • more perfect test case coverage
  • more perfect benchmark case

env

  • minimum go version: go 1.19
  • change go 1.19, ^1.19, 1.19.12-bullseye, 1.19.12 to new go version

libs

lib version
https://github.com/stretchr/testify v1.8.4
https://github.com/sebdah/goldie v2.5.3

usage

gin mock

package gin_mock_test

import (
    "github.com/gin-gonic/gin"
    "github.com/sinlov-go/go-http-mock/gin_mock"
)

var (
    basePath    = "/api/v1"
    basicRouter *gin.Engine
)

func init() {
	basicRouter = setupTestRouter()
	// bind gin router
	router(basicRouter, basePath)
}

func setupTestRouter() *gin.Engine {
	e := gin_mock.MockEngine(func(engine *gin.Engine) error {
		return nil
	})
	return e
}
  • test case example
package gin_mock_test_test

import (
	"github.com/sinlov-go/go-http-mock/gin_mock"
	"github.com/stretchr/testify/assert"
	"net/http"
	"testing"
)

func TestGetString(t *testing.T) {
	ginMock := gin_mock.NewGinMock(t, basicRouter, basePath, "/Biz/string")
	recorder := ginMock.
		Method(http.MethodGet).
		Body(nil).
		NewRecorder()
	assert.Equal(t, http.StatusOK, recorder.Code)
	assert.Equal(t, "this is Biz message", recorder.Body.String())
}
  • more example see package github.com/sinlov-go/go-http-mock/gin_mock_test

dev

# It needs to be executed after the first use or update of dependencies.
$ make init dep
  • test code
$ make test testBenchmark

add main.go file and run

# run at env dev use cmd/main.go
$ make dev
  • ci to fast check
# check style at local
$ make style

# run ci at local
$ make ci

docker

# then test build as test/Dockerfile
$ make dockerTestRestartLatest
# clean test build
$ make dockerTestPruneLatest

# more info see
$ make helpDocker

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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