jawstest

package
v0.505.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package jawstest provides an importable harness for driving a jaws.Request's WebSocket message-processing loop in tests.

It lives in its own package, rather than in package jaws, so that net/http/httptest stays out of the production build of consumers that import github.com/linkdata/jaws. It reaches the request loop through the exported jaws.Jaws.TestServe hook.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TestRequest

type TestRequest struct {
	*jaws.Request
	Recorder *httptest.ResponseRecorder
	ReadyCh  chan struct{}
	DoneCh   chan struct{}
	InCh     chan wire.WsMsg
	OutCh    chan wire.WsMsg
	BcastCh  chan wire.Message
}

TestRequest is a request harness intended for tests.

The embedded jaws.Request provides the usual request methods (NewElement, JawsKeyString, and so on). The channels expose the loop's wiring: send incoming WebSocket messages on InCh, read outbound messages from OutCh, and inject broadcasts on BcastCh. ReadyCh is closed once the loop is running and DoneCh once it has stopped.

OutCh is buffered but must be drained: a test that produces more outbound messages than the buffer holds without reading OutCh stalls the loop, and a wait on DoneCh after TestRequest.Close then never completes.

Recorder is a sink for the test's own rendering, for example as the Writer of a ui.RequestWriter; nothing in the harness writes to it.

func NewTestRequest

func NewTestRequest(jw *jaws.Jaws, r *http.Request) *TestRequest

NewTestRequest creates a TestRequest for use when testing. Passing nil for r creates a GET / request with no body. It requires the Jaws processing loop (jaws.Jaws.Serve or jaws.Jaws.ServeWithTimeout) to be running, and returns nil if the request cannot be created or claimed.

func (*TestRequest) BodyHTML

func (tr *TestRequest) BodyHTML() template.HTML

BodyHTML returns the recorded response body as trusted HTML.

func (*TestRequest) BodyString

func (tr *TestRequest) BodyString() string

BodyString returns the recorded response body with surrounding whitespace removed.

func (*TestRequest) Close

func (tr *TestRequest) Close()

Close stops the test request's processing loop by closing InCh.

It does not wait for the loop to stop; wait on DoneCh for that. Calling Close more than once panics.

Jump to

Keyboard shortcuts

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