2018-11-17 08:28:50 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-04-08 13:24:06 +02:00
|
|
|
"strings"
|
2018-11-17 08:28:50 +01:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2018-11-17 08:28:50 +01:00
|
|
|
)
|
|
|
|
|
2020-04-08 13:24:06 +02:00
|
|
|
// NewApiTest returns new API test helper
|
2019-05-06 23:18:10 +02:00
|
|
|
func NewApiTest() (app *gin.Engine, router *gin.RouterGroup, conf *config.Config) {
|
|
|
|
conf = config.TestConfig()
|
2018-11-17 08:28:50 +01:00
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
app = gin.New()
|
|
|
|
router = app.Group("/api/v1")
|
2019-05-06 23:18:10 +02:00
|
|
|
return app, router, conf
|
2018-11-17 08:28:50 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 13:24:06 +02:00
|
|
|
// Performs API request with empty request body.
|
2018-11-18 19:18:19 +01:00
|
|
|
// See https://medium.com/@craigchilds94/testing-gin-json-responses-1f258ce3b0b1
|
|
|
|
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
2018-11-17 08:28:50 +01:00
|
|
|
req, _ := http.NewRequest(method, path, nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
return w
|
2018-11-17 12:29:01 +01:00
|
|
|
}
|
2020-04-08 13:24:06 +02:00
|
|
|
|
|
|
|
// Performs API request including request body as string.
|
|
|
|
func PerformRequestWithBody(r http.Handler, method, path, body string) *httptest.ResponseRecorder {
|
|
|
|
reader := strings.NewReader(body)
|
|
|
|
req, _ := http.NewRequest(method, path, reader)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
return w
|
|
|
|
}
|