Documentation ¶
Index ¶
- func NewTestClient(fn RoundTripFunc) *http.Client
- func TestTools_CreateDirIfNotExist(t *testing.T)
- func TestTools_DownloadStaticFile(t *testing.T)
- func TestTools_ErrorJSON(t *testing.T)
- func TestTools_PushJSONToRemote(t *testing.T)
- func TestTools_RandomString(t *testing.T)
- func TestTools_ReadJSONAndMarshal(t *testing.T)
- func TestTools_Slugify(t *testing.T)
- func TestTools_UploadFiles(t *testing.T)
- func TestTools_UploadOneFile(t *testing.T)
- func TestTools_WriteJSON(t *testing.T)
- func Test_ReadJSON(t *testing.T)
- type JSONResponse
- type RoundTripFunc
- type Tools
- func (t *Tools) CreateDirIfNotExist(path string) error
- func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, p, file, displayName string)
- func (t *Tools) ErrorJSON(w http.ResponseWriter, err error, status ...int) error
- func (t *Tools) PushJSONToRemote(uri string, data interface{}, client ...*http.Client) (*http.Response, int, error)
- func (t *Tools) RandomString(n int) string
- func (t *Tools) ReadJSON(w http.ResponseWriter, r *http.Request, data interface{}) error
- func (t *Tools) Slugify(s string) (string, error)
- func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error)
- func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error)
- func (t *Tools) WriteJSON(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error
- type UploadedFile
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewTestClient ¶
func NewTestClient(fn RoundTripFunc) *http.Client
NewTestClient returns *http.Client with Transport replaced to avoid making real calls
func TestTools_ErrorJSON ¶
func TestTools_RandomString ¶
func TestTools_Slugify ¶
func TestTools_UploadFiles ¶
func TestTools_UploadOneFile ¶
func TestTools_WriteJSON ¶
func Test_ReadJSON ¶
Types ¶
type JSONResponse ¶
type JSONResponse struct { Error bool `json:"error"` Message string `json:"message"` Data interface{} `json:"data,omitempty"` }
JSONResponse is the type used for sending JSON around.
type RoundTripFunc ¶
RoundTripFunc is used to satisfy the interface requirements for http.Client
type Tools ¶
type Tools struct { MaxJSONSize int // maximum size of JSON file we'll process MaxFileSize int // maximum size of uploaded files in bytes AllowedFileTypes []string // allowed file types for upload (e.g. image/jpeg) AllowUnknownFields bool // if set to true, allow unknown fields in JSON }
Tools is the type for this package. Create a variable of this type, and you have access to all the exported methods with the receiver type *Tools.
func (*Tools) CreateDirIfNotExist ¶
CreateDirIfNotExist creates a directory, and all necessary parent directories, if it does not exist.
func (*Tools) DownloadStaticFile ¶
func (t *Tools) DownloadStaticFile(w http.ResponseWriter, r *http.Request, p, file, displayName string)
DownloadStaticFile downloads a file, and tries to force the browser to avoid displaying it in the browser window by setting content-disposition. It also allows specification of the display name.
func (*Tools) ErrorJSON ¶
ErrorJSON takes an error, and optionally a response status code, and generates and sends a json error response.
func (*Tools) PushJSONToRemote ¶
func (t *Tools) PushJSONToRemote(uri string, data interface{}, client ...*http.Client) (*http.Response, int, error)
PushJSONToRemote posts arbitrary json to some url, and returns the response, the response status code, and error, if any. The final parameter, client, is optional, and will default to the standard http.Client. It exists to make testing possible without an active remote url.
func (*Tools) RandomString ¶
RandomString returns a random string of letters of length n
func (*Tools) UploadFiles ¶
func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error)
UploadFiles uploads one or more file to a specified directory, and gives the files a random name. It returns a slice containing the newly named files, the original file names, the size of the files, and potentially an error. If the optional last parameter is set to true, then we will not rename the files, but will use the original file names.
func (*Tools) UploadOneFile ¶
func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error)
UploadOneFile is just a convenience method that calls UploadFiles, but expects only one file to be in the upload.
type UploadedFile ¶
UploadedFile is a struct used for the uploaded file