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 ¶
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 (*FakeHub) Publish ¶
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 ¶
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 ¶
Subscriptions returns what the hub currently holds for topic, after settling.
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 ¶
DenyAll makes the hub refuse every subscription with the given reason, so a subscriber's denial handling can be tested.
func WithHubOptions ¶
WithHubOptions passes options straight through to the underlying websub.Hub, for tests that need a particular lease range, signature algorithm, or store.