github.com/astaxie/beego@v1.12.3/testing/client.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testing 16 17 import ( 18 "github.com/astaxie/beego/config" 19 "github.com/astaxie/beego/httplib" 20 ) 21 22 var port = "" 23 var baseURL = "http://localhost:" 24 25 // TestHTTPRequest beego test request client 26 type TestHTTPRequest struct { 27 httplib.BeegoHTTPRequest 28 } 29 30 func getPort() string { 31 if port == "" { 32 config, err := config.NewConfig("ini", "../conf/app.conf") 33 if err != nil { 34 return "8080" 35 } 36 port = config.String("httpport") 37 return port 38 } 39 return port 40 } 41 42 // Get returns test client in GET method 43 func Get(path string) *TestHTTPRequest { 44 return &TestHTTPRequest{*httplib.Get(baseURL + getPort() + path)} 45 } 46 47 // Post returns test client in POST method 48 func Post(path string) *TestHTTPRequest { 49 return &TestHTTPRequest{*httplib.Post(baseURL + getPort() + path)} 50 } 51 52 // Put returns test client in PUT method 53 func Put(path string) *TestHTTPRequest { 54 return &TestHTTPRequest{*httplib.Put(baseURL + getPort() + path)} 55 } 56 57 // Delete returns test client in DELETE method 58 func Delete(path string) *TestHTTPRequest { 59 return &TestHTTPRequest{*httplib.Delete(baseURL + getPort() + path)} 60 } 61 62 // Head returns test client in HEAD method 63 func Head(path string) *TestHTTPRequest { 64 return &TestHTTPRequest{*httplib.Head(baseURL + getPort() + path)} 65 }