Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/genral expect function using closure #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import (
"github.com/bitly/go-simplejson"
)

// ExpectFunc function type used as argument to Expect()
type ExpectFunc func(F *Frisby) (bool, string)

// Expect Checks according to the given function, which allows you to describe any kind of assertion.
func (F *Frisby) Expect(foo ExpectFunc) *Frisby {
Global.NumAsserts++
if ok, err_str := foo(F); !ok {
F.AddError(err_str)
}
return F
}

// Checks the response status code
func (F *Frisby) ExpectStatus(code int) *Frisby {
Global.NumAsserts++
Expand Down
13 changes: 11 additions & 2 deletions frisby.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"

"github.com/mozillazg/request"
)

var Global global_data

const defaultFileKey = "file"

type Frisby struct {
Name string
Url string
Expand Down Expand Up @@ -191,12 +194,18 @@ func (F *Frisby) SetJson(json interface{}) *Frisby {
}

// Add a file to the Form data for the coming request
func (F *Frisby) AddFile(filename string) *Frisby {
func (F *Frisby) AddFile(key, filename string) *Frisby {
file, err := os.Open(filename)
if err != nil {
F.Errs = append(F.Errs, err)
} else {
fileField := request.FileField{"file", filename, file}
if len(key) == 0 {
key = defaultFileKey
}
fileField := request.FileField{
FieldName: key,
FileName: filepath.Base(filename),
File: file}
F.Req.Files = append(F.Req.Files, fileField)
}
return F
Expand Down