Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
226 lines
5.1 KiB
Go
226 lines
5.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package request
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
)
|
|
|
|
// Context should be abbreviated as `rctx`.
|
|
type Context struct {
|
|
t i18n.TranslateFunc
|
|
session model.Session
|
|
requestId string
|
|
ipAddress string
|
|
xForwardedFor string
|
|
path string
|
|
userAgent string
|
|
acceptLanguage string
|
|
connectionId string
|
|
logger mlog.LoggerIFace
|
|
context context.Context
|
|
}
|
|
|
|
func NewContext(ctx context.Context, requestId, ipAddress, xForwardedFor, path, userAgent, acceptLanguage string, t i18n.TranslateFunc) *Context {
|
|
return &Context{
|
|
t: t,
|
|
requestId: requestId,
|
|
ipAddress: ipAddress,
|
|
xForwardedFor: xForwardedFor,
|
|
path: path,
|
|
userAgent: userAgent,
|
|
acceptLanguage: acceptLanguage,
|
|
context: ctx,
|
|
}
|
|
}
|
|
|
|
func EmptyContext(logger mlog.LoggerIFace) *Context {
|
|
return &Context{
|
|
t: i18n.T,
|
|
logger: logger,
|
|
context: context.Background(),
|
|
}
|
|
}
|
|
|
|
// TestContext creates an empty context with a new logger to use in testing where a test helper is
|
|
// not required.
|
|
func TestContext(tb testing.TB) *Context {
|
|
logger := mlog.CreateConsoleTestLogger(tb)
|
|
return EmptyContext(logger)
|
|
}
|
|
|
|
// clone creates a shallow copy of Context, allowing clones to apply per-request changes.
|
|
func (c *Context) clone() *Context {
|
|
cCopy := *c
|
|
return &cCopy
|
|
}
|
|
|
|
func (c *Context) T(translationID string, args ...any) string {
|
|
return c.t(translationID, args...)
|
|
}
|
|
|
|
func (c *Context) GetT() i18n.TranslateFunc {
|
|
return c.t
|
|
}
|
|
|
|
func (c *Context) Session() *model.Session {
|
|
return &c.session
|
|
}
|
|
|
|
func (c *Context) RequestId() string {
|
|
return c.requestId
|
|
}
|
|
|
|
func (c *Context) IPAddress() string {
|
|
return c.ipAddress
|
|
}
|
|
|
|
func (c *Context) XForwardedFor() string {
|
|
return c.xForwardedFor
|
|
}
|
|
|
|
func (c *Context) Path() string {
|
|
return c.path
|
|
}
|
|
|
|
func (c *Context) UserAgent() string {
|
|
return c.userAgent
|
|
}
|
|
|
|
func (c *Context) AcceptLanguage() string {
|
|
return c.acceptLanguage
|
|
}
|
|
|
|
// ConnectionId returns the identifier of the WebSocket connection associated
|
|
// with the request, when present. It is populated from the "Connection-Id"
|
|
// HTTP header that authenticated clients set when they have an active
|
|
// WebSocket connection, allowing handlers and plugins to correlate an HTTP
|
|
// request with its originating WebSocket connection. Returns an empty string
|
|
// when the header is absent (e.g., requests from clients without an active
|
|
// WebSocket connection or from non-WebSocket integrations).
|
|
func (c *Context) ConnectionId() string {
|
|
return c.connectionId
|
|
}
|
|
|
|
func (c *Context) Logger() mlog.LoggerIFace {
|
|
return c.logger
|
|
}
|
|
|
|
func (c *Context) Context() context.Context {
|
|
return c.context
|
|
}
|
|
|
|
func (c *Context) WithT(t i18n.TranslateFunc) CTX {
|
|
rctx := c.clone()
|
|
rctx.t = t
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithSession(s *model.Session) CTX {
|
|
rctx := c.clone()
|
|
if s == nil {
|
|
rctx.session = model.Session{}
|
|
} else {
|
|
rctx.session = *s
|
|
}
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithRequestId(s string) CTX {
|
|
rctx := c.clone()
|
|
rctx.requestId = s
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithIPAddress(s string) CTX {
|
|
rctx := c.clone()
|
|
rctx.ipAddress = s
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithXForwardedFor(s string) CTX {
|
|
rctx := c.clone()
|
|
rctx.xForwardedFor = s
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithPath(s string) CTX {
|
|
rctx := c.clone()
|
|
rctx.path = s
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithUserAgent(s string) CTX {
|
|
rctx := c.clone()
|
|
rctx.userAgent = s
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithAcceptLanguage(s string) CTX {
|
|
rctx := c.clone()
|
|
rctx.acceptLanguage = s
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithConnectionId(s string) CTX {
|
|
rctx := c.clone()
|
|
rctx.connectionId = s
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithContext(ctx context.Context) CTX {
|
|
rctx := c.clone()
|
|
rctx.context = ctx
|
|
return rctx
|
|
}
|
|
|
|
func (c *Context) WithLogger(logger mlog.LoggerIFace) CTX {
|
|
rctx := c.clone()
|
|
rctx.logger = logger
|
|
return rctx
|
|
}
|
|
|
|
// WithLogFields returns a new context with the given fields added to the logger.
|
|
func (c *Context) WithLogFields(fields ...mlog.Field) CTX {
|
|
return c.WithLogger(c.logger.With(fields...))
|
|
}
|
|
|
|
func (c *Context) With(f func(ctx CTX) CTX) CTX {
|
|
return f(c)
|
|
}
|
|
|
|
// CTX should be abbreviated as `rctx`.
|
|
type CTX interface {
|
|
T(string, ...any) string
|
|
GetT() i18n.TranslateFunc
|
|
Session() *model.Session
|
|
RequestId() string
|
|
IPAddress() string
|
|
XForwardedFor() string
|
|
Path() string
|
|
UserAgent() string
|
|
AcceptLanguage() string
|
|
ConnectionId() string
|
|
Logger() mlog.LoggerIFace
|
|
Context() context.Context
|
|
WithT(i18n.TranslateFunc) CTX
|
|
WithSession(s *model.Session) CTX
|
|
WithRequestId(string) CTX
|
|
WithIPAddress(string) CTX
|
|
WithXForwardedFor(string) CTX
|
|
WithPath(string) CTX
|
|
WithUserAgent(string) CTX
|
|
WithAcceptLanguage(string) CTX
|
|
WithConnectionId(string) CTX
|
|
WithLogger(mlog.LoggerIFace) CTX
|
|
WithLogFields(fields ...mlog.Field) CTX
|
|
WithContext(ctx context.Context) CTX
|
|
With(func(ctx CTX) CTX) CTX
|
|
}
|