Documentation
¶
Overview ¶
Package libContext provides context initialization utilities for requestCore handlers.
Index ¶
- Constants
- func AddMiddlewareLogs(w webFramework.WebFramework, title, tag string) func(time.Time, int)
- func AddWebHandlerLogs(c any, title, tag string) func(time.Time, int)
- func AddWebLogs(w webFramework.WebFramework, title, tag string) func(time.Time, int)
- func InitContext(c any) webFramework.WebFramework
- func InitContextNoAuditTrail(c any) webFramework.WebFramework
- func InitContextWithHandler(c context.Context, _ response.ResponseHandler) webFramework.WebFramework
- func InitNetHTTPContext(r *http.Request, w http.ResponseWriter, unknownUser bool) webFramework.WebFramework
- type TestingParser
- func (t TestingParser) Abort() error
- func (t TestingParser) AddCustomAttributes(attr slog.Attr)
- func (t TestingParser) AddSpanAttribute(key, value string)
- func (t TestingParser) AddSpanAttributes(attrs map[string]string)
- func (t TestingParser) AddSpanEvent(name string, attrs map[string]string)
- func (t TestingParser) CheckURLParam(name string) (string, bool)
- func (c TestingParser) FileAttachment(path, fileName string)
- func (c TestingParser) FormValue(name string) string
- func (t TestingParser) GetArgs(args ...any) map[string]string
- func (t TestingParser) GetBody(target any) error
- func (t TestingParser) GetContext() context.Context
- func (t TestingParser) GetHTTPHeader() http.Header
- func (t TestingParser) GetHeader(target webFramework.HeaderInterface) error
- func (t TestingParser) GetHeaderValue(name string) string
- func (t TestingParser) GetLocal(name string) any
- func (t TestingParser) GetLocalString(name string) string
- func (t TestingParser) GetMethod() string
- func (t TestingParser) GetPath() string
- func (t TestingParser) GetRawURLQuery() string
- func (t TestingParser) GetTraceContext() trace.SpanContext
- func (t TestingParser) GetURI(target any) error
- func (t TestingParser) GetURLParam(name string) string
- func (t TestingParser) GetURLParams() map[string]string
- func (t TestingParser) GetURLQuery(target any) error
- func (t TestingParser) Next() error
- func (t TestingParser) ParseCommand(command, title string, request webFramework.RecordData, ...) string
- func (t TestingParser) RecordSpanError(err error, attrs map[string]string)
- func (c TestingParser) SaveFile(formTagName, path string) error
- func (t TestingParser) SendJSONRespBody(status int, resp any) error
- func (t TestingParser) SetContext(ctx context.Context)
- func (t TestingParser) SetLocal(name string, value any)
- func (t TestingParser) SetReqHeader(name string, value string)
- func (t TestingParser) SetRespHeader(name string, value string)
- func (t TestingParser) SetTraceContext(spanCtx trace.SpanContext)
- func (t TestingParser) StartSpan(name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
Constants ¶
const ( // WebFrameworkKey is the context key used to store the web framework type. WebFrameworkKey = libQuery.ContextKey("webFramework") // Gin identifies the Gin web framework. Gin = "gin" // Fiber identifies the Fiber web framework. Fiber = "fiber" // NetHTTP identifies the net/http web framework. NetHTTP = "nethttp" // Testing identifies the testing framework. Testing = "testing" // UserIDHeader is the HTTP header name for the user identifier. UserIDHeader = "User-Id" // UserIDLocal is the local storage key for the user identifier. UserIDLocal = "userId" // UnknownUser is the default user identifier when none is provided. UnknownUser = "unknown" )
const ( // HeaderEnvKey is the environment variable name for test headers. HeaderEnvKey = "h" // LocalEnvKey is the environment variable name for test locals. LocalEnvKey = "l" )
Variables ¶
This section is empty.
Functions ¶
func AddMiddlewareLogs ¶ added in v0.15.5
func AddMiddlewareLogs(w webFramework.WebFramework, title, tag string) func(time.Time, int)
AddMiddlewareLogs adds request metadata as direct logs and returns a closure that logs elapsed time and status.
func AddWebHandlerLogs ¶ added in v0.15.1
AddWebHandlerLogs initializes a context from the given framework context and returns a logging closure.
func AddWebLogs ¶ added in v0.15.1
func AddWebLogs(w webFramework.WebFramework, title, tag string) func(time.Time, int)
AddWebLogs adds request metadata as log tags and returns a closure that logs elapsed time and status.
func InitContext ¶
func InitContext(c any) webFramework.WebFramework
InitContext initializes a webFramework.WebFramework from the given framework-specific context.
func InitContextNoAuditTrail ¶ added in v0.9.9
func InitContextNoAuditTrail(c any) webFramework.WebFramework
InitContextNoAuditTrail initializes a webFramework.WebFramework without audit trail. It is useful in Get handlers which mostly don't have audit trail.
func InitContextWithHandler ¶
func InitContextWithHandler(c context.Context, _ response.ResponseHandler) webFramework.WebFramework
InitContextWithHandler initializes a webFramework.WebFramework from the given context and response handler.
func InitNetHTTPContext ¶ added in v0.28.1
func InitNetHTTPContext(r *http.Request, w http.ResponseWriter, unknownUser bool) webFramework.WebFramework
InitNetHTTPContext initializes a webFramework.WebFramework for the net/http framework.
Types ¶
type TestingParser ¶ added in v0.9.7
type TestingParser struct {
Root *testing.T
Method, Path, RawQuery string
Header webFramework.HeaderInterface
HeaderError error
Uri any
UriError error
HttpHeader http.Header
Body, UrlQuery any
BodyError, UrlQueryError error
Headers *sync.Map
Locals *sync.Map
UrlParams map[string]string
Args map[string]string
NextError error
AbortError error
SendError error
ParsedCommands map[string]string
}
TestingParser implements the webFramework.RequestParser interface for testing.
func (TestingParser) Abort ¶ added in v0.9.7
func (t TestingParser) Abort() error
Abort simulates aborting the middleware chain and returns the configured abort error.
func (TestingParser) AddCustomAttributes ¶ added in v0.13.2
func (t TestingParser) AddCustomAttributes(attr slog.Attr)
AddCustomAttributes stores a custom slog attribute in the test locals map.
func (TestingParser) AddSpanAttribute ¶ added in v0.18.0
func (t TestingParser) AddSpanAttribute(key, value string)
AddSpanAttribute adds a span attribute (no-op for testing).
func (TestingParser) AddSpanAttributes ¶ added in v0.18.0
func (t TestingParser) AddSpanAttributes(attrs map[string]string)
AddSpanAttributes adds multiple span attributes (no-op for testing).
func (TestingParser) AddSpanEvent ¶ added in v0.18.0
func (t TestingParser) AddSpanEvent(name string, attrs map[string]string)
AddSpanEvent adds a span event (no-op for testing).
func (TestingParser) CheckURLParam ¶ added in v0.28.1
func (t TestingParser) CheckURLParam(name string) (string, bool)
CheckURLParam returns a URL path parameter by name and whether it exists.
func (TestingParser) FileAttachment ¶ added in v0.10.4
func (c TestingParser) FileAttachment(path, fileName string)
FileAttachment simulates sending a file attachment (no-op in the test parser).
func (TestingParser) FormValue ¶ added in v0.10.1
func (c TestingParser) FormValue(name string) string
FormValue returns an empty string for form values in the test parser.
func (TestingParser) GetArgs ¶ added in v0.9.7
func (t TestingParser) GetArgs(args ...any) map[string]string
GetArgs returns the test arguments map.
func (TestingParser) GetBody ¶ added in v0.9.7
func (t TestingParser) GetBody(target any) error
GetBody populates the target with the test request body.
func (TestingParser) GetContext ¶ added in v0.22.0
func (t TestingParser) GetContext() context.Context
GetContext returns a background context for testing
func (TestingParser) GetHTTPHeader ¶ added in v0.28.1
func (t TestingParser) GetHTTPHeader() http.Header
GetHTTPHeader returns the full HTTP header map for the test request.
func (TestingParser) GetHeader ¶ added in v0.9.7
func (t TestingParser) GetHeader(target webFramework.HeaderInterface) error
GetHeader populates the target struct with the test header value.
func (TestingParser) GetHeaderValue ¶ added in v0.9.7
func (t TestingParser) GetHeaderValue(name string) string
GetHeaderValue returns a header value by name from the test headers map.
func (TestingParser) GetLocal ¶ added in v0.9.7
func (t TestingParser) GetLocal(name string) any
GetLocal returns a value from the test locals map by name.
func (TestingParser) GetLocalString ¶ added in v0.9.7
func (t TestingParser) GetLocalString(name string) string
GetLocalString returns a string value from the test locals map by name.
func (TestingParser) GetMethod ¶ added in v0.9.7
func (t TestingParser) GetMethod() string
GetMethod returns the HTTP method for the test request.
func (TestingParser) GetPath ¶ added in v0.9.7
func (t TestingParser) GetPath() string
GetPath returns the URL path for the test request.
func (TestingParser) GetRawURLQuery ¶ added in v0.28.1
func (t TestingParser) GetRawURLQuery() string
GetRawURLQuery returns the raw query string for the test request.
func (TestingParser) GetTraceContext ¶ added in v0.18.0
func (t TestingParser) GetTraceContext() trace.SpanContext
Tracing methods for TestingParser
func (TestingParser) GetURI ¶ added in v0.28.1
func (t TestingParser) GetURI(target any) error
GetURI populates the target with the test URI parameters.
func (TestingParser) GetURLParam ¶ added in v0.28.1
func (t TestingParser) GetURLParam(name string) string
GetURLParam returns a URL path parameter by name from the test parameters.
func (TestingParser) GetURLParams ¶ added in v0.28.1
func (t TestingParser) GetURLParams() map[string]string
GetURLParams returns all URL path parameters for the test request.
func (TestingParser) GetURLQuery ¶ added in v0.28.1
func (t TestingParser) GetURLQuery(target any) error
GetURLQuery populates the target with the test URL query parameters.
func (TestingParser) Next ¶ added in v0.9.7
func (t TestingParser) Next() error
Next simulates advancing to the next middleware and returns the configured next error.
func (TestingParser) ParseCommand ¶ added in v0.9.7
func (t TestingParser) ParseCommand(command, title string, request webFramework.RecordData, parser webFramework.FieldParser) string
ParseCommand returns a pre-parsed command from the test parsed commands map.
func (TestingParser) RecordSpanError ¶ added in v0.18.0
func (t TestingParser) RecordSpanError(err error, attrs map[string]string)
RecordSpanError records a span error (no-op for testing).
func (TestingParser) SaveFile ¶ added in v0.10.1
func (c TestingParser) SaveFile( formTagName, path string, ) error
SaveFile simulates saving an uploaded file and always returns nil in the test parser.
func (TestingParser) SendJSONRespBody ¶ added in v0.9.7
func (t TestingParser) SendJSONRespBody(status int, resp any) error
SendJSONRespBody simulates sending a JSON response body and returns the configured send error.
func (TestingParser) SetContext ¶ added in v0.22.0
func (t TestingParser) SetContext(ctx context.Context)
SetContext is a no-op for testing
func (TestingParser) SetLocal ¶ added in v0.9.7
func (t TestingParser) SetLocal(name string, value any)
SetLocal stores a value in the test locals map by name.
func (TestingParser) SetReqHeader ¶ added in v0.9.7
func (t TestingParser) SetReqHeader(name string, value string)
SetReqHeader sets a request header value in the test headers map.
func (TestingParser) SetRespHeader ¶ added in v0.10.28
func (t TestingParser) SetRespHeader(name string, value string)
SetRespHeader sets a response header value in the test headers map.
func (TestingParser) SetTraceContext ¶ added in v0.18.0
func (t TestingParser) SetTraceContext(spanCtx trace.SpanContext)
SetTraceContext sets the trace span context (no-op for testing).
func (TestingParser) StartSpan ¶ added in v0.18.0
func (t TestingParser) StartSpan(name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
StartSpan starts a new tracing span and returns a background context with nil span for testing.