httputil

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeQuery added in v0.1.9

func DecodeQuery(dst interface{}, src map[string][]string) error

DecodeQuery decodes a map[string][]string to a struct.

The first parameter must be a pointer to a struct.

The second parameter is a map, typically url.Values from an HTTP request. Keys are "paths" in dotted notation to the struct fields and nested structs.

func EncodeQuery added in v0.1.9

func EncodeQuery(src interface{}, dst map[string][]string) error

EncodeQuery encodes a struct into map[string][]string.

Intended for use with url.Values.

func Mock

func Mock(handlers ...MockHandler) http.Handler

Mock provide a very simple way to mock a JSON HTTP handler base on path, method and header for testing. Mock use sub-router definition hence it should be used with option server.HTTPPrefixHandler. NOTICE: Don't use this function for production since it's not optimized for performance.

func Write

func Write(w http.ResponseWriter, contentType string, code int, body []byte)

Write write the status code and body on a http ResponseWriter

func WriteError

func WriteError(w http.ResponseWriter, code int, err error)

WriteError write the status code and the error in JSON format on a http ResponseWriter. For writing error as plain text or other formats, use Write.

func WriteJSON

func WriteJSON(w http.ResponseWriter, code int, data interface{})

WriteJSON write status and JSON data to http ResponseWriter.

Types

type MockHandler

type MockHandler struct {
	Request  MockRequest  `yaml:"request" json:"request"`
	Response MockResponse `yaml:"response" json:"response"`
}

MockHandler hold HTTP mock specification.

func MustReadMockFromFile

func MustReadMockFromFile(path string) []MockHandler

MustReadMockFromFile read mock specifications from JSON file. This function panics if any error.

type MockRequest

type MockRequest struct {
	// Path can be a fixed string or a pattern. See more at github.com/gorilla/mux.
	Path    string            `yaml:"path" json:"path"`
	Methods []string          `yaml:"methods" json:"methods"`
	Headers map[string]string `yaml:"headers" json:"headers"`
}

MockRequest hold HTTP mocks request specification.

type MockResponse

type MockResponse struct {
	Status  int               `yaml:"status" json:"status"`
	Headers map[string]string `yaml:"headers" json:"headers"`
	// Body can be a file using prefix file://path.
	Body interface{} `yaml:"body" json:"body"`
}

MockResponse hold HTTP mocks response specification.

type QueryDecoder added in v0.1.9

type QueryDecoder = schema.Decoder

QueryDecoder is a query param decoder.

func NewQueryDecoder added in v0.1.9

func NewQueryDecoder(tag string) *QueryDecoder

NewQueryDecoder return new query decoder using the given tag.

type QueryEncoder added in v0.1.9

type QueryEncoder = schema.Encoder

QueryEncoder is a query param encoder.

func NewQueryEncoder added in v0.1.9

func NewQueryEncoder(tag string) *QueryEncoder

NewQueryEncoder return new query encoder using the given tag.

Jump to

Keyboard shortcuts

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