some basic tests for the router

This commit is contained in:
Chapuis Bertil
2015-09-17 13:46:49 +02:00
parent d4717695cb
commit 7bf9802504
+39 -153
View File
@@ -1,168 +1,54 @@
package main package main
import ( import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing" "testing"
"github.com/restic/restic/backend" "github.com/stretchr/testify/require"
) )
func TestRepositoryName(t *testing.T) { func TestRouter(t *testing.T) {
var name string router := NewRouter()
var err error
name, err = RepositoryName("") getConfig := []byte("GET /config")
if err == nil { router.Get("/config", func(w http.ResponseWriter, r *http.Request) {
t.Error("empty string should produce an error") w.Write(getConfig)
} })
name, err = RepositoryName("/") postConfig := []byte("POST /config")
if err == nil { router.Post("/config", func(w http.ResponseWriter, r *http.Request) {
t.Error("empty repository name should produce an error") w.Write(postConfig)
} })
name, err = RepositoryName("//") getBlobs := []byte("GET /blobs/")
if err == nil { router.Get("/blobs/", func(w http.ResponseWriter, r *http.Request) {
t.Error("empty repository name should produce an error") w.Write(getBlobs)
} })
name, err = RepositoryName("/$test") getBlob := []byte("GET /blobs/:sha")
if err == nil { router.Get("/blobs/:sha", func(w http.ResponseWriter, r *http.Request) {
t.Error("special characters should produce an error") w.Write(getBlob)
} })
name, err = RepositoryName("/test") server := httptest.NewServer(router)
if name != "test" { defer server.Close()
t.Errorf("repository name is %s but should be test", name)
}
name, err = RepositoryName("/test-1234") getConfigResp, _ := http.Get(server.URL + "/config")
if name != "test-1234" { getConfigBody, _ := ioutil.ReadAll(getConfigResp.Body)
t.Errorf("repository name is %s but should be test-1234", name) require.Equal(t, string(getConfig), string(getConfigBody))
}
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)
}
}
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))
} }