geminitest

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 5 Imported by: 0

README

geminitest

Easily test gemini handlers, modeled after go httptest.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRequest

func NewRequest(rawurl string) *gemini.Request

NewRequest returns a new incoming server Request, suitable for passing to an gemini.Handler for testing.

NewRequest panics on error for ease of use in testing, where a panic is acceptable.

Types

type ResponseRecorder

type ResponseRecorder struct {
	// Status is the response status code set by WriteHeader.
	Status gemini.Status

	// Meta returns the response meta.
	// For successful responses, the meta should contain the media type of the response.
	// For failure responses, the meta should contain a short description of the failure.
	Meta string

	// Body is the buffer to which the Handler's Write calls are sent.
	// If nil, the Writes are silently discarded.
	Body *bytes.Buffer

	// Flushed is whether the Handler called Flush.
	Flushed bool

	// The MediaType set by SetMediaType
	MediaType string
	// contains filtered or unexported fields
}

ResponseRecorder is an implementation of gemini.ResponseWriter that records its mutations for later inspection in tests.

Example
package main

import (
	geminitest "codeberg.org/iamruinous/geminitest"
	"context"
	"fmt"

	gemini "git.sr.ht/~adnano/go-gemini"
	"io"
)

func main() {
	handler := func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
		io.WriteString(w, "# Hello World!")
	}

	req := geminitest.NewRequest("gemini://example.com/foo")
	w := geminitest.NewRecorder()
	ctx := context.Background()
	handler(ctx, w, req)

	resp := w.Result()
	body, _ := io.ReadAll(resp.Body)

	fmt.Println(int(resp.Status))
	fmt.Println(string(body))

}
Output:

20
# Hello World!

func NewRecorder

func NewRecorder() *ResponseRecorder

NewRecorder returns an initialized ResponseRecorder.

func (*ResponseRecorder) Flush

func (rw *ResponseRecorder) Flush() error

Flush implements gemini.Flusher. To test whether Flush was called, see rw.Flushed.

func (*ResponseRecorder) Result

func (rw *ResponseRecorder) Result() *gemini.Response

Result returns the response generated by the handler.

The returned Response will have at least its Status, MediaType and Body populated. More fields may be populated in the future, so callers should not DeepEqual the result in tests.

The Response.Body is guaranteed to be non-nil and Body.Read call is guaranteed to not return any error other than io.EOF.

Result must only be called after the handler has finished running.

func (*ResponseRecorder) SetMediaType

func (rw *ResponseRecorder) SetMediaType(mediatype string)

SetMediaType implements gemini.ResponseWriter.

func (*ResponseRecorder) Write

func (rw *ResponseRecorder) Write(buf []byte) (int, error)

Write implements gemini.ResponseWriter. The data in buf is written to rw.Body, if not nil.

func (*ResponseRecorder) WriteHeader

func (rw *ResponseRecorder) WriteHeader(status gemini.Status, meta string)

WriteHeader implements gemini.ResponseWriter.

Jump to

Keyboard shortcuts

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