diff --git a/src/http.go b/src/http.go index 045af02b..bc85a99b 100644 --- a/src/http.go +++ b/src/http.go @@ -85,13 +85,13 @@ func prometheusExporter(metricsPort string, metricsPath string) { // API http server func httpServer(httpPort string, reloaderChan chan<- bool) { reloadHandler := ReloadHandler{Chan: reloaderChan} - http.HandleFunc("/info", info) + http.HandleFunc("/info", infoHandler) http.HandleFunc("/reload", reloadHandler.handler) logger.Fatalln(http.ListenAndServe(":"+httpPort, nil)) } -// Info resource -func info(res http.ResponseWriter, req *http.Request) { +// Info resource handler +func infoHandler(res http.ResponseWriter, req *http.Request) { var err error var js []byte diff --git a/src/http_test.go b/src/http_test.go new file mode 100644 index 00000000..3d492779 --- /dev/null +++ b/src/http_test.go @@ -0,0 +1,67 @@ +// +// s3sync-service - Realtime S3 synchronisation tool +// Copyright (c) 2020 Yevgeniy Valeyev +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +package main + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func TestInfoHandler(t *testing.T) { + config = &Config{} + + req, err := http.NewRequest("GET", "/info", nil) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() + handler := http.HandlerFunc(infoHandler) + + handler.ServeHTTP(rr, req) + + if status := rr.Code; status != http.StatusOK { + t.Error( + "handler returned wrong status code: got", status, + "epected", http.StatusOK) + } + + expected, err := json.Marshal(InfoResponse{ + version, + startupTime, + status, + len(config.Sites), + config.UploadWorkers, + config.ChecksumWorkers, + config.LogLevel, + }) + + if err != nil { + t.Error(err.Error()) + } + + if rr.Body.String() != string(expected) { + t.Error( + "handler returned unexpected body: got", rr.Body.String(), + "expected", string(expected), + ) + } +}