websubtest

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package websubtest provides in-process WebSub servers for testing code that subscribes to or publishes topics.

The hub and publisher here speak the real protocol rather than returning canned responses: NewHub runs an actual websub.Hub, so a subscriber tested against it goes through discovery, subscription, verification of intent, and signed content distribution exactly as it would against a public hub. Nothing leaves the process.

func TestMySubscriber(t *testing.T) {
	hub := websubtest.NewHub(t)
	topic := websubtest.NewPublisher(t, hub, "application/atom+xml", "<feed/>")

	sub, err := websub.NewSubscriber(callbackURL)
	// ... mount sub, then:
	active, err := sub.Subscribe(ctx, topic.URL())

	topic.SetContent("application/atom+xml", "<feed>updated</feed>")
	hub.Publish(t, topic.URL())
}

Every constructor registers its own cleanup, so nothing needs closing by hand.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Content

type Content struct {
	Type string
	Body string
}

Content is an explicit body to distribute, for FakeHub.Publish.

type FakeHub

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

FakeHub is a real hub on a local server.

func NewHub

func NewHub(t testing.TB, opts ...Option) *FakeHub

NewHub starts a hub on a local listener and stops it when the test ends.

func (*FakeHub) Hub

func (h *FakeHub) Hub() *websub.Hub

Hub returns the underlying hub, for anything these helpers do not cover.

func (*FakeHub) Publish

func (h *FakeHub) Publish(t testing.TB, topic string, content ...Content)

Publish distributes topic to every subscriber and waits for delivery.

With no body the hub fetches the topic itself, which is what a real publisher's ping causes. Pass a content type and body to distribute content directly instead.

func (*FakeHub) Settle

func (h *FakeHub) Settle(t testing.TB)

Settle waits for the hub to finish the verification and delivery work it started while answering a request.

A subscriber considers itself subscribed the moment it answers the hub's challenge, which is just before the hub has recorded the subscription. Call this between subscribing and publishing.

func (*FakeHub) Subscriptions

func (h *FakeHub) Subscriptions(t testing.TB, topic string) []websub.Subscription

Subscriptions returns what the hub currently holds for topic, after settling.

func (*FakeHub) URL

func (h *FakeHub) URL() string

URL returns the hub's URL, for handing to a publisher or asserting on.

type FakePublisher

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

FakePublisher serves a topic document that advertises a hub, so a subscriber under test can discover it the same way it would discover a real one.

func NewPublisher

func NewPublisher(t testing.TB, hub *FakeHub, contentType, body string) *FakePublisher

NewPublisher starts a topic on a local listener, advertising hub, and stops it when the test ends.

func NewPublisherAt

func NewPublisherAt(t testing.TB, hubURL, contentType, body string) *FakePublisher

NewPublisherAt is NewPublisher for a hub that is not a FakeHub, such as one the caller is testing.

func (*FakePublisher) Publish

func (p *FakePublisher) Publish(t testing.TB)

Publish tells the hub the topic changed, exactly as a real publisher would. When the hub is a FakeHub it also waits for the delivery that notification triggers, so an assertion straight afterward is safe.

func (*FakePublisher) Publisher

func (p *FakePublisher) Publisher() *websub.Publisher

Publisher returns the underlying publisher.

func (*FakePublisher) Requests

func (p *FakePublisher) Requests() int

Requests returns how many times the topic has been fetched, which is how to tell whether a subscriber performed discovery.

func (*FakePublisher) SetContent

func (p *FakePublisher) SetContent(contentType, body string)

SetContent replaces what the topic serves. The next publish distributes it.

func (*FakePublisher) URL

func (p *FakePublisher) URL() string

URL returns the topic URL, which is also the self URL it advertises.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures a fake hub or publisher.

func DenyAll

func DenyAll(reason string) Option

DenyAll makes the hub refuse every subscription with the given reason, so a subscriber's denial handling can be tested.

func WithHubOptions

func WithHubOptions(opts ...websub.HubOption) Option

WithHubOptions passes options straight through to the underlying websub.Hub, for tests that need a particular lease range, signature algorithm, or store.

func WithMode

func WithMode(m websub.Mode) Option

WithMode selects which specification the fake hub applies. The default is websub.ModeWebSub.

Jump to

Keyboard shortcuts

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