testing

package
v0.14.2 Latest Latest
Warning

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

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

Documentation

Overview

Package testing helps a test build an UploadedFile without a real browser: a fake file of a given size or with given content, a fake image, and the MIME type lookups uploads are validated against.

Where Fake lives, and why

Fake is here rather than on hesape/http.UploadedFile because testing.File embeds UploadedFile: this package imports hesape/http, so a Fake on the parent would import this one back and close a cycle. Go has no class hierarchy to hang a constructor off the child type, so it lands beside what it builds instead.

Index

Constants

View Source
const DefaultMimeType = "application/octet-stream"

DefaultMimeType is what Get answers when nothing is known about an extension.

Variables

This section is empty.

Functions

func From

func From(filename string) string

From is the type for a filename, read off its extension.

func Get

func Get(extension string) string

Get is the type for an extension, or DefaultMimeType.

func GetMimeTypes

func GetMimeTypes() map[string]string

GetMimeTypes is the extension-to-type table From and Get consult.

func Search(contentType string) string

Search is an extension for a type, empty when none is known.

Types

type File

type File struct {
	*hhttp.UploadedFile

	// Name is the name the fake announces.
	Name string
	// TempFile is the file on disk holding the bytes. It is removed by
	// [File.Close].
	TempFile *os.File
	// SizeToReport is the size [File.Size] set, in bytes.
	SizeToReport int64
	// MimeTypeToReport is the type [File.MimeType] set.
	MimeTypeToReport string
}

File is an UploadedFile a test made, with a size and a type it reports rather than ones a browser sent.

It embeds *hesape/http.UploadedFile, so everything an upload answers -- HashName, Extension, Get, StoreAs -- is the same method on the same file.

func Create

func Create(name string, kilobytes ...int) (*File, error)

Create is a fake file of the given size in kilobytes. For content instead of a size, use CreateWithContent. The variadic argument defaults to 0.

func CreateWithContent

func CreateWithContent(name, content string) (*File, error)

CreateWithContent is a fake file holding the given content.

func Image

func Image(name string, size ...int) (*File, error)

Image is a fake image of the given size. The variadic arguments default to 10 and 10.

func NewFile

func NewFile(name string, tempFile *os.File) *File

NewFile builds a fake around a name and a temporary file already holding the bytes.

func (*File) Close

func (f *File) Close() error

Close removes the temporary file. It is what a test defers, since nothing removes it automatically.

func (*File) GetMimeType

func (f *File) GetMimeType() string

GetMimeType is the type the fake reports, falling back to the one the name implies.

func (*File) GetSize

func (f *File) GetSize() int64

GetSize is the size the fake reports, falling back to the size on disk.

func (*File) MimeType

func (f *File) MimeType(contentType string) *File

MimeType sets the type the fake reports.

func (*File) Size

func (f *File) Size(kilobytes int) *File

Size sets the size the fake reports, in kilobytes.

type FileFactory

type FileFactory struct{}

FileFactory is the three ways to make a fake upload.

func Fake

func Fake() *FileFactory

Fake begins creating a new file fake.

It is in this package and not on hesape/http.UploadedFile because File embeds UploadedFile: this sub-package imports the parent, so a Fake on the parent would import the sub-package back and close a cycle.

func NewFileFactory

func NewFileFactory() *FileFactory

NewFileFactory builds a FileFactory. See the package doc for why the entry point (Fake) is here and not on hesape/http.UploadedFile.

func (*FileFactory) Create

func (f *FileFactory) Create(name string, args ...int) (*File, error)

Create is a fake file of the given size in kilobytes. The variadic argument defaults to 0.

Returns (*File, error): making a temporary file can fail.

func (*FileFactory) CreateWithContent

func (f *FileFactory) CreateWithContent(name, content string) (*File, error)

CreateWithContent is a fake file holding the given bytes, reporting their length as its size.

func (*FileFactory) CreateWithMimeType

func (f *FileFactory) CreateWithMimeType(name string, kilobytes int, contentType string) (*File, error)

CreateWithMimeType is Create with the type the fake reports also set. It is a separate method because Go has no optional argument that can be skipped over.

func (*FileFactory) Image

func (f *FileFactory) Image(name string, size ...int) (*File, error)

Image is a fake image of the given size, encoded in the format the name's extension names.

PNG, JPEG and GIF are what image/png, image/jpeg and image/gif encode; anything else falls back to JPEG.

The variadic arguments are width and height, defaulting to 10 and 10.

Jump to

Keyboard shortcuts

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