fakehttpfs

package module
v0.0.0-...-beddaa4 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2014 License: MIT Imports: 8 Imported by: 0

README

fakehttpfs

fakehttpfs is a go test fake filesystem implementing the net/http Filesystem interface. It is designed to make HTTP/filesystem related tests easier to read and write.

Usage

Run go get github.com/paulhammond/fakehttpfs to install.

Create a fake filesystem using the fakehttpfs.FileSystem function:

testFS := fakehttpfs.FileSystem(
        fakehttpfs.File("/robots.txt", "User-agent: *\nDisallow: /"),
        fakehttpfs.Dir("/misc",
                fakehttpfs.File("hello.txt", "Hello")
        )
);

file, err = testFS.Open("/robots.txt")
file, err = testFS.Open("/misc/file.txt")

Full documentation is available through godoc.

License

MIT license, see LICENSE.txt for details.

Documentation

Overview

package fakehttpfs provides a fake filesystem that implements the http.FileSystem interface.

To use it call the FileSystem function with one or more http.File objects. The File and Dir helper functions create files and directories respectively. For example:

testFS := fakehttpfs.FileSystem(
        fakehttpfs.File("/robots.txt", "User-agent: *\nDisallow: /"),
        fakehttpfs.Dir("/misc",
                fakehttpfs.File("hello.txt", "Hello", os.FileMode(0600))
                os.Open("/path/to/some/real/file.txt")
        )
);

file, err := testFS.Open("/robots.txt")
file, err := testFS.Open("/misc/file.txt")

Subdirectories need to be created with the Dir helper, but you do not have to use the File helper. If you'd like to write your own mock or stub or even use a real file you can.

The fake filesystem is not even slightly safe for concurrent use. By design concurrent calls to Open() with the same path will return the same file value (allowing you to test equality in your tests if needed). As a result the values will also share the same underlying io.ReadSeeker. Concurrent calls to file.Read() will give unpredictable results.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dir

func Dir(name string, files ...http.File) http.File

Creates a test fake directory containing the files.

func File

func File(name, contents string, options ...interface{}) http.File

Creates a test fake file with string contents. If options includes a time.Time it is used to specify the file modification time, if not the zero time is used. Likewise, if options includes an os.FileMode it is used to specify the file mode, if not 0644 is used.

func FileSystem

func FileSystem(files ...http.File) http.FileSystem

Creates a test fake filesystem containing the files.

Types

This section is empty.

Jump to

Keyboard shortcuts

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