mock

package
v0.0.0-...-9bd47a8 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustMarshal

func MustMarshal(v interface{}) []byte

MustMarshal helper function that wraps json.Marshal

func NewMockedHTTPClient

func NewMockedHTTPClient(options ...MockBackendOption) *http.Client

NewMockedHTTPClient creates and configures an http.Client that points to a mocked GitHub's backend API.

Example:

mockedHTTPClient := NewMockedHTTPClient(

WithRequestMatch(
	GetUsersByUsername,
	github.User{
		Name: github.String("foobar"),
	},
),
WithRequestMatch(
	GetUsersOrgsByUsername,
	[]github.Organization{
		{
			Name: github.String("foobar123thisorgwasmocked"),
		},
	},
),
WithRequestMatchHandler(
	GetOrgsProjectsByOrg,
	func(w http.ResponseWriter, _ *http.Request) {
		w.Write(MustMarshal([]github.Project{
			{
				Name: github.String("mocked-proj-1"),
			},
			{
				Name: github.String("mocked-proj-2"),
			},
		}))
	},
),

)

c := github.NewClient(mockedHTTPClient)

func WriteError

func WriteError(
	w http.ResponseWriter,
	httpStatus int,
	msg string,
)

WriteError helper function to write errors to HTTP handlers

Types

type EndpointPattern

type EndpointPattern struct {
	Pattern string // eg. "/repos/{owner}/{repo}/actions/artifacts"
	Method  string // "GET", "POST", "PATCH", etc
}

EndpointPattern models the GitHub's API endpoints

var PostIssue EndpointPattern = EndpointPattern{
	Pattern: "/rest/api/2/issue",
	Method:  "POST",
}

type EnforceHostRoundTripper

type EnforceHostRoundTripper struct {
	Host                 string
	UpstreamRoundTripper http.RoundTripper
}

EnforceHostRoundTripper rewrites all requests with the given `Host`.

func (*EnforceHostRoundTripper) RoundTrip

func (efrt *EnforceHostRoundTripper) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip implementation of `http.RoundTripper`

type FIFOReponseHandler

type FIFOReponseHandler struct {
	Responses    [][]byte
	CurrentIndex int
	// contains filtered or unexported fields
}

FIFOReponseHandler handler implementation that responds to the HTTP requests following a FIFO approach.

Once all available `Responses` have been used, this handler will panic()!

func (*FIFOReponseHandler) ServeHTTP

func (srh *FIFOReponseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implementation of `http.Handler`

type MockBackendOption

type MockBackendOption func(*mux.Router)

MockBackendOption is used to configure the *mux.router for the mocked backend

func WithRequestMatch

func WithRequestMatch(
	ep EndpointPattern,
	responsesFIFO ...interface{},
) MockBackendOption

WithRequestMatch implements a simple FIFO for requests of the given `pattern`.

Once all responses have been used, it shall panic()!

Example:

WithRequestMatch(
	GetUsersByUsername,
	github.User{
		Name: github.String("foobar"),
	},
)

func WithRequestMatchHandler

func WithRequestMatchHandler(
	ep EndpointPattern,
	handler http.Handler,
) MockBackendOption

WithRequestMatchHandler implements a request callback for the given `pattern`.

For custom implementations, this handler usage is encouraged.

Example:

WithRequestMatchHandler(
	GetOrgsProjectsByOrg,
	func(w http.ResponseWriter, _ *http.Request) {
		w.Write(MustMarshal([]github.Project{
			{
				Name: github.String("mocked-proj-1"),
			},
			{
				Name: github.String("mocked-proj-2"),
			},
		}))
	},
)

Jump to

Keyboard shortcuts

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