0
0
Fork 0
mirror of https://github.com/matrix-org/dendrite synced 2024-12-15 07:43:43 +01:00
dendrite/test/http.go
kegsay 236b16aa6c
Begin adding syncapi component tests (#2442)
* Add very basic syncapi tests

* Add a way to inject jetstream messages

* implement add_state_ids

* bugfixes

* Unbreak tests

* Remove now un-needed API call

* Linting
2022-05-09 17:23:02 +01:00

45 lines
932 B
Go

package test
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"testing"
)
type HTTPRequestOpt func(req *http.Request)
func WithJSONBody(t *testing.T, body interface{}) HTTPRequestOpt {
t.Helper()
b, err := json.Marshal(body)
if err != nil {
t.Fatalf("WithJSONBody: %s", err)
}
return func(req *http.Request) {
req.Body = io.NopCloser(bytes.NewBuffer(b))
}
}
func WithQueryParams(qps map[string]string) HTTPRequestOpt {
var vals url.Values = map[string][]string{}
for k, v := range qps {
vals.Set(k, v)
}
return func(req *http.Request) {
req.URL.RawQuery = vals.Encode()
}
}
func NewRequest(t *testing.T, method, path string, opts ...HTTPRequestOpt) *http.Request {
t.Helper()
req, err := http.NewRequest(method, "http://localhost"+path, nil)
if err != nil {
t.Fatalf("failed to make new HTTP request %v %v : %v", method, path, err)
}
for _, o := range opts {
o(req)
}
return req
}