diff --git a/router_test.go b/router_test.go index a41f075..de991eb 100644 --- a/router_test.go +++ b/router_test.go @@ -1,168 +1,54 @@ package main import ( + "io/ioutil" + "net/http" + "net/http/httptest" + "strings" "testing" - "github.com/restic/restic/backend" + "github.com/stretchr/testify/require" ) -func TestRepositoryName(t *testing.T) { - var name string - var err error +func TestRouter(t *testing.T) { + router := NewRouter() - name, err = RepositoryName("") - if err == nil { - t.Error("empty string should produce an error") - } + getConfig := []byte("GET /config") + router.Get("/config", func(w http.ResponseWriter, r *http.Request) { + w.Write(getConfig) + }) - name, err = RepositoryName("/") - if err == nil { - t.Error("empty repository name should produce an error") - } + postConfig := []byte("POST /config") + router.Post("/config", func(w http.ResponseWriter, r *http.Request) { + w.Write(postConfig) + }) - name, err = RepositoryName("//") - if err == nil { - t.Error("empty repository name should produce an error") - } + getBlobs := []byte("GET /blobs/") + router.Get("/blobs/", func(w http.ResponseWriter, r *http.Request) { + w.Write(getBlobs) + }) - name, err = RepositoryName("/$test") - if err == nil { - t.Error("special characters should produce an error") - } + getBlob := []byte("GET /blobs/:sha") + router.Get("/blobs/:sha", func(w http.ResponseWriter, r *http.Request) { + w.Write(getBlob) + }) - name, err = RepositoryName("/test") - if name != "test" { - t.Errorf("repository name is %s but should be test", name) - } + server := httptest.NewServer(router) + defer server.Close() - name, err = RepositoryName("/test-1234") - if name != "test-1234" { - t.Errorf("repository name is %s but should be test-1234", name) - } - - name, err = RepositoryName("/test_1234") - if name != "test_1234" { - t.Errorf("repository name is %s but should be test_1234", name) - } -} - -func TestBackendType(t *testing.T) { - var bt backend.Type - - bt = BackendType("/") - if bt != "" { - t.Error("backend type should be nil") - } - - bt = BackendType("/test") - if bt != "" { - t.Error("backend type should be nil") - } - - bt = BackendType("/test/config") - if bt != backend.Config { - t.Error("backend type should be config") - } - - bt = BackendType("/test/config/") - if bt != backend.Config { - t.Error("backend type should be config") - } - - bt = BackendType("/test/config/test") - if bt != backend.Config { - t.Error("backend type should be config") - } -} - -func TestBlobID(t *testing.T) { - var id backend.ID - - id = BlobID("/") - if !id.IsNull() { - t.Error("blob id should be nil") - } - - id = BlobID("/test") - if !id.IsNull() { - t.Error("blob id should be nil") - } - - id = BlobID("/test/data") - if !id.IsNull() { - t.Error("blob id should be nil") - } - - id = BlobID("/test/data/") - if !id.IsNull() { - t.Error("blob id should be nil") - } - - id = BlobID("/test/data/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") - if id.IsNull() { - t.Error("blob id should not be nil") - } -} - -func TestRestAPI(t *testing.T) { - type route struct { - method string - path string - } - - validEndpoints := []route{ - route{"HEAD", "/repo/config"}, - route{"GET", "/repo/config"}, - route{"POST", "/repo/config"}, - route{"GET", "/repo/data/"}, - route{"HEAD", "/repo/data/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/data/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"POST", "/repo/data/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"DELETE", "/repo/data/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/snapshots/"}, - route{"HEAD", "/repo/snapshots/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/snapshots/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"POST", "/repo/snapshots/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"DELETE", "/repo/snapshots/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/index/"}, - route{"HEAD", "/repo/index/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/index/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"POST", "/repo/index/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"DELETE", "/repo/index/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/locks/"}, - route{"HEAD", "/repo/locks/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/locks/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"POST", "/repo/locks/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"DELETE", "/repo/locks/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/keys/"}, - route{"HEAD", "/repo/keys/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/keys/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"POST", "/repo/keys/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"DELETE", "/repo/keys/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - } - - for _, route := range validEndpoints { - if RestAPI(route.method, route.path) == nil { - t.Errorf("request %s %s should return a handler", route.method, route.path) - } - } - - invalidEndpoints := []route{ - route{"GET", "/"}, - route{"GET", "/repo"}, - route{"GET", "/repo/config/"}, - route{"GET", "/repo/config/aaaa"}, - route{"GET", "/repo/data"}, - route{"GET", "/repo/data/aaaaaaa"}, - route{"GET", "/repo/keys/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, - route{"GET", "/repo/keys/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/"}, - route{"GET", "/repo/keys/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/test"}, - } - - for _, route := range invalidEndpoints { - if RestAPI(route.method, route.path) != nil { - t.Errorf("request %s %s should return nil", route.method, route.path) - } - } + getConfigResp, _ := http.Get(server.URL + "/config") + getConfigBody, _ := ioutil.ReadAll(getConfigResp.Body) + require.Equal(t, string(getConfig), string(getConfigBody)) + postConfigResp, _ := http.Post(server.URL+"/config", "binary/octet-stream", strings.NewReader("post test")) + postConfigBody, _ := ioutil.ReadAll(postConfigResp.Body) + require.Equal(t, string(postConfig), string(postConfigBody)) + + getBlobsResp, _ := http.Get(server.URL + "/blobs/") + getBlobsBody, _ := ioutil.ReadAll(getBlobsResp.Body) + require.Equal(t, string(getBlobs), string(getBlobsBody)) + + getBlobResp, _ := http.Get(server.URL + "/blobs/test") + getBlobBody, _ := ioutil.ReadAll(getBlobResp.Body) + require.Equal(t, string(getBlob), string(getBlobBody)) }