quintodrome/api/api_test.go

48 lines
1.2 KiB
Go
Raw Normal View History

2016-02-27 14:42:08 -09:00
package api_test
2016-02-24 14:24:06 -09:00
import (
"fmt"
"net/http"
2016-02-25 14:52:07 -09:00
"net/http/httptest"
2016-03-01 12:05:49 -09:00
"strings"
"github.com/astaxie/beego"
2017-04-01 05:47:14 -08:00
_ "github.com/cloudsonic/sonic-server/init"
2016-02-24 14:24:06 -09:00
)
const (
2016-03-02 09:18:39 -09:00
testUser = "deluan"
testPassword = "wordpass"
2016-03-02 09:18:39 -09:00
testClient = "test"
testVersion = "1.0.0"
)
2016-03-01 12:05:49 -09:00
func AddParams(endpoint string, params ...string) string {
url := fmt.Sprintf("%s?u=%s&p=%s&c=%s&v=%s&f=json", endpoint, testUser, testPassword, testClient, testVersion)
2016-03-01 12:05:49 -09:00
if len(params) > 0 {
url = url + "&" + strings.Join(params, "&")
}
return url
}
2016-02-24 14:33:13 -09:00
func Get(url string, testCase string) (*http.Request, *httptest.ResponseRecorder) {
2016-02-24 14:24:06 -09:00
r, _ := http.NewRequest("GET", url, nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
2016-02-25 07:14:16 -09:00
beego.Debug("testing", testCase, fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
2016-02-24 14:24:06 -09:00
return r, w
}
func GetWithHeader(url string, header, value, testCase string) (*http.Request, *httptest.ResponseRecorder) {
r, _ := http.NewRequest("GET", url, nil)
r.Header.Add(header, value)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Debug("testing", testCase, fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
return r, w
}