Skip to content

fasthttp mock by match request and return response

License

Notifications You must be signed in to change notification settings

gopereza/fasthttpmock

Repository files navigation

fasthttp mock Build Status

Dep

dep ensure -add github.com/gopereza/fasthttpmock

Example

package main

import (
	"github.com/gopereza/fasthttpmock"
	"github.com/valyala/fasthttp"
)

func main() {
	pairs := fasthttpmock.NewRequestResponsePairs()

	{
		request := &fasthttp.Request{}
		request.Header.SetMethod("GET")
		request.SetRequestURI("http://example.com/test-1")

		response := &fasthttp.Response{}
		response.SetStatusCode(fasthttp.StatusOK)
		response.SetBodyString(`{"code":"1"}`)

		pairs.Add(request, response)
	}

	client := fasthttpmock.NewClient(pairs, fasthttpmock.Equal, fasthttpmock.Copy)

	request := fasthttp.AcquireRequest()
	request.Header.SetMethod("GET")
	request.SetRequestURI("http://example.com/test-1")
	response := fasthttp.AcquireResponse()

	_ = client.Do(request, response)
}

Usage

package main

import (
	"github.com/gopereza/fasthttpmock"
	"github.com/valyala/fasthttp"
)

func main() {
	fastClient := &fasthttp.Client{}

	client := fasthttpmock.NewWrapClient(fastClient)

	request := fasthttp.AcquireRequest()
	response := &fasthttp.Response{}
	_ = client.Do(request, response)

	mockClient := fasthttpmock.NewClient(fasthttpmock.NewRequestResponsePairs(), fasthttpmock.Equal, fasthttpmock.Copy)

	// switch to mock usage
	client.SetMockClient(mockClient)

	// switch to real usage
	client.SetMockClient(nil)
}

About

fasthttp mock by match request and return response

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages