gosubmit

package module
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2022 License: MIT Imports: 16 Imported by: 0

README

gosubmit

Build Status

Description

Docs are available here: https://godoc.org/github.com/jeremija/gosubmit

Helps filling out plain html forms during testing. Will automatically take the existing values from the form so there is no need to manually set things like csrf tokens. Alerts about missing required fields, or when pattern validation does not match. See example_test.go for a full example.

package gosubmit_test

import (
	// TODO import app
	. "github.com/jeremija/gosubmit"

	"net/http"
	"net/http/httptest"
)

func TestLogin(t *testing.T) {
	w := httptest.NewRecorder()
	r := httptest.NewRequest("GET", "/auth/login", nil)

	app.ServeHTTP(w, r)

	r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest(
		Set("username", "user"),
		Set("password", "password"),
	)

	if err != nil {
		t.Fatalf("Error filling form: %s", err)
	}

	w := httptest.NewRecorder()
	app.ServeHTTP(w, r)

	if code := w.Result().StatusCode; code != http.StatusOK {
		t.Errorf("Expected status ok but got %d", code)
	}
}

Autofilling of all required input fields is supported:

r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest(
	Autofill(),
)

Elements that include a pattern attribute for validation will not be autofilled and have to be filled in manually. For example:

r, err := ParseResponse(w.Result(), r.URL).FirstForm().NewTestRequest(
	Autofill(),
	Set("validatedURL", "https://www.example.com"),
)

Testing Helpers

To avoid checking for error in tests manually when creating a new test request , the value of t *testing.T can be provided:

r := ParseResponse(w.Result(), r.URL).FirstForm().Testing(t).NewTestRequest(
	Autofill(),
	Set("validatedURL", "https://www.example.com"),
)

In case of any errors, the t.Fatalf() function will be called. t.Helper() is used appropriately to ensure line numbers reported by go test are correct.

Supported Elements

  • input[type=checkbox]
  • input[type=date]
  • input[type=email]
  • input[type=hidden]
  • input[type=number]
  • input[type=radio]
  • input[type=text]
  • input[type=url]
  • textarea
  • select
  • select[multiple]
  • button[type=submit] with name and value
  • input[type=submit] with name and value

If an input element is not on this list, it will default to text input.

Who Is Using gosubmit?

License

MIT

Documentation

Index

Constants

View Source
const (
	ElementSelect   = "select"
	ElementInput    = "input"
	ElementButton   = "button"
	ElementTextArea = "textarea"

	InputTypeText     = "text"
	InputTypeFile     = "file"
	InputTypeCheckbox = "checkbox"
	InputTypeRadio    = "radio"
	InputTypeHidden   = "hidden"
	InputTypeSubmit   = "submit"
	InputTypeEmail    = "email"
	InputTypeURL      = "url"
	InputTypeDate     = "date"
	InputTypeNumber   = "number"
)
View Source
const (
	AutoFillEmail = "test@example.com"
	AutoFillURL   = "https://www.example.com"
	ISO8601Date   = "2006-01-02"
	AutoFillDate  = ISO8601Date
)
View Source
const ContentTypeForm = "application/x-www-form-urlencoded"
View Source
const ContentTypeMultipart = "multipart/form-data"

Variables

View Source
var AutoFillFile = []byte{0xd, 0xe, 0xa, 0xd, 0xb, 0xe, 0xe, 0xf}
View Source
var PatternEmail = regexp.MustCompile("[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$")
View Source
var PatternURL = regexp.MustCompile("^https?://.+")

Functions

This section is empty.

Types

type Button

type Button struct {
	Name  string
	Value string
}

type Checkbox

type Checkbox struct {
	// contains filtered or unexported fields
}

func (Checkbox) AutoFill

func (i Checkbox) AutoFill() (values []string)

func (Checkbox) Fill

func (i Checkbox) Fill(val string) (value string, ok bool)

func (Checkbox) Multiple

func (i Checkbox) Multiple() bool

func (Checkbox) Options

func (i Checkbox) Options() []string

type DateInput

type DateInput struct {
	// contains filtered or unexported fields
}

func (DateInput) AutoFill

func (i DateInput) AutoFill() []string

func (DateInput) Fill

func (i DateInput) Fill(val string) (value string, ok bool)

func (DateInput) Multipart

func (i DateInput) Multipart() bool

func (DateInput) Multiple

func (i DateInput) Multiple() bool

func (DateInput) Name

func (i DateInput) Name() string

func (DateInput) Options

func (i DateInput) Options() (values []string)

func (DateInput) Required

func (i DateInput) Required() bool

func (DateInput) Type

func (i DateInput) Type() string

func (DateInput) Value

func (i DateInput) Value() string

func (DateInput) Values

func (i DateInput) Values() []string

type Document

type Document struct {
	// contains filtered or unexported fields
}

func Parse

func Parse(r io.Reader) (doc Document)

Parse all forms in the HTML document.

func ParseResponse

func ParseResponse(r *http.Response, url *url.URL) Document

func ParseWithURL

func ParseWithURL(r io.Reader, defaultURL string) (doc Document)

Parse all formsr in the HTML document and set the default URL if <form action="..."> attribute is missing

func (*Document) Err

func (e *Document) Err() error

func (Document) FindForm

func (d Document) FindForm(attrKey string, attrValue string) (form Form)

func (Document) FindFormsByClass

func (d Document) FindFormsByClass(className string) (forms Forms)

func (Document) FirstForm

func (d Document) FirstForm() (form Form)

func (Document) Forms

func (d Document) Forms() Forms

type EmailInput

type EmailInput struct {
	TextInput
}

func (EmailInput) AutoFill

func (i EmailInput) AutoFill() []string

func (EmailInput) Multipart

func (i EmailInput) Multipart() bool

func (EmailInput) Multiple

func (i EmailInput) Multiple() bool

func (EmailInput) Name

func (i EmailInput) Name() string

func (EmailInput) Options

func (i EmailInput) Options() (values []string)

func (EmailInput) Required

func (i EmailInput) Required() bool

func (EmailInput) Type

func (i EmailInput) Type() string

func (EmailInput) Value

func (i EmailInput) Value() string

func (EmailInput) Values

func (i EmailInput) Values() []string

type FileInput

type FileInput struct {
	// contains filtered or unexported fields
}

func (FileInput) AutoFill

func (f FileInput) AutoFill() (values []string)

func (FileInput) Fill

func (f FileInput) Fill(val string) (value string, ok bool)

func (FileInput) Multipart

func (f FileInput) Multipart() bool

func (FileInput) Multiple

func (i FileInput) Multiple() bool

func (FileInput) Name

func (i FileInput) Name() string

func (FileInput) Options

func (i FileInput) Options() (values []string)

func (FileInput) Required

func (i FileInput) Required() bool

func (FileInput) Type

func (i FileInput) Type() string

func (FileInput) Value

func (i FileInput) Value() string

func (FileInput) Values

func (i FileInput) Values() []string

type Form

type Form struct {

	// All html attributes of the form. Used to find the form by attribute
	Attr      []html.Attribute
	ClassList []string
	// Value of Enctype attribute, default is application/x-www-form-urlencoded.
	// For forms with file uploads it should be multipart/form-data.
	ContentType string
	// All found inputs
	Inputs Inputs
	// Value of form method attribute
	Method string
	// Value form action attribute
	URL string
	// All found <button type="submit"> and <input type="submit"> elements.
	Buttons []Button
	// contains filtered or unexported fields
}

func (*Form) Err

func (e *Form) Err() error

func (Form) GetOptionsFor

func (f Form) GetOptionsFor(name string) (options []string)

Returns a list of available input values for elements with options (checkbox, radio or select).

func (Form) GetParams

func (f Form) GetParams(opts ...Option) (string, error)

Fills the form and returns query parameters for a GET request.

func (Form) IsRequired

func (f Form) IsRequired(name string) bool

Returns true if field is required, false otherwise.

func (Form) MultipartParams

func (f Form) MultipartParams(opts ...Option) (boundary string, data []byte, err error)

Fills the form and returns parameters for a multipart request. If there was any error in the parsing or if the form was filled incorrectly, it will return an error.

func (Form) NewRequest

func (f Form) NewRequest(opts ...Option) (*http.Request, error)

Fills the form and returns a new request. If there was any error in the parsing or if the form was filled incorrectly, it will return an error.

func (Form) NewTestRequest

func (f Form) NewTestRequest(opts ...Option) (*http.Request, error)

Fills the form and returns a new test request. If there was any error in the parsing or if the form was filled incorrectly, it will return an error.

func (Form) PostParams

func (f Form) PostParams(opts ...Option) ([]byte, error)

Fills the form and returns body for a POST request.

func (Form) Testing

func (f Form) Testing(t test) TestingForm

func (Form) Validate

func (f Form) Validate(opts ...Option) error

Fills the form and returns an error if there was an error. Useful for testing.

type Forms

type Forms []Form

func (Forms) First

func (forms Forms) First() (form Form)

func (Forms) Last

func (forms Forms) Last() (form Form)

type HiddenInput

type HiddenInput struct {
	// contains filtered or unexported fields
}

func (HiddenInput) AutoFill

func (i HiddenInput) AutoFill() (values []string)

func (HiddenInput) Fill

func (i HiddenInput) Fill(val string) (value string, ok bool)

func (HiddenInput) Multipart

func (i HiddenInput) Multipart() bool

func (HiddenInput) Multiple

func (i HiddenInput) Multiple() bool

func (HiddenInput) Name

func (i HiddenInput) Name() string

func (HiddenInput) Options

func (i HiddenInput) Options() (values []string)

func (HiddenInput) Required

func (i HiddenInput) Required() bool

func (HiddenInput) Type

func (i HiddenInput) Type() string

func (HiddenInput) Value

func (i HiddenInput) Value() string

func (HiddenInput) Values

func (i HiddenInput) Values() []string

type Input

type Input interface {
	Name() string
	Type() string
	Value() string
	Values() []string
	Options() []string
	Fill(val string) (value string, ok bool)
	Required() bool
	Multiple() bool
	Multipart() bool
	AutoFill() []string
}

type Inputs

type Inputs map[string]Input

type NumberInput

type NumberInput struct {
	// contains filtered or unexported fields
}

func (NumberInput) AutoFill

func (i NumberInput) AutoFill() []string

func (NumberInput) Fill

func (i NumberInput) Fill(val string) (value string, ok bool)

func (NumberInput) Multipart

func (i NumberInput) Multipart() bool

func (NumberInput) Multiple

func (i NumberInput) Multiple() bool

func (NumberInput) Name

func (i NumberInput) Name() string

func (NumberInput) Options

func (i NumberInput) Options() (values []string)

func (NumberInput) Required

func (i NumberInput) Required() bool

func (NumberInput) Type

func (i NumberInput) Type() string

func (NumberInput) Value

func (i NumberInput) Value() string

func (NumberInput) Values

func (i NumberInput) Values() []string

type Option

type Option func(f *filler) error

func Add

func Add(name string, value string) Option

Adds a name=value pair to the form. If there is an empty value it will be replaced, otherwise a second value will be added, but only if the element supports multiple values, like checkboxes or <select multiple> elements.

func AddFile

func AddFile(fieldname string, filename string, contents []byte) Option

Fill data for multipart request

func AutoFill

func AutoFill() Option

func Click

func Click(buttonValue string) Option

Adds the submit buttons name=value combination to the form submission. Useful when there are two or more buttons on a form and their values make a difference on how the server's going to process the form data.

func Reset

func Reset(name string) Option

Deletes a field from the form. Useful to remove preselected values

func Set

func Set(name string, value string) Option

Set a name=value pair to the form and replace any set value(s).

func UnsafeSet

func UnsafeSet(name string, value string) Option

Set a value without validation

func WithContext

func WithContext(ctx context.Context) Option

type Radio

type Radio struct {
	// contains filtered or unexported fields
}

func (Radio) AutoFill

func (i Radio) AutoFill() (values []string)

func (Radio) Fill

func (i Radio) Fill(val string) (value string, ok bool)

func (Radio) Multiple

func (i Radio) Multiple() bool

func (Radio) Options

func (i Radio) Options() []string

type Select

type Select struct {
	// contains filtered or unexported fields
}

func (Select) AutoFill

func (i Select) AutoFill() (values []string)

func (Select) Fill

func (i Select) Fill(val string) (value string, ok bool)

func (Select) Multiple

func (i Select) Multiple() bool

func (Select) Options

func (i Select) Options() []string

type TestingForm

type TestingForm struct {
	// contains filtered or unexported fields
}

func (TestingForm) NewTestRequest

func (f TestingForm) NewTestRequest(opts ...Option) *http.Request

type TextInput

type TextInput struct {
	// contains filtered or unexported fields
}

func (TextInput) AutoFill

func (i TextInput) AutoFill() (value []string)

func (TextInput) Fill

func (i TextInput) Fill(val string) (value string, ok bool)

func (TextInput) Multipart

func (i TextInput) Multipart() bool

func (TextInput) Multiple

func (i TextInput) Multiple() bool

func (TextInput) Name

func (i TextInput) Name() string

func (TextInput) Options

func (i TextInput) Options() (values []string)

func (TextInput) Required

func (i TextInput) Required() bool

func (TextInput) Type

func (i TextInput) Type() string

func (TextInput) Value

func (i TextInput) Value() string

func (TextInput) Values

func (i TextInput) Values() []string

type URLInput

type URLInput struct {
	TextInput
}

func (URLInput) AutoFill

func (i URLInput) AutoFill() []string

func (URLInput) Multipart

func (i URLInput) Multipart() bool

func (URLInput) Multiple

func (i URLInput) Multiple() bool

func (URLInput) Name

func (i URLInput) Name() string

func (URLInput) Options

func (i URLInput) Options() (values []string)

func (URLInput) Required

func (i URLInput) Required() bool

func (URLInput) Type

func (i URLInput) Type() string

func (URLInput) Value

func (i URLInput) Value() string

func (URLInput) Values

func (i URLInput) Values() []string

Jump to

Keyboard shortcuts

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