http

package
v0.15.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindValueByCode

func FindValueByCode(dataStruct []map[string]interface{}, code string, param string) (interface{}, error)

FindValueByCode Find value by code and param from dataStruct

func GetParamFromJSON

func GetParamFromJSON(ctx context.Context, file, code, param string) (interface{}, error)

GetParamFromJSON Retrieve values from JSON structure file assets

func InitializeContext

func InitializeContext(ctx context.Context) context.Context

InitializeContext adds the HTTP session to the context. The new context is returned because context is immutable.

func JSONEquals

func JSONEquals(expected interface{}, current interface{}) bool

JSONEquals Check if unmarshalled JSON maps are equal

func LoadJSONData

func LoadJSONData(file string) ([]byte, error)

LoadJSONData Load file contents into bytes

func UnmarshalJSONData

func UnmarshalJSONData(data []byte) ([]map[string]interface{}, error)

UnmarshalJSONData Unmarshal bytes to json map struct

Types

type ContextKey

type ContextKey string

ContextKey defines a type to store the HTTP session in context.Context.

type Logger

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

Logger logs the HTTP request and response in a configurable file.

func GetLogger

func GetLogger() *Logger

GetLogger returns the logger for HTTP requests and responses. If the logger is not created yet, it creates a new instance of Logger.

func NewLogger

func NewLogger(path string) (*Logger, error)

NewLogger creates an instance of the logger. It configures the file path where the HTTP request and response are written.

func (Logger) LogRequest

func (l Logger) LogRequest(req *http.Request, body []byte, corr string)

LogRequest logs an HTTP request in the configured log file.

func (Logger) LogResponse

func (l Logger) LogResponse(resp *http.Response, body []byte, corr string)

LogResponse logs an HTTP response in the configured log file.

func (Logger) LogTimeout

func (l Logger) LogTimeout(corr string)

LogTimeout logs an HTTP response with timeout in the configured log file.

type Request

type Request struct {
	// Endpoint of the HTTP server. It might include a base path.
	Endpoint string
	// Path of the API endpoint. This path is considered with the endpoint to invoke the HTTP server.
	Path string
	// Query parameters
	QueryParams map[string][]string
	// Request headers
	Headers map[string][]string
	// HTTP method
	Method string
	// Request body as slice of bytes
	RequestBody []byte
	// Username for basic authentication
	Username string
	// Password for basic authentication
	Password string
}

Request information of the Session.

func (*Request) AddJSONHeaders

func (r *Request) AddJSONHeaders()

AddJSONHeaders adds json headers to Request if they are null

type Response

type Response struct {
	// HTTP response
	Response *http.Response
	// Response body as slice of bytes
	ResponseBody []byte
}

Response information of the session.

type Session

type Session struct {
	Request    Request
	Response   Response
	NoRedirect bool
	Timeout    time.Duration
	Timedout   bool
}

Session contains the information of a HTTP session (request and response).

func GetSession

func GetSession(ctx context.Context) *Session

GetSession returns the HTTP session stored in context. Note that the context should be previously initialized with InitializeContext function.

func (*Session) AddToRequestMessageFromJSONFile

func (s *Session) AddToRequestMessageFromJSONFile(message interface{})

AddToRequestMessageFromJSONFile adds to Request Body the message from JSON file

func (*Session) ConfigureCredentials

func (s *Session) ConfigureCredentials(ctx context.Context, username, password string)

func (*Session) ConfigureEndpoint

func (s *Session) ConfigureEndpoint(ctx context.Context, endpoint string)

ConfigureEndpoint configures the HTTP endpoint.

func (*Session) ConfigureHeaders

func (s *Session) ConfigureHeaders(ctx context.Context, headers map[string][]string)

ConfigureHeaders stores a table of HTTP headers in the application context.

func (*Session) ConfigureNoRedirection

func (s *Session) ConfigureNoRedirection(ctx context.Context)

ConfigureNoRedirection configures no redirection for the HTTP client.

func (*Session) ConfigurePath

func (s *Session) ConfigurePath(ctx context.Context, path string)

ConfigurePath configures the path of the HTTP endpoint. It configures a resource path in the application context. The API endpoint and the resource path are composed when invoking the HTTP server.

func (*Session) ConfigureQueryParams

func (s *Session) ConfigureQueryParams(ctx context.Context, params map[string][]string)

ConfigureQueryParams stores a table of query parameters in the application context.

func (*Session) ConfigureRequestBodyJSONFile

func (s *Session) ConfigureRequestBodyJSONFile(ctx context.Context, code, file string) error

ConfigureRequestBodyJSONFile writes the body in the HTTP request as a JSON from file.

func (*Session) ConfigureRequestBodyJSONFileWithout

func (s *Session) ConfigureRequestBodyJSONFileWithout(ctx context.Context, code, file string, params []string) error

ConfigureRequestBodyJSONFileWithout writes the body in the HTTP request as a JSON from file without given values.

func (*Session) ConfigureRequestBodyJSONProperties

func (s *Session) ConfigureRequestBodyJSONProperties(ctx context.Context, props map[string]interface{}) error

ConfigureRequestBodyJSONProperties writes the body in the HTTP request as a JSON with properties.

func (*Session) ConfigureRequestBodyJSONText

func (s *Session) ConfigureRequestBodyJSONText(ctx context.Context, message string)

ConfigureRequestBodyJSONText // ConfigureRequestBodyJSONText writes the body in the HTTP request as a JSON from text.

func (*Session) ConfigureRequestBodyURLEncodedProperties

func (s *Session) ConfigureRequestBodyURLEncodedProperties(ctx context.Context, props map[string][]string) error

ConfigureRequestBodyURLEncodedProperties writes the body in the HTTP request as x-www-form-urlencoded with properties.

func (*Session) SendHTTPRequest

func (s *Session) SendHTTPRequest(ctx context.Context, method string) error

SendHTTPRequest sends a HTTP request using the configuration in the application context.

func (*Session) SetHTTPResponseTimeout

func (s *Session) SetHTTPResponseTimeout(ctx context.Context, timeout int)

SetHTTPResponseTimeout configures a response timeout in milliseconds.

func (*Session) StoreResponseBodyJSONPropertyInContext

func (s *Session) StoreResponseBodyJSONPropertyInContext(ctx context.Context, key string, ctxtKey string) error

StoreResponseBodyJSONPropertyInContext extracts a JSON property from the HTTP response body and stores it in the context.

func (*Session) StoreResponseHeaderInContext

func (s *Session) StoreResponseHeaderInContext(ctx context.Context, header string, ctxtKey string) error

StoreResponseHeaderInContext stores in context a header of the HTTP response. If the header does not exist, the context value is empty. This method does not support multiple headers with the same name. It just stores one of them.

func (*Session) URL

func (s *Session) URL() (*url.URL, error)

URL composes the endpoint, the resource, and query parameters to build a URL.

func (*Session) ValidateNotResponseHeaders

func (s *Session) ValidateNotResponseHeaders(ctx context.Context, expectedHeaders []string) error

ValidateNotResponseHeaders checks that a set of response headers are not included in HTTP response.

func (*Session) ValidateResponseBodyEmpty

func (s *Session) ValidateResponseBodyEmpty(ctx context.Context) error

ValidateResponseBodyEmpty validates that the response body is empty. It checks the Content-Length header and the response body buffer.

func (*Session) ValidateResponseBodyJSONFile

func (s *Session) ValidateResponseBodyJSONFile(ctx context.Context, code, file, respDataLocation string) error

ValidateResponseBodyJSONFile validates the response body against the JSON in File.

func (*Session) ValidateResponseBodyJSONFileWithout

func (s *Session) ValidateResponseBodyJSONFileWithout(ctx context.Context, code, file, respDataLocation string, params []string) error

ValidateResponseBodyJSONFileWithout validates the response body against the JSON in File without params.

func (*Session) ValidateResponseBodyJSONProperties

func (s *Session) ValidateResponseBodyJSONProperties(ctx context.Context, props map[string]interface{}) error

ValidateResponseBodyJSONProperties validates a list of properties in the JSON body of the HTTP response.

func (*Session) ValidateResponseBodyJSONSchema

func (s *Session) ValidateResponseBodyJSONSchema(ctx context.Context, schema string) error

ValidateResponseBodyJSONSchema validates the response body against the JSON schema.

func (*Session) ValidateResponseBodyText

func (s *Session) ValidateResponseBodyText(ctx context.Context, expectedText string) error

ValidateResponseBodyText validates that the response body payload is the expected text.

func (*Session) ValidateResponseFromJSONFile

func (s *Session) ValidateResponseFromJSONFile(response interface{}, respDataLocation string) error

ValidateResponseFromJSONFile validates the response body against the response from JSON File.

func (*Session) ValidateResponseHeaders

func (s *Session) ValidateResponseHeaders(ctx context.Context, expectedHeaders map[string][]string) error

ValidateResponseHeaders checks a set of response headers.

func (*Session) ValidateResponseTimedout

func (s *Session) ValidateResponseTimedout(ctx context.Context) error

ValidateResponseTimedout checks if the HTTP client timed out without receiving a response.

func (*Session) ValidateStatusCode

func (s *Session) ValidateStatusCode(ctx context.Context, expectedCode int) error

ValidateStatusCode validates the status code from the HTTP response.

type Steps

type Steps struct {
}

Steps type is responsible to initialize the HTTP client steps in godog framework.

func (Steps) InitializeSteps

func (s Steps) InitializeSteps(ctx context.Context, scenCtx *godog.ScenarioContext) context.Context

InitializeSteps adds client HTTP steps to the scenario context. It implements StepsInitializer interface. It returns a new context (context is immutable) with the HTTP Context.

Jump to

Keyboard shortcuts

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