1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-18 05:10:54 +00:00
Beego/testing/client.go

66 lines
1.8 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-08-21 14:47:58 +00:00
package testing
import (
2013-11-06 08:51:33 +00:00
"github.com/astaxie/beego/config"
2013-08-22 06:27:23 +00:00
"github.com/astaxie/beego/httplib"
2013-08-21 14:47:58 +00:00
)
var port = ""
var baseUrl = "http://localhost:"
// beego test request client
2013-11-06 08:12:10 +00:00
type TestHttpRequest struct {
2013-08-21 14:47:58 +00:00
httplib.BeegoHttpRequest
}
2013-11-06 08:12:10 +00:00
func getPort() string {
if port == "" {
2013-11-06 08:51:33 +00:00
config, err := config.NewConfig("ini", "../conf/app.conf")
2013-11-06 08:12:10 +00:00
if err != nil {
2013-08-21 14:47:58 +00:00
return "8080"
}
2013-11-06 08:12:10 +00:00
port = config.String("httpport")
2013-08-21 14:47:58 +00:00
return port
}
return port
}
// returns test client in GET method
2013-08-21 14:47:58 +00:00
func Get(path string) *TestHttpRequest {
2013-11-06 08:12:10 +00:00
return &TestHttpRequest{*httplib.Get(baseUrl + getPort() + path)}
2013-08-21 14:47:58 +00:00
}
// returns test client in POST method
2013-08-21 14:47:58 +00:00
func Post(path string) *TestHttpRequest {
2013-11-06 08:12:10 +00:00
return &TestHttpRequest{*httplib.Post(baseUrl + getPort() + path)}
2013-08-21 14:47:58 +00:00
}
// returns test client in PUT method
2013-08-21 14:47:58 +00:00
func Put(path string) *TestHttpRequest {
2013-11-06 08:12:10 +00:00
return &TestHttpRequest{*httplib.Put(baseUrl + getPort() + path)}
2013-08-21 14:47:58 +00:00
}
// returns test client in DELETE method
2013-08-21 14:47:58 +00:00
func Delete(path string) *TestHttpRequest {
2013-11-06 08:12:10 +00:00
return &TestHttpRequest{*httplib.Delete(baseUrl + getPort() + path)}
2013-08-21 14:47:58 +00:00
}
// returns test client in HEAD method
2013-08-21 14:47:58 +00:00
func Head(path string) *TestHttpRequest {
2013-11-06 08:12:10 +00:00
return &TestHttpRequest{*httplib.Head(baseUrl + getPort() + path)}
2013-08-21 14:47:58 +00:00
}