httphelper

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: GPL-3.0 Imports: 14 Imported by: 3

README

HTTP Helper

This package helps in processing and testing HTTP requests.

This package contains various useful functions which help in processing HTTP requests.

  • Receiving an object encoded with JSON format from HTTP request;
  • Replying with an object encoded with JSON format;
  • Replying with a text message with HTTP status code;
  • Replying with an error with HTTP status code;
  • Performing a simple automated HTTP handler test using the built-in test package;
  • Reading a single-value HTTP header;
  • Reading a non-duplicate HTTP "cookie";
  • Parsing the HTTP Accept header.
  • Checking browser (client) support for JSON.

Documentation

Index

Constants

View Source
const (
	ErrNoRecords           = "no records"
	ErrSyntaxErrorInRecord = "syntax error in record: %s"
	ErrSyntaxErrorInWeight = "syntax error in weight: %s"
	ErrEndOfList           = "EOL"
)
View Source
const (
	ErrHttpHeaderNameIsNotSet   = "HTTP header name is not set"
	ErrHttpHeaderNameIsNotFound = "HTTP header name is not found"
	ErrFMultipleHttpHeaders     = "multiple HTTP headers: %s"
	ErrFHttpHeaderIsNotFound    = "HTTP header is not found: %s"
)

Errors

View Source
const (
	ErrFDuplicateCookie = "duplicate cookie: %s"
)
View Source
const (
	ErrNullPointer = "null pointer"
)

Errors.

Variables

This section is empty.

Functions

func CheckBrowserSupportForJson added in v0.22.0

func CheckBrowserSupportForJson(req *http.Request) (ok bool, err error)

func DeleteHttpHeader added in v0.22.0

func DeleteHttpHeader(r *http.Request, headerNameToDelete string) (err error)

DeleteHttpHeader function tries to delete a header from the HTTP request.

func FindHttpHeader added in v0.22.0

func FindHttpHeader(req *http.Request, headerNameAsked string) (hdrNm string, err error)

FindHttpHeader function tries to find the HTTP header with name similar to the specified one. According to the Section 4.2 of RFC 2616, HTTP header names are case-insensitive. On success, it returns the exact header name which was found.

Notes. This function is useful for inspection of HTTP requests having incorrect HTTP headers. It tries to show an actual name of the incorrectly set header. For example, if a request has a header named 'CONtent-TyPe', this function will search for headers similar to 'Content-Type' and find the modified variant of it and return it as it is, i.e. as 'CONtent-TyPe'. Most users do not need this function.

func GetCookieByName added in v0.22.0

func GetCookieByName(req *http.Request, cookieName string) (cookie *http.Cookie, err error)

GetCookieByName reads a non-duplicate cookie. If a cookie is not found, null is returned.

func GetSingleHttpHeader added in v0.22.0

func GetSingleHttpHeader(req *http.Request, headerName string) (h string, err error)

GetSingleHttpHeader reads exactly one, single HTTP header. If multiple headers are found, an error is returned. Unfortunately, Go language does not do this by default and returns only a first header value even when there are multiple values available. Such a behaviour may lead to unexpected errors.

func ParseRecordWeight added in v0.16.0

func ParseRecordWeight(rwt string) (weight float32, err error)

func PerformAverageHttpTest

func PerformAverageHttpTest(test *AverageTest) (err error)

PerformAverageHttpTest function performs the simulation of an average HTTP test handler. It writes the received results into the 'ResultReceived' field of a test object.

func PerformSimpleHttpTest

func PerformSimpleHttpTest(test *SimpleTest) (err error)

PerformSimpleHttpTest function performs the simulation of a simple HTTP test handler. It writes the received results into the 'ResultReceived' field of a test object.

func ReceiveJSON

func ReceiveJSON(r *http.Request, receiver interface{}) (err error)

ReceiveJSON function receives an object encoded with JSON format from the HTTP request's body. The receiver must be a pointer.

func ReplyErrorInternal

func ReplyErrorInternal(rw http.ResponseWriter, err error)

ReplyErrorInternal function replies to the HTTP request with an error and 'Internal Server Error' HTTP status code.

func ReplyErrorWithCode

func ReplyErrorWithCode(rw http.ResponseWriter, httpStatusCode int, err error)

ReplyErrorWithCode function replies to the HTTP request with an error and the specified HTTP status code.

func ReplyJSON

func ReplyJSON(rw http.ResponseWriter, replyObject interface{})

ReplyJSON function sends an object in JSON format to the HTTP output stream.

func ReplyJSONFast

func ReplyJSONFast(rw http.ResponseWriter, replyObject interface{})

ReplyJSONFast function sends an object in JSON format to the HTTP output stream using the faster but less secure way than an ordinary 'ReplyJSON' method.

func ReplyTextWithCode

func ReplyTextWithCode(rw http.ResponseWriter, httpStatusCode int, replyText string)

ReplyTextWithCode function replies to the HTTP request with the specified text and HTTP status code.

Types

type AcceptedMimeType added in v0.16.0

type AcceptedMimeType struct {
	MimeType string
	Weight   float32
}

func ParseRecord added in v0.16.0

func ParseRecord(rec string) (amt *AcceptedMimeType, err error)

type AcceptedMimeTypes added in v0.16.0

type AcceptedMimeTypes struct {
	// contains filtered or unexported fields
}

AcceptedMimeTypes provides convenient access to a list of MIME types received from the 'Accept' HTTP header. The struct has a simple iterator which allows to list values with the 'Next' method. The first returned value is the most acceptable, i.e. a value with the greatest weight. The last returned value is the least acceptable, i.e. a value with the lowest weight. Please, note that the iterator is not protected by a mutex against simultaneous access for performance reasons.

func NewAcceptedMimeTypesFromHeader added in v0.16.0

func NewAcceptedMimeTypesFromHeader(hdr string) (amts *AcceptedMimeTypes, err error)

NewAcceptedMimeTypesFromHeader parses the provided 'Accept' HTTP header and returns an AcceptedMimeType object.

func (*AcceptedMimeTypes) Next added in v0.16.0

func (amts *AcceptedMimeTypes) Next() (amt *AcceptedMimeType, err error)

Next gets the next MIME type in the list. If no more items are available, an error is returned.

func (*AcceptedMimeTypes) Reset added in v0.16.0

func (amts *AcceptedMimeTypes) Reset()

Reset resets the iterator. Normally, such a method is not used in iterable objects, but since the Go programming language is very unusual and unique, for those who for some reason want to iterate values several times, this method may be useful.

type AverageTest

type AverageTest struct {
	Parameter      AverageTestParameter
	ResultExpected AverageTestResult
	ResultReceived AverageTestResult
}

AverageTest is an average HTTP test.

type AverageTestParameter

type AverageTestParameter struct {
	RequestMethod  string
	RequestUrl     string
	RequestHeaders http.Header
	RequestBody    io.Reader
	RequestHandler http.HandlerFunc
}

AverageTestParameter is a parameter of an average HTTP test.

type AverageTestResult

type AverageTestResult struct {
	ResponseStatusCode int
	ResponseHeaders    http.Header
	ResponseBody       []byte
}

AverageTestResult is a result of an average HTTP test.

type SimpleTest

type SimpleTest struct {
	Parameter      SimpleTestParameter
	ResultExpected SimpleTestResult
	ResultReceived SimpleTestResult
}

SimpleTest is a simple HTTP test.

type SimpleTestParameter

type SimpleTestParameter struct {
	RequestMethod  string
	RequestUrl     string
	RequestBody    io.Reader
	RequestHandler http.HandlerFunc
}

SimpleTestParameter is a parameter of a simple HTTP test.

type SimpleTestResult

type SimpleTestResult struct {
	ResponseStatusCode int
	ResponseBodyString string
}

SimpleTestResult is a result of a simple HTTP test.

Jump to

Keyboard shortcuts

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