2018-11-17 07:28:50 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-05-08 16:35:19 +00:00
|
|
|
"os"
|
2020-04-08 11:24:06 +00:00
|
|
|
"strings"
|
2020-05-08 16:35:19 +00:00
|
|
|
"testing"
|
2018-11-17 07:28:50 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-02 19:30:50 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2019-05-06 21:18:10 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2022-09-28 07:01:17 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2022-10-15 19:54:11 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2018-11-17 07:28:50 +00:00
|
|
|
)
|
|
|
|
|
2023-01-28 19:56:57 +00:00
|
|
|
type CloseableResponseRecorder struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
closeCh chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CloseableResponseRecorder) CloseNotify() <-chan bool {
|
|
|
|
return r.closeCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CloseableResponseRecorder) closeClient() {
|
|
|
|
r.closeCh <- true
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:01:17 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
log = logrus.StandardLogger()
|
|
|
|
log.SetLevel(logrus.TraceLevel)
|
|
|
|
event.AuditLog = log
|
|
|
|
|
2022-09-29 22:42:19 +00:00
|
|
|
c := config.TestConfig()
|
2022-10-15 19:54:11 +00:00
|
|
|
get.SetConfig(c)
|
2022-09-28 07:01:17 +00:00
|
|
|
|
|
|
|
code := m.Run()
|
|
|
|
|
|
|
|
_ = c.CloseDb()
|
|
|
|
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2020-12-18 12:05:48 +00:00
|
|
|
// NewApiTest returns new API test helper.
|
2019-05-06 21:18:10 +00:00
|
|
|
func NewApiTest() (app *gin.Engine, router *gin.RouterGroup, conf *config.Config) {
|
2018-11-17 07:28:50 +00:00
|
|
|
gin.SetMode(gin.TestMode)
|
2022-09-28 07:01:17 +00:00
|
|
|
|
2018-11-17 07:28:50 +00:00
|
|
|
app = gin.New()
|
|
|
|
router = app.Group("/api/v1")
|
|
|
|
|
2022-10-15 19:54:11 +00:00
|
|
|
return app, router, get.Config()
|
2020-12-18 12:05:48 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 15:55:40 +00:00
|
|
|
// Executes an API request with an empty request body.
|
2018-11-18 18:18:19 +00:00
|
|
|
// See https://medium.com/@craigchilds94/testing-gin-json-responses-1f258ce3b0b1
|
|
|
|
func PerformRequest(r http.Handler, method, path string) *httptest.ResponseRecorder {
|
2018-11-17 07:28:50 +00:00
|
|
|
req, _ := http.NewRequest(method, path, nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
2020-04-08 11:24:06 +00:00
|
|
|
|
2020-12-18 12:05:48 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2022-03-31 15:55:40 +00:00
|
|
|
// Executes an API request with the request body as a string.
|
2020-04-08 11:24:06 +00:00
|
|
|
func PerformRequestWithBody(r http.Handler, method, path, body string) *httptest.ResponseRecorder {
|
|
|
|
reader := strings.NewReader(body)
|
|
|
|
req, _ := http.NewRequest(method, path, reader)
|
|
|
|
w := httptest.NewRecorder()
|
2020-05-08 16:35:19 +00:00
|
|
|
|
2021-08-11 10:47:13 +00:00
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
2022-09-28 07:01:17 +00:00
|
|
|
return w
|
2020-05-08 16:35:19 +00:00
|
|
|
}
|
2023-01-28 19:56:57 +00:00
|
|
|
|
|
|
|
// Executes an API request with a stream response.
|
|
|
|
func PerformRequestWithStream(r http.Handler, method, path string) *CloseableResponseRecorder {
|
|
|
|
req, _ := http.NewRequest(method, path, nil)
|
|
|
|
w := &CloseableResponseRecorder{httptest.NewRecorder(), make(chan bool, 1)}
|
|
|
|
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|