shiori/internal/testutil/http.go
Felipe Martin 6f19c12c95
Start working on new REST API. Refactor logic in domains. (#497)
* added 404 template

* added auth domain

* added embed file for frontend

* added base config and dependencies

* added basic new http server

* added separated server command

* updated go modules

* removed modd file

* Added shortcut to send internal server error response

* Added JWT support to Auth Domain

* Added JWT support to API

* docs: added comments to response struct

* naming

* inline returns

* updated dependencies

* production logger

* bookmarks endpoint

* reverted old views api path

* frontend for api v1

* proper 404 error (not working atm)

* use response

* removed 404 html

* server error handler

* login and basic auth

* adjusted session duration

* properly retrieve tags

* properly delete bookmark

* cleanup

* archiver domain

* debug routes

* bookmark routes

* expiration by parameter

* move to logrus

* logout

* frontend cache

* updated dependencies

* http: migrated to gin

* linted

* Added version command

* unit tests, docs

* response test utils and tests

* remove logout handler

* auth

* createtag

* improved http test utilities

* assert message equals

* Remove 1.19 from test matrix

* moved api to v1 folder

* docs: contribute docs

* updated makefile

* updated usage docs

* warn in server command

* updaed docs with shiori version command

* Updated documentation

* deps: update
2023-07-17 14:30:18 +01:00

49 lines
1.1 KiB
Go

package testutil
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"github.com/gin-gonic/gin"
)
// NewGin returns a new gin engine with test mode enabled.
func NewGin() *gin.Engine {
engine := gin.New()
gin.SetMode(gin.TestMode)
return engine
}
type Option = func(*http.Request)
func WithBody(body string) Option {
return func(request *http.Request) {
request.Body = io.NopCloser(strings.NewReader(body))
}
}
func WithHeader(name, value string) Option {
return func(request *http.Request) {
request.Header.Add(name, value)
}
}
func PerformRequest(handler http.Handler, method, path string, options ...Option) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
return PerformRequestWithRecorder(recorder, handler, method, path, options...)
}
func PerformRequestWithRecorder(recorder *httptest.ResponseRecorder, r http.Handler, method, path string, options ...Option) *httptest.ResponseRecorder {
request, err := http.NewRequest(method, path, nil)
if err != nil {
panic(err)
}
for _, opt := range options {
opt(request)
}
r.ServeHTTP(recorder, request)
return recorder
}