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
- func From(filename string) string
- func Get(extension string) string
- func GetMimeTypes() map[string]string
- func Search(contentType string) string
- type File
- type FileFactory
- func (f *FileFactory) Create(name string, args ...int) (*File, error)
- func (f *FileFactory) CreateWithContent(name, content string) (*File, error)
- func (f *FileFactory) CreateWithMimeType(name string, kilobytes int, contentType string) (*File, error)
- func (f *FileFactory) Image(name string, size ...int) (*File, error)
Constants ¶
const DefaultMimeType = "application/octet-stream"
DefaultMimeType is what Get answers when nothing is known about an extension.
Variables ¶
This section is empty.
Functions ¶
func GetMimeTypes ¶
GetMimeTypes is the extension-to-type table From and Get consult.
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 ¶
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 ¶
CreateWithContent is a fake file holding the given content.
func (*File) Close ¶
Close removes the temporary file. It is what a test defers, since nothing removes it automatically.
func (*File) GetMimeType ¶
GetMimeType is the type the fake reports, falling back to the one the name implies.
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.