Implement test setup and teardown

Will be reused for the core tests
This commit is contained in:
Felix Geisendörfer 2013-05-07 11:05:20 +02:00
parent 6adf639d37
commit 11343caaae
1 changed files with 42 additions and 20 deletions

View File

@ -11,6 +11,42 @@ import (
const basePath = "/files/" const basePath = "/files/"
func Setup() *TestSetup {
dir, err := ioutil.TempDir("", "tus_handler_test")
if err != nil {
panic(err)
}
config := HandlerConfig{
Dir: dir,
MaxSize: 1024 * 1024,
BasePath: basePath,
}
handler, err := NewHandler(config)
if err != nil {
panic(err)
}
server := httptest.NewServer(handler)
return &TestSetup{
Handler: handler,
Server: server,
}
}
type TestSetup struct {
Handler *Handler
Server *httptest.Server
}
func (s *TestSetup) Teardown() {
s.Server.Close()
if err := os.RemoveAll(s.Handler.config.Dir); err != nil {
panic(err)
}
}
var Protocol_FileCreation_Tests = []struct { var Protocol_FileCreation_Tests = []struct {
Description string Description string
Method string Method string
@ -48,25 +84,8 @@ var Protocol_FileCreation_Tests = []struct {
} }
func TestProtocol_FileCreation(t *testing.T) { func TestProtocol_FileCreation(t *testing.T) {
dir, err := ioutil.TempDir("", "tus_handler_test") setup := Setup()
if err != nil { defer setup.Teardown()
t.Fatal(err)
}
defer os.Remove(dir)
config := HandlerConfig{
Dir: dir,
MaxSize: 1024 * 1024,
BasePath: basePath,
}
handler, err := NewHandler(config)
if err != nil {
t.Fatal(err)
}
server := httptest.NewServer(handler)
defer server.Close()
Tests: Tests:
for _, test := range Protocol_FileCreation_Tests { for _, test := range Protocol_FileCreation_Tests {
@ -77,7 +96,7 @@ Tests:
method = "POST" method = "POST"
} }
req, err := http.NewRequest(method, server.URL+config.BasePath, nil) req, err := http.NewRequest(method, setup.Server.URL+setup.Handler.config.BasePath, nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -118,3 +137,6 @@ Tests:
} }
} }
} }
var Protocol_Core_Tests = []struct {
}{}