package tests

import (
	"bytes"
	"encoding/json"
	"net/http"
	"net/http/httptest"

	"github.com/pingcap/dbaas/billing2/utils/panicIf"
)

func PerformGetRequest(router http.Handler, path string) *httptest.ResponseRecorder {
	req, _ := http.NewRequest("GET", path, nil)
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)
	return w
}

func PerformRequest(router http.Handler, method string, path string, input interface{}) *httptest.ResponseRecorder {
	var inputBytes []byte
	var err error
	if input != nil {
		inputBytes, err = json.Marshal(input)
		panicIf.Error(err)
	}
	req, _ := http.NewRequest(method, path, bytes.NewReader(inputBytes))
	w := httptest.NewRecorder()
	router.ServeHTTP(w, req)
	return w
}
