Testing

laravel-go-captcha Alpha Laravel for Go

The captchatest package helps your application's tests get past a captcha without reading the image.

Turn the captcha off

client := laraveltest.New(t, options, captchatest.Disable())

Every rule passes and the routes aren't registered, in that test application only.

Answer it

To test the real flow, record the answers the package issues. A test application runs in one process, so it can use the memory cache with CAPTCHA_SINGLE_PROCESS=true:

pkg := captcha.New()
rec := captchatest.Record(pkg)
client := laraveltest.New(t, laravel.Options{Packages: []laravel.Package{pkg}},
	laraveltest.Env(map[string]string{"CAPTCHA_SINGLE_PROCESS": "true"}))

client.Get("/captcha/default").AssertOK()
client.Post("/contact", url.Values{
	"message": {"Hello"},
	"captcha": {rec.Answer(t, "default")},
}).AssertRedirect("/thanks")
  • rec.Answer(t, profile) returns the answer of the latest session image for that profile.
  • rec.AnswerFor(t, key) returns the answer of a keyed challenge.

Build a new package value for each test application. A recorder only sees answers issued by its own package value, so parallel tests can't read each other's answers. Production code never keeps answers.