diff --git a/doc_test.go b/doc_test.go index d47e08a..c7efccb 100644 --- a/doc_test.go +++ b/doc_test.go @@ -3,6 +3,7 @@ package frisby_test import ( "fmt" "reflect" + "testing" "github.com/bitly/go-simplejson" "github.com/verdverm/frisby" @@ -10,6 +11,16 @@ import ( 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() { diff --git a/frisby.go b/frisby.go index 422be12..32ab854 100644 --- a/frisby.go +++ b/frisby.go @@ -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 } diff --git a/global.go b/global.go index 9f5e640..f80f1b6 100644 --- a/global.go +++ b/global.go @@ -13,6 +13,8 @@ type global_data struct { Req *request.Request Errs map[string][]error + BaseURL string + NumRequest int NumAsserts int NumErrored int @@ -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 @@ -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 +} \ No newline at end of file