jawstest

package
v0.804.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

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

The harness uses the real JaWS request-processing loop and exposes its channels directly. Start jaws.Jaws.Serve or jaws.Jaws.ServeWithTimeout before constructing a TestRequest, wait for TestRequest.ReadyCh before driving it, and drain TestRequest.OutCh while output can be produced. TestRequest.Close closes only the inbound channel; callers should then wait for TestRequest.DoneCh and must not close TestRequest.OutCh or TestRequest.BcastCh.

Index

Examples

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 // response recorder
	ReadyCh  chan struct{}              // closed once the processing loop is running
	DoneCh   chan struct{}              // closed once the processing loop has stopped
	InCh     chan wire.WsMsg            // send inbound WebSocket messages here
	OutCh    chan wire.WsMsg            // outbound messages; buffered but must be drained or the loop stalls
	BcastCh  chan wire.Message          // inject broadcasts here
	// contains filtered or unexported fields
}

TestRequest is a request harness intended for tests.

Values must be created with NewTestRequest or NewTestRequestWithPanic; the zero value is not usable.

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 starts with "Cache-Control: no-store" and records response body written by the test; the harness writes no body.

func NewTestRequest

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

NewTestRequest creates and serves a TestRequest for jw.

Passing nil for r uses a GET / request with no body.

Unexpected request-loop panics are re-raised on the loop goroutine. Use NewTestRequestWithPanic to capture an expected panic.

It panics if the request cannot be created or claimed. It requires the Jaws processing loop (jaws.Jaws.Serve or jaws.Jaws.ServeWithTimeout) to be running; if it is not, the underlying jaws.Jaws.TestServe panics.

Example
package main

import (
	"fmt"

	"github.com/linkdata/jaws"
	"github.com/linkdata/jaws/jawstest"
)

func main() {
	jw, err := jaws.New()
	if err != nil {
		panic(err)
	}
	defer jw.Close()
	go jw.Serve()

	tr := jawstest.NewTestRequest(jw, nil)
	if tr == nil {
		panic("request was not created")
	}
	<-tr.ReadyCh

	drained := make(chan struct{})
	go func() {
		defer close(drained)
		for {
			select {
			case <-tr.OutCh:
			case <-tr.DoneCh:
				return
			}
		}
	}()

	tr.Close()
	<-tr.DoneCh
	<-drained

	fmt.Println("stopped")
}
Output:
stopped

func NewTestRequestWithPanic added in v0.700.0

func NewTestRequestWithPanic(jw *jaws.Jaws, r *http.Request, onPanic func(recovered any)) *TestRequest

NewTestRequestWithPanic creates and serves a TestRequest with explicit loop-panic handling.

onPanic is called on the request-loop goroutine when it stops, with the recovered value or nil after a normal exit. DoneCh closes after onPanic returns or unwinds.

It otherwise behaves like NewTestRequest.

func (*TestRequest) BodyHTML

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

BodyHTML returns the recorded response body as trusted html/template.HTML.

The body is whatever the test itself rendered into TestRequest.Recorder, so it is treated as trusted; do not feed untrusted input through Recorder when the result is rendered as 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. Close is idempotent: calling it more than once is safe and closes InCh only once.

Jump to

Keyboard shortcuts

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