mirror of
https://github.com/restic/rest-server.git
synced 2025-12-07 09:36:13 -08:00
test that inaccessible files result in status 'internal error'
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -269,6 +270,74 @@ func TestResticHandler(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestResticErrorHandler runs tests on the restic handler error handling.
|
||||||
|
func TestResticErrorHandler(t *testing.T) {
|
||||||
|
var tests = []struct {
|
||||||
|
seq []TestRequest
|
||||||
|
}{
|
||||||
|
// Test inaccessible file
|
||||||
|
{
|
||||||
|
[]TestRequest{{
|
||||||
|
req: newRequest(t, "GET", "/config", nil),
|
||||||
|
want: []wantFunc{wantCode(http.StatusInternalServerError)},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]TestRequest{{
|
||||||
|
req: newRequest(t, "GET", "/parent4/config", nil),
|
||||||
|
want: []wantFunc{wantCode(http.StatusNotFound)},
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// setup the server with a local backend in a temporary directory
|
||||||
|
tempdir, err := ioutil.TempDir("", "rest-server-test-")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure the tempdir is properly removed
|
||||||
|
defer func() {
|
||||||
|
err := os.RemoveAll(tempdir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// set append-only mode and configure path
|
||||||
|
mux, err := NewHandler(&Server{
|
||||||
|
AppendOnly: true,
|
||||||
|
Path: tempdir,
|
||||||
|
NoAuth: true,
|
||||||
|
Debug: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error from NewHandler: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the repo
|
||||||
|
checkRequest(t, mux.ServeHTTP,
|
||||||
|
newRequest(t, "POST", "/?create=true", nil),
|
||||||
|
[]wantFunc{wantCode(http.StatusOK)})
|
||||||
|
// create inaccessible config
|
||||||
|
checkRequest(t, mux.ServeHTTP,
|
||||||
|
newRequest(t, "POST", "/config", strings.NewReader("example")),
|
||||||
|
[]wantFunc{wantCode(http.StatusOK)})
|
||||||
|
err = os.Chmod(path.Join(tempdir, "config"), 0o000)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run("", func(t *testing.T) {
|
||||||
|
for i, seq := range test.seq {
|
||||||
|
t.Logf("request %v: %v %v", i, seq.req.Method, seq.req.URL.Path)
|
||||||
|
checkRequest(t, mux.ServeHTTP, seq.req, seq.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSplitURLPath(t *testing.T) {
|
func TestSplitURLPath(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
// Params
|
// Params
|
||||||
|
|||||||
Reference in New Issue
Block a user