httphelper

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: GPL-3.0 Imports: 15 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 into HTTP request's body with JSON format;
  • Replying with an object encoded into JSON format;
  • Replying with a text message and HTTP status code;
  • Replying with an error and HTTP status code;
  • Performing a simple automated HTTP handler test using the built-in 'httptest' package;
  • Parsing the HTTP Accept header.

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"
)

Errors

View Source
const (
	ErrNotPointer = "not a pointer"
)

Errors.

View Source
const (
	ErrNullPointer = "null pointer"
)

Errors.

Variables

This section is empty.

Functions

func DeleteHTTPHeader

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

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

func FindHTTPHeader

func FindHTTPHeader(r *http.Request, headerNameAsked string) (hdr 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, returns 'nil' and the exact header name which was found.

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.

func ReplyErrorInternal

func ReplyErrorInternal(w 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(w 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(w http.ResponseWriter, replyObject interface{})

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

func ReplyJSONFast

func ReplyJSONFast(w 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(w 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