http

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	InvalidPath    = "[CONF:apiKey.invalid_apiKey]"
	NilString      = "%nil%"
	Slash          = "/"
	DefaultTestURL = "https://jsonplaceholder.typicode.com/"
)

Variables

View Source
var AuthHeaders = map[string]string{
	"X-API-KEY":     "apikey",
	"Authorization": "jwt",
}

Functions

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.

Types

type ContextKey

type ContextKey string

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

type Logger

type Logger struct {
	Log *golium.Logger
}

Logger logs 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 (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 RequestParams added in v0.18.0

type RequestParams struct {
	Method     string
	URL        string
	Endpoint   string
	Path       string
	APIKey     string
	BodyParams schema.Params
	Table      *godog.Table
}

type Session

type Session struct {
	Request            model.Request
	Response           model.Response
	NoRedirect         bool
	InsecureSkipVerify 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) 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) ConfigureInsecureSkipVerify added in v0.16.1

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

ConfigureInsecureSkipVerify configures insecure skip verify for the HTTP client in HTTPS calls.

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(httpPath 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(params map[string][]string)

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

func (*Session) ConfigureRequestBody added in v0.17.0

func (s *Session) ConfigureRequestBody(ctx context.Context, message interface{})

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

func (*Session) ConfigureRequestBodyJSONFile

func (s *Session) ConfigureRequestBodyJSONFile(
	ctx context.Context,
	bodyParams schema.Params,
) error

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

func (*Session) ConfigureRequestBodyJSONFileWithout

func (s *Session) ConfigureRequestBodyJSONFileWithout(
	ctx context.Context,
	bodyParams schema.Params,
	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) 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) GetURL added in v0.17.0

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

GetURL returns URL from Configuration or Context

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) SendRequest added in v0.17.0

func (s *Session) SendRequest(
	ctx context.Context,
	uRL, method, endpoint, apiKey string,
) error

SendRequestWithoutBackslash send request without backslash.

func (*Session) SendRequestWithBody added in v0.17.0

func (s *Session) SendRequestWithBody(
	ctx context.Context,
	uRL, method, endpoint string, bodyParams schema.Params, apiKey string,
) error

SendRequestWithBody send request using body from JSON file located in schemas.

func (*Session) SendRequestWithBodyModifyingFields added in v0.17.0

func (s *Session) SendRequestWithBodyModifyingFields(
	ctx context.Context,
	uRL, method, endpoint string, bodyParams schema.Params, apiKey string, t *godog.Table,
) error

SendRequestWithBodyModifyingFields send request using body from JSON file located in schemas modifying fields.

func (*Session) SendRequestWithBodyWithoutFields added in v0.17.0

func (s *Session) SendRequestWithBodyWithoutFields(
	ctx context.Context,
	uRL, method, endpoint string, bodyParams schema.Params, apiKey string, t *godog.Table,
) error

SendRequestWithBodyWithoutFields send request using body from JSON file located in schemas without fields.

func (*Session) SendRequestWithFilters added in v0.17.0

func (s *Session) SendRequestWithFilters(
	ctx context.Context,
	uRL, method, endpoint, apiKey, filters string,
) error

SendRequestWithFilters send request using filters with query params.

func (*Session) SendRequestWithMultipartBody added in v0.18.0

func (s *Session) SendRequestWithMultipartBody(
	ctx context.Context,
	reqParams RequestParams, fileField, file string,
) error

SendRequestWithMultipartBody send request using multipart body.

func (*Session) SendRequestWithPath added in v0.17.0

func (s *Session) SendRequestWithPath(
	ctx context.Context,
	uRL, method, endpoint, requestPath, apiKey string,
) error

SendRequestWithPath send request with path.

func (*Session) SendRequestWithPathAndBody added in v0.17.0

func (s *Session) SendRequestWithPathAndBody(
	ctx context.Context,
	uRL, method, endpoint, requestPath string, bodyParams schema.Params, apiKey string,
) error

SendRequestWithPathAndBody send request with path and JSON body.

func (*Session) SendRequestWithQueryParams added in v0.17.0

func (s *Session) SendRequestWithQueryParams(
	ctx context.Context,
	uRL, method, endpoint, apiKey string,
	t *godog.Table,
) error

SendRequestWithQueryParams send request using with query params.

func (*Session) SendRequestWithoutBackslash added in v0.17.0

func (s *Session) SendRequestWithoutBackslash(
	ctx context.Context,
	uRL, method, endpoint, apiKey string,
) error

SendRequestWithoutBackslash send request without backslash.

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, 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, 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,
	responseParams schema.Params,
	respDataLocation string) error

ValidateResponseBodyJSONFile validates the response body against the JSON in File.

func (*Session) ValidateResponseBodyJSONFileModifying added in v0.17.0

func (s *Session) ValidateResponseBodyJSONFileModifying(
	ctx context.Context, responseParams schema.Params,
	t *godog.Table,
) error

ValidateResponseBodyJSONFileModifying validates the response body against the JSON in File modifying params.

func (*Session) ValidateResponseBodyJSONFileWithout

func (s *Session) ValidateResponseBodyJSONFileWithout(
	ctx context.Context,
	responseParams schema.Params,
	respDataLocation string, t *godog.Table) 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, schemaName 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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