docs: improve webtest
continuous-integration/drone the build failed
Details
continuous-integration/drone the build failed
Details
This commit is contained in:
parent
d89da311b1
commit
dbedcfa8f6
|
@ -17,6 +17,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// DBConnection - url to database on setting up default WebService for webtest
|
||||||
DBConnection = "user=root password=root dbname=defaultdb host=localhost port=26257 sslmode=disable"
|
DBConnection = "user=root password=root dbname=defaultdb host=localhost port=26257 sslmode=disable"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -28,11 +29,13 @@ type testServer struct {
|
||||||
lastCookies []*http.Cookie
|
lastCookies []*http.Cookie
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Login Request format (maybe just internal usage)
|
||||||
type Login struct {
|
type Login struct {
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New starts WebService for testing
|
||||||
func New(assert *assert.Assertions) *testServer {
|
func New(assert *assert.Assertions) *testServer {
|
||||||
// db setup
|
// db setup
|
||||||
dbConfig := database.Database{
|
dbConfig := database.Database{
|
||||||
|
@ -68,33 +71,37 @@ func New(assert *assert.Assertions) *testServer {
|
||||||
assert: assert,
|
assert: assert,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (this *testServer) DatabaseMigration(f func(db *database.Database)) {
|
|
||||||
f(this.db)
|
// DatabaseMigration set up a migration on webtest WebService
|
||||||
this.db.MigrateTestdata()
|
func (s *testServer) DatabaseMigration(f func(db *database.Database)) {
|
||||||
|
f(s.db)
|
||||||
|
s.db.MigrateTestdata()
|
||||||
}
|
}
|
||||||
func (this *testServer) Request(method, url string, body interface{}, expectCode int, jsonObj interface{}) {
|
|
||||||
|
// Request sends a request to webtest WebService
|
||||||
|
func (s *testServer) Request(method, url string, body interface{}, expectCode int, jsonObj interface{}) {
|
||||||
var jsonBody io.Reader
|
var jsonBody io.Reader
|
||||||
if body != nil {
|
if body != nil {
|
||||||
if strBody, ok := body.(string); ok {
|
if strBody, ok := body.(string); ok {
|
||||||
jsonBody = strings.NewReader(strBody)
|
jsonBody = strings.NewReader(strBody)
|
||||||
} else {
|
} else {
|
||||||
jsonBodyArray, err := json.Marshal(body)
|
jsonBodyArray, err := json.Marshal(body)
|
||||||
this.assert.Nil(err, "no request created")
|
s.assert.Nil(err, "no request created")
|
||||||
jsonBody = bytes.NewBuffer(jsonBodyArray)
|
jsonBody = bytes.NewBuffer(jsonBodyArray)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest(method, url, jsonBody)
|
req, err := http.NewRequest(method, url, jsonBody)
|
||||||
this.assert.Nil(err, "no request created")
|
s.assert.Nil(err, "no request created")
|
||||||
if len(this.lastCookies) > 0 {
|
if len(s.lastCookies) > 0 {
|
||||||
for _, c := range this.lastCookies {
|
for _, c := range s.lastCookies {
|
||||||
req.AddCookie(c)
|
req.AddCookie(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
this.gin.ServeHTTP(w, req)
|
s.gin.ServeHTTP(w, req)
|
||||||
|
|
||||||
// valid statusCode
|
// valid statusCode
|
||||||
this.assert.Equal(expectCode, w.Code, "expected http status code")
|
s.assert.Equal(expectCode, w.Code, "expected http status code")
|
||||||
if expectCode != w.Code {
|
if expectCode != w.Code {
|
||||||
fmt.Printf("wrong status code, body:%v\n", w.Body)
|
fmt.Printf("wrong status code, body:%v\n", w.Body)
|
||||||
return
|
return
|
||||||
|
@ -103,14 +110,14 @@ func (this *testServer) Request(method, url string, body interface{}, expectCode
|
||||||
if jsonObj != nil {
|
if jsonObj != nil {
|
||||||
// fetch JSON
|
// fetch JSON
|
||||||
err = json.NewDecoder(w.Body).Decode(jsonObj)
|
err = json.NewDecoder(w.Body).Decode(jsonObj)
|
||||||
this.assert.Nil(err, "decode json")
|
s.assert.Nil(err, "decode json")
|
||||||
}
|
}
|
||||||
|
|
||||||
result := w.Result()
|
result := w.Result()
|
||||||
if result != nil {
|
if result != nil {
|
||||||
cookies := result.Cookies()
|
cookies := result.Cookies()
|
||||||
if len(cookies) > 0 {
|
if len(cookies) > 0 {
|
||||||
this.lastCookies = cookies
|
s.lastCookies = cookies
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue