http, cache: Support cache resizing

This commit is contained in:
Martin Polden
2020-09-11 21:16:43 +02:00
parent ceaff84709
commit b7ed233452
4 changed files with 100 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/mpolden/echoip/iputil/geo"
@ -56,6 +57,23 @@ func httpGet(url string, acceptMediaType string, userAgent string) (string, int,
return string(data), res.StatusCode, nil
}
func httpPost(url, body string) (*http.Response, string, error) {
r, err := http.NewRequest(http.MethodPost, url, strings.NewReader(body))
if err != nil {
return nil, "", err
}
res, err := http.DefaultClient.Do(r)
if err != nil {
return nil, "", err
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, "", err
}
return res, string(data), nil
}
func TestCLIHandlers(t *testing.T) {
log.SetOutput(ioutil.Discard)
s := httptest.NewServer(testServer().Handler())
@ -175,6 +193,21 @@ func TestCacheHandler(t *testing.T) {
}
}
func TestCacheResizeHandler(t *testing.T) {
log.SetOutput(ioutil.Discard)
srv := testServer()
srv.profile = true
s := httptest.NewServer(srv.Handler())
_, got, err := httpPost(s.URL+"/debug/cache/resize", "10")
if err != nil {
t.Fatal(err)
}
want := `{"message":"Changed cache capacity to 10."}`
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestIPFromRequest(t *testing.T) {
var tests = []struct {
remoteAddr string