{"modulePath":"github.com/matrix-org/complement","version":"v0.0.0-20261130133012-5a6e00b89831","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package federation\n\npackage federation is an EXPERIMENTAL set of functions for creating mock federation servers, for testing over federation. It is marked as EXPERIMENTAL as the API may break without warning.\n\n## Functions\n\n```go\nfunc HandleDirectoryLookups() func(*Server)\n```\nEXPERIMENTAL HandleDirectoryLookups will automatically return room IDs for any aliases present on this server.\n\n```go\nfunc HandleEventAuthRequests() func(*Server)\n```\nEXPERIMENTAL HandleEventAuthRequests is an option which will process GET /\\_matrix/federation/v1/event\\_auth/{roomId}/{eventId} requests universally when requested.\n\n```go\nfunc HandleEventRequests() func(*Server)\n```\nEXPERIMENTAL HandleEventRequests is an option which will process GET /\\_matrix/federation/v1/event/{eventId} requests universally when requested.\n\n```go\nfunc HandleInviteRequests(inviteCallback func(gomatrixserverlib.PDU)) func(*Server)\n```\nEXPERIMENTAL HandleInviteRequests is an option which makes the server process invite requests.\n\ninviteCallback is a callback function that if non-nil will be called and passed the incoming invite event\n\n```go\nfunc HandleKeyRequests() func(*Server)\n```\nEXPERIMENTAL HandleKeyRequests is an option which will process GET /\\_matrix/key/v2/server requests universally when requested.\n\n```go\nfunc HandleMakeSendJoinRequests() func(*Server)\n```\nEXPERIMENTAL HandleMakeSendJoinRequests is an option which will process make\\_join and send\\_join requests for rooms which are present in this server. To add a room to this server, see Server.MustMakeRoom. No checks are done to see whether join requests are allowed or not. If you wish to test that, write your own test.\n\n```go\nfunc HandleMediaRequests(mediaIds map[string]func(w http.ResponseWriter)) func(*Server)\n```\nEXPERIMENTAL HandleMediaRequests is an option which will process /\\_matrix/media/v1/download/\\* using the provided map as a way to do so. The key of the map is the media ID to be handled.\n\n```go\nfunc HandlePartialStateMakeSendJoinRequests() func(*Server)\n```\nHandlePartialStateMakeSendJoinRequests is similar to HandleMakeSendJoinRequests, but expects a partial-state join.\n\n```go\nfunc HandleTransactionRequests(pduCallback func(gomatrixserverlib.PDU), eduCallback func(gomatrixserverlib.EDU)) func(*Server)\n```\nEXPERIMENTAL HandleTransactionRequests is an option which will process GET /\\_matrix/federation/v1/send/{transactionID} requests universally when requested. pduCallback and eduCallback are functions that if non-nil will be called and passed each PDU or EDU event received in the transaction. Callbacks will be fired AFTER the event has been stored onto the respective ServerRoom.\n\n```go\nfunc MakeJoinRequestsHandler(s *Server, w http.ResponseWriter, req *http.Request)\n```\nEXPERIMENTAL MakeJoinRequestsHandler is the http.Handler implementation for the make\\_join part of HandleMakeSendJoinRequests.\n\n```go\nfunc MakeRespMakeJoin(s *Server, room *ServerRoom, userID string) (resp fclient.RespMakeJoin, err error)\n```\nEXPERIMENTAL MakeRespMakeJoin makes the response for a /make\\_join request, without verifying any signatures or dealing with HTTP responses itself.\n\n```go\nfunc MakeRespMakeKnock(s *Server, room *ServerRoom, userID string) (resp fclient.RespMakeKnock, err error)\n```\nEXPERIMENTAL MakeRespMakeKnock makes the response for a /make\\_knock request, without verifying any signatures or dealing with HTTP responses itself.\n\n```go\nfunc SendJoinRequestsHandler(s *Server, w http.ResponseWriter, req *http.Request, expectPartialState bool, omitServersInRoom bool)\n```\nEXPERIMENTAL SendJoinRequestsHandler is the http.Handler implementation for the send\\_join part of HandleMakeSendJoinRequests.\n\nexpectPartialState should be true if we should expect the incoming send\\_join request to use the partial\\_state flag, per MSC3706. In that case, we reply with only the critical subset of the room state.\n\nomitServersInRoom should be false to respond to partial\\_state joins with the complete list of servers in the room. When omitServersInRoom is true, a misbehaving server is simulated and only the current server is returned to the joining server.\n\n```go\nfunc SupportedRoomVersions() []gomatrixserverlib.RoomVersion\n```\nSupportedRoomVersions is a convenience method which returns a list of the room versions supported by gomatrixserverlib.\n\n## Types\n\n```go\ntype Event struct {\n\tType     string\n\tSender   string\n\tStateKey *string\n\tContent  map[string]interface{}\n\n\tUnsigned map[string]interface{}\n\t// The events needed to authenticate this event.\n\t// This can be either []EventReference for room v1/v2, or []string for room v3 onwards.\n\t// If it is left at nil, MustCreateEvent will populate it automatically based on the room state.\n\tAuthEvents interface{}\n\t// The prev events of the event if we want to override or falsify them.\n\t// If it is left at nil, MustCreateEvent will populate it automatically based on the forward extremities.\n\tPrevEvents interface{}\n\t// If this is a redaction, the event that it redacts\n\tRedacts string\n}\n```\n\n```go\nfunc InitialRoomEvents(roomVer gomatrixserverlib.RoomVersion, creator string) []Event\n```\nInitialRoomEvents returns the initial set of events that get created when making a room.\n\n```go\ntype FederationDeployment interface {\n\tGetConfig() *config.Complement\n\tRoundTripper() http.RoundTripper\n}\n```\nSubset of Deployment used in federation\n\n```go\ntype JoinRoomOpt func(jr *joinRoom)\n```\nJoinRoomOpt is an option for configuring how the server should join the room\n\n```go\nfunc WithPartialState() JoinRoomOpt\n```\nWithPartialState tells the server to join the room with partial state\n\n```go\nfunc WithRoomOpts(opts ...ServerRoomOpt) JoinRoomOpt\n```\nWithRoomOpts controls how the newly joined room is created\n\n```go\ntype KnockRoomOpt func(kr *knockRoom)\n```\nKnockRoomOpt is an option for configuring how the server should knock on the room\n\n```go\nfunc WithStrictKnockRoomStateChecks() KnockRoomOpt\n```\nWithStrictKnockRoomStateChecks tells the server to strictly check that the received \\`knock\\_room\\_state\\` is valid according to the spec (c.f. MSC4311).\n\n```go\ntype Server struct {\n\n\t// Default: true\n\tUnexpectedRequestsAreErrors bool\n\n\tPriv  ed25519.PrivateKey\n\tKeyID gomatrixserverlib.KeyID\n\t// contains filtered or unexported fields\n}\n```\nEXPERIMENTAL Server represents a federation server\n\n```go\nfunc NewServer(t ct.TestLike, deployment FederationDeployment, opts ...func(*Server)) *Server\n```\nEXPERIMENTAL NewServer creates a new federation server with configured options.\n\n```go\nfunc (s *Server) DoFederationRequest(\n\tctx context.Context,\n\tt ct.TestLike,\n\tdeployment FederationDeployment,\n\treq fclient.FederationRequest) (*http.Response, error)\n```\nDoFederationRequest signs and sends an arbitrary federation request from this server, and returns the response.\n\nThe requests will be routed according to the deployment map in \\`deployment\\`.\n\nThe caller does not need to worry about closing the returned \\`http.Response.Body\\` as this is handled automatically.\n\n```go\nfunc (s *Server) FederationClient(deployment FederationDeployment) fclient.FederationClient\n```\nFederationClient returns a client which will sign requests using this server's key.\n\nThe requests will be routed according to the deployment map in \\`deployment\\`, which satisfies the RoundTripper interface.\n\n```go\nfunc (s *Server) Listen() (cancel func())\n```\n\n```go\nfunc (s *Server) MakeAliasMapping(aliasLocalpart, roomID string) string\n```\nMakeAliasMapping will create a mapping of room alias to room ID on this server. Returns the alias. If this is the first time calling this function, a directory lookup handler will be added to handle alias requests over federation.\n\n```go\nfunc (s *Server) MustCreateEvent(t ct.TestLike, room *ServerRoom, ev Event) gomatrixserverlib.PDU\n```\nMustCreateEvent will create and sign a new latest event for the given room. It does not insert this event into the room however. See ServerRoom.AddEvent for that.\n\n```go\nfunc (s *Server) MustJoinRoom(t ct.TestLike, deployment FederationDeployment, remoteServer spec.ServerName, roomID string, userID string, opts ...JoinRoomOpt) *ServerRoom\n```\nMustJoinRoom will make the server send a /make\\_join and a /send\\_join to join a room It returns the resultant room.\n\nArgs:\n\n  - \\`remoteServer\\`: This should be a resolvable address within the deployment network.\n\n```go\nfunc (s *Server) MustKnockRoom(\n\tt ct.TestLike,\n\tdeployment FederationDeployment,\n\tremoteServer spec.ServerName,\n\troomID string,\n\tuserID string,\n\topts ...KnockRoomOpt,\n) *ServerRoom\n```\nMustKnockRoom  will make the server send a /make\\_knock and a /send\\_knock to knock on a room It returns the resultant room.\n\nArgs:\n\n  - \\`remoteServer\\`: This should be a resolvable address within the deployment network.\n\n```go\nfunc (s *Server) MustLeaveRoom(t ct.TestLike, deployment FederationDeployment, remoteServer spec.ServerName, roomID string, userID string)\n```\nLeaves a room. If this is rejecting an invite then a make\\_leave request is made first, before send\\_leave.\n\nArgs:\n\n  - \\`remoteServer\\`: This should be a resolvable address within the deployment network.\n\n```go\nfunc (s *Server) MustMakeRoom(t ct.TestLike, roomVer gomatrixserverlib.RoomVersion, events []Event, opts ...ServerRoomOpt) *ServerRoom\n```\nMustMakeRoom will add a room to this server so it is accessible to other servers when prompted via federation. The \\`events\\` will be added to this room. Returns the created room.\n\n```go\nfunc (s *Server) MustSendTransaction(t ct.TestLike, deployment FederationDeployment, destination spec.ServerName, pdus []json.RawMessage, edus []gomatrixserverlib.EDU)\n```\nMustSendTransaction sends the given PDUs/EDUs to the target destination, returning an error if the /send fails or if the response contains an error for any sent PDUs. Times out after 10 seconds.\n\nArgs:\n\n  - \\`destination\\`: This should be a resolvable address within the deployment network.\n\n```go\nfunc (s *Server) Mux() *mux.Router\n```\nMux returns this server's router so you can attach additional paths.\n\n```go\nfunc (s *Server) SendFederationRequest(\n\tctx context.Context,\n\tt ct.TestLike,\n\tdeployment FederationDeployment,\n\treq fclient.FederationRequest,\n\tresBody interface{},\n) error\n```\nSendFederationRequest signs and sends an arbitrary federation request from this server.\n\nThe requests will be routed according to the deployment map in \\`deployment\\`.\n\n```go\nfunc (s *Server) ServerName() spec.ServerName\n```\nReturn the server name of this federation server. Only valid AFTER calling Listen() - doing so before will produce an error.\n\nIt is not supported to call ServerName() before Listen() because Listen() modifies the server name. Listen() will select a random OS-provided high-numbered port to listen on, which then needs to be retrofitted into the server name so containers know how to route to it.\n\n```go\nfunc (s *Server) UserID(localpart string) string\n```\nUserID returns the complete user ID for the given localpart\n\n```go\nfunc (s *Server) ValidFederationRequest(t ct.TestLike, handler func(fr *fclient.FederationRequest, pathParams map[string]string) util.JSONResponse) http.HandlerFunc\n```\nValidFederationRequest is a wrapper around http.HandlerFunc which automatically validates the incoming federation request and supports sending back JSON. Fails the test if the request is not valid.\n\n```go\ntype ServerRoom struct {\n\t// Functions to map Complement events into actual PDUs.\n\t// Most tests don't care about this and can use the default functions,\n\t// but if your MSC or tests fiddle with the raw JSON in some way then these\n\t// function need to be replaced. By replacing these functions, helper functions\n\t// which indirectly create events (e.g joins and leaves) will automatically use\n\t// them and will hence work with your custom code.\n\tServerRoomImpl\n\n\tVersion            gomatrixserverlib.RoomVersion\n\tRoomID             string\n\tState              map[string]gomatrixserverlib.PDU\n\tStateMutex         sync.RWMutex\n\tTimeline           []gomatrixserverlib.PDU\n\tTimelineMutex      sync.RWMutex\n\tForwardExtremities []string\n\tDepth              int64\n\t// contains filtered or unexported fields\n}\n```\nEXPERIMENTAL ServerRoom represents a room on this test federation server\n\n```go\nfunc NewServerRoom(roomVer gomatrixserverlib.RoomVersion, roomId string) *ServerRoom\n```\nNewServerRoom creates an empty room structure with no events\n\n```go\nfunc (r *ServerRoom) AddEvent(ev gomatrixserverlib.PDU)\n```\nAddEvent adds a new event to the timeline, updating current state if it is a state event. Updates depth and forward extremities.\n\n```go\nfunc (r *ServerRoom) AllCurrentState() (events []gomatrixserverlib.PDU)\n```\nAllCurrentState returns all the current state events\n\n```go\nfunc (r *ServerRoom) AuthChain() (chain []gomatrixserverlib.PDU)\n```\nAuthChain returns all auth events for all events in the current state TODO: recursively\n\n```go\nfunc (r *ServerRoom) AuthChainForEvents(events []gomatrixserverlib.PDU) (chain []gomatrixserverlib.PDU)\n```\nAuthChainForEvents returns all auth events for all events in the given state\n\n```go\nfunc (r *ServerRoom) AuthEvents(sn gomatrixserverlib.StateNeeded) (eventIDs []string)\n```\nAuthEvents returns the state event IDs of the auth events which authenticate this event\n\n```go\nfunc (r *ServerRoom) CurrentState(evType, stateKey string) gomatrixserverlib.PDU\n```\nCurrentState returns the state event for the given (type, state\\_key) or nil.\n\n```go\nfunc (r *ServerRoom) EventIDsOrReferences(events []gomatrixserverlib.PDU) (refs []interface{})\n```\nEventIDsOrReferences converts a list of events into a list of EventIDs or EventReferences, depending on the room version\n\n```go\nfunc (r *ServerRoom) GetEventInTimeline(eventID string) (gomatrixserverlib.PDU, bool)\n```\nFetches the event with given event ID from the room timeline.\n\n```go\nfunc (r *ServerRoom) MustHaveMembershipForUser(t ct.TestLike, userID, wantMembership string)\n```\nCheck that the user currently has the membership provided in this room. Fails the test if not.\n\n```go\nfunc (r *ServerRoom) ReplaceCurrentState(ev gomatrixserverlib.PDU)\n```\nReplaceCurrentState inserts a new state event for this room or replaces current state depending on the (type, state\\_key) provided. The event provided must be a state event.\n\n```go\nfunc (r *ServerRoom) ServersInRoom() (servers []spec.ServerName)\n```\nServersInRoom gets all servers currently joined to the room\n\n```go\nfunc (r *ServerRoom) WaiterForEvent(eventID string) *helpers.Waiter\n```\nWaiterForEvent creates a Waiter which waits until the given event ID is added to the room. This can be used as a synchronisation point to wait until the server under test has sent a given PDU in a /send transaction to the Complement server. This is the equivalent to listening for the PDU in the /send transaction and then unblocking the Waiter. Note that calling this function doesn't actually block. Call .Wait(time.Duration) on the waiter to block.\n\nNote: you must still add HandleTransactionRequests(nil,nil) to your server for the server to automatically add events to the room.\n\n```go\ntype ServerRoomImpl interface {\n\t// ProtoEventCreator converts a Complement Event into a gomatrixserverlib proto event, ready to be signed.\n\t// This function is used in /make_x endpoints to create proto events to return to other servers.\n\t// This function is one of two used when creating events, the other being EventCreator.\n\tProtoEventCreator(room *ServerRoom, ev Event) (*gomatrixserverlib.ProtoEvent, error)\n\t// EventCreator converts a proto event into a signed PDU.\n\tEventCreator(room *ServerRoom, s *Server, proto *gomatrixserverlib.ProtoEvent) (gomatrixserverlib.PDU, error)\n\t// PopulateFromSendJoinResponse should replace the state of this ServerRoom with the information contained\n\t// in RespSendJoin and the join event.\n\tPopulateFromSendJoinResponse(room *ServerRoom, joinEvent gomatrixserverlib.PDU, resp fclient.RespSendJoin)\n\t// GenerateSendJoinResponse generates a /send_join response to send back to a server.\n\tGenerateSendJoinResponse(room *ServerRoom, s *Server, joinEvent gomatrixserverlib.PDU, expectPartialState, omitServersInRoom bool) fclient.RespSendJoin\n}\n```\n\n```go\ntype ServerRoomImplCustom struct {\n\tServerRoomImplDefault\n\tProtoEventCreatorFn            func(def ServerRoomImpl, room *ServerRoom, ev Event) (*gomatrixserverlib.ProtoEvent, error)\n\tEventCreatorFn                 func(def ServerRoomImpl, room *ServerRoom, s *Server, proto *gomatrixserverlib.ProtoEvent) (gomatrixserverlib.PDU, error)\n\tPopulateFromSendJoinResponseFn func(def ServerRoomImpl, room *ServerRoom, joinEvent gomatrixserverlib.PDU, resp fclient.RespSendJoin)\n\tGenerateSendJoinResponseFn     func(def ServerRoomImpl, room *ServerRoom, s *Server, joinEvent gomatrixserverlib.PDU, expectPartialState, omitServersInRoom bool) fclient.RespSendJoin\n}\n```\n\n```go\nfunc (i *ServerRoomImplCustom) EventCreator(room *ServerRoom, s *Server, proto *gomatrixserverlib.ProtoEvent) (gomatrixserverlib.PDU, error)\n```\n\n```go\nfunc (i *ServerRoomImplCustom) GenerateSendJoinResponse(room *ServerRoom, s *Server, joinEvent gomatrixserverlib.PDU, expectPartialState, omitServersInRoom bool) fclient.RespSendJoin\n```\n\n```go\nfunc (i *ServerRoomImplCustom) PopulateFromSendJoinResponse(room *ServerRoom, joinEvent gomatrixserverlib.PDU, resp fclient.RespSendJoin)\n```\n\n```go\nfunc (i *ServerRoomImplCustom) ProtoEventCreator(room *ServerRoom, ev Event) (*gomatrixserverlib.ProtoEvent, error)\n```\n\n```go\ntype ServerRoomImplDefault struct{}\n```\n\n```go\nfunc (i *ServerRoomImplDefault) EventCreator(room *ServerRoom, s *Server, proto *gomatrixserverlib.ProtoEvent) (gomatrixserverlib.PDU, error)\n```\n\n```go\nfunc (i *ServerRoomImplDefault) GenerateSendJoinResponse(room *ServerRoom, s *Server, joinEvent gomatrixserverlib.PDU, expectPartialState, omitServersInRoom bool) fclient.RespSendJoin\n```\n\n```go\nfunc (i *ServerRoomImplDefault) PopulateFromSendJoinResponse(room *ServerRoom, joinEvent gomatrixserverlib.PDU, resp fclient.RespSendJoin)\n```\n\n```go\nfunc (i *ServerRoomImplDefault) ProtoEventCreator(room *ServerRoom, ev Event) (*gomatrixserverlib.ProtoEvent, error)\n```\n\n```go\ntype ServerRoomOpt func(r *ServerRoom)\n```\nServerRoomOpt are options that can configure ServerRooms\n\n```go\nfunc WithImpl(impl ServerRoomImpl) ServerRoomOpt\n```\nWithImpl configures the room to have the given ServerRoomImpl. Useful for custom rooms.\n\n```go\nfunc WithRoomID(roomID string) ServerRoomOpt\n```\nWithRoomID configures the room to have the given room ID\n\n","path":"github.com/matrix-org/complement/federation","name":"federation","synopsis":"package federation is an EXPERIMENTAL set of functions for creating mock federation servers, for testing over federation.","isRedistributable":true}
