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

添加 BaseURL 支持 #28

Open
wants to merge 1 commit 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
11 changes: 11 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@ package frisby_test
import (
"fmt"
"reflect"
"testing"

"github.com/bitly/go-simplejson"
"github.com/verdverm/frisby"
)

func init() {
frisby.Global.PrintProgressDot = false
frisby.Global.SetBaseURL("https://gocn.io/")
}

func TestBaseURL(t *testing.T) {
frisby.Create("Test GET Go homepage").
Get("question/265").
Send().
ExpectStatus(200).
ExpectContent("case").
PrintReport()
}

func ExampleFrisby_Get() {
Expand Down
22 changes: 15 additions & 7 deletions frisby.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,52 +51,60 @@ func Create(name string) *Frisby {
return F
}

func getURL(url string) string {
if Global.BaseURL == "" {
return url
} else {
return Global.BaseURL + url
}
}

// Set the HTTP method to GET for the given URL
func (F *Frisby) Get(url string) *Frisby {
F.Method = "GET"
F.Url = url
F.Url = getURL(url)
return F
}

// Set the HTTP method to POST for the given URL
func (F *Frisby) Post(url string) *Frisby {
F.Method = "POST"
F.Url = url
F.Url = getURL(url)
return F
}

// Set the HTTP method to PUT for the given URL
func (F *Frisby) Put(url string) *Frisby {
F.Method = "PUT"
F.Url = url
F.Url = getURL(url)
return F
}

// Set the HTTP method to PATCH for the given URL
func (F *Frisby) Patch(url string) *Frisby {
F.Method = "PATCH"
F.Url = url
F.Url = getURL(url)
return F
}

// Set the HTTP method to DELETE for the given URL
func (F *Frisby) Delete(url string) *Frisby {
F.Method = "DELETE"
F.Url = url
F.Url = getURL(url)
return F
}

// Set the HTTP method to HEAD for the given URL
func (F *Frisby) Head(url string) *Frisby {
F.Method = "HEAD"
F.Url = url
F.Url = getURL(url)
return F
}

// Set the HTTP method to OPTIONS for the given URL
func (F *Frisby) Options(url string) *Frisby {
F.Method = "OPTIONS"
F.Url = url
F.Url = getURL(url)
return F
}

Expand Down
8 changes: 8 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type global_data struct {
Req *request.Request
Errs map[string][]error

BaseURL string

NumRequest int
NumAsserts int
NumErrored int
Expand All @@ -30,6 +32,7 @@ func init() {
Global.Errs = make(map[string][]error, 0)
Global.PrintProgressDot = true
Global.PathSeparator = DefaultPathSeparator
Global.BaseURL = ""
}

// Set BasicAuth values for the coming request
Expand Down Expand Up @@ -177,3 +180,8 @@ func (G *global_data) PrintReport() *global_data {

return G
}

func (G *global_data) SetBaseURL(url string) *global_data {
G.BaseURL = url
return G
}