Help us shape the future of Go! Take the 2026 Go Developer Survey

testserver

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package testserver stands in for meshStack's login endpoint and for one endpoint behind it, so that a test can drive the token cache without a backend.

It imports internal/auth for the api key settings. A package whose own tests live in the internal test package therefore cannot use it without creating an import cycle, and has to move those tests to an external test package first.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiKey

type ApiKey struct {
	ClientId     string `json:"clientId"`
	ClientSecret string `json:"clientSecret"`
}

func (ApiKey) SetEnv

func (k ApiKey) SetEnv(t *testing.T)

SetEnv points the api key settings at this key for the rest of the test, so that the code under test resolves this credential.

type Counts

type Counts struct {
	Logins        int64
	Greetings     int64
	RevokedTokens int64
	// UnknownTokens counts the 401s no test asked for: a token this Server never minted, or
	// no token at all. It is the one count that stays at zero.
	UnknownTokens int64
}

type GreetingClient

type GreetingClient func(ctx context.Context, url *url.URL) (string, error)

GreetingClient is all this package needs of the code under test: a GET that parses a JSON string, carrying whatever authorization the caller wired up.

type Server

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

func New

func New(t *testing.T, honored ...ApiKey) *Server

New starts a server that honors the given api keys, and stops it when the test ends.

func (*Server) Counts

func (s *Server) Counts(t *testing.T) Counts

func (*Server) Greeting

func (s *Server) Greeting(t *testing.T, ctx context.Context, greet GreetingClient) error

Greeting calls the authorized endpoint and returns what went wrong, for a caller on its own goroutine, where require may not be used.

func (*Server) GreetingUrl

func (s *Server) GreetingUrl(t *testing.T) *url.URL

func (*Server) MintToken

func (s *Server) MintToken(t *testing.T, validFor time.Duration) string

MintToken issues a token this Server honors, exactly as its login endpoint does, for a test that hands one to the code under test directly.

func (*Server) RequireGreeting

func (s *Server) RequireGreeting(t *testing.T, greet GreetingClient)

func (*Server) RevokeNewestToken

func (s *Server) RevokeNewestToken(t *testing.T) (revoked bool)

RevokeNewestToken stops honoring the newest token still honored, and reports whether there was one. The newest is the one a cache is most likely to be holding, so it is the one that makes a caller refresh.

A concurrent caller must not have a request in flight while this runs. An authorized client retries a 401 exactly once, and the token it retries with can be one it adopted from another process's cache, so revoking during a request can leave a 401 that nothing recovers from.

func (*Server) Url

func (s *Server) Url(t *testing.T) *url.URL

Jump to

Keyboard shortcuts

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