testserver

package
v0.0.0-...-114e636 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package testserver implements a barebone Mastodon server for testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFakeAccount

func NewFakeAccount(accountID mastodon.ID, username string) mastodon.Account

func NewFakeNotification

func NewFakeNotification(notifID mastodon.ID, notifType string, accountSrcID mastodon.ID, status *mastodon.Status) *mastodon.Notification

func NewFakeStatus

func NewFakeStatus(statusID mastodon.ID, accountID mastodon.ID) *mastodon.Status

func NewHTTPErrorf

func NewHTTPErrorf(status int, format string, a ...any) error

func WriteError

func WriteError(w http.ResponseWriter, req *http.Request, err error)

Types

type Entity

type Entity[T any] struct {
	Value T
	// contains filtered or unexported fields
}

type EntityList

type EntityList[T any] struct {
	// contains filtered or unexported fields
}

EntityList manages a sorted list, suitable for being queried with pagination.

func (*EntityList[T]) ByID

func (list *EntityList[T]) ByID(id string) (T, error)

ByID returns the entity with that ID. Returns the default value if not found.

func (*EntityList[T]) Clear

func (list *EntityList[T]) Clear()

func (*EntityList[T]) CreateNextID

func (list *EntityList[T]) CreateNextID() string

CreateNextID gives back an ID suitable for this list which would insert an entity at the end of the list. The ID is not reserved, so multiple call to this function without insertion might return the same value.

func (*EntityList[T]) Insert

func (list *EntityList[T]) Insert(value T, id string) error

Insert a new value in the list, keeping it sorted.

func (*EntityList[T]) List

func (list *EntityList[T]) List(req *http.Request, linkPath string) ([]T, string, error)

List manages a listing requests as a mastodon server. It takes care of pagination, and returns:

  • The list of matching values
  • The content for the `Link` header (can be empty)
  • or an error

func (*EntityList[T]) Update

func (list *EntityList[T]) Update(id string, t T) error

Update changes in-place the value of an entity.

type HTTPError

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

func (HTTPError) Error

func (e HTTPError) Error() string

type JSONHandler

type JSONHandler func(w http.ResponseWriter, req *http.Request) (any, error)

func (JSONHandler) ServeHTTP

func (h JSONHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

type LoggingHandler

type LoggingHandler struct {
	Logf    func(format string, args ...any)
	Handler http.Handler
	// contains filtered or unexported fields
}

LoggingHandler is an intercept http handler which writes http traffic on the provided testing construct.

func (*LoggingHandler) ServeHTTP

func (h *LoggingHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request)

type Server

type Server struct {
	// If present, block on list request.
	// When a request arrives, it will send a `chan struct{}` over the provided channel.
	// The receiver must then close the sent channel to indicate that the test server
	// can continue serving.
	// Must be set before any request is started.
	TestBlockList chan chan struct{}
	// contains filtered or unexported fields
}

func New

func New() *Server

func (*Server) AddFakeNotification

func (s *Server) AddFakeNotification() error

func (*Server) AddFakeStatus

func (s *Server) AddFakeStatus() (*mastodon.Status, error)

func (*Server) AddJSONStatuses

func (s *Server) AddJSONStatuses(statusesFS fs.FS) error

func (*Server) AddReblog

func (s *Server) AddReblog(idToReblog mastodon.ID) (*mastodon.Status, error)

AddReblog creates a reblog of the provided status ID. If the status ID does not already exists, create one.

func (*Server) ClearNotifications

func (s *Server) ClearNotifications()

func (*Server) RegisterOn

func (s *Server) RegisterOn(mux *http.ServeMux)

func (*Server) SetListDelay

func (s *Server) SetListDelay(delay time.Duration)

func (*Server) SetStatusContent

func (s *Server) SetStatusContent(id mastodon.ID, content string) error

func (*Server) SetStatusFavourite

func (s *Server) SetStatusFavourite(id mastodon.ID) error

func (*Server) SetStatusUnfavourite

func (s *Server) SetStatusUnfavourite(id mastodon.ID) error

func (*Server) UpdateStatus

func (s *Server) UpdateStatus(status *mastodon.Status) error

Jump to

Keyboard shortcuts

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