func TestFileCreate(t *testing.T) {
	path, remover := util.CreateTempDir("foo")
	defer remover()

	file_path := filepath.Join(path, "foo.txt")

	file, err := CreateFileModel(file_path, "testing")
	if err != nil {
		t.Logf("got an error: %s", err)
		t.FailNow()
	}

	if file.Name() != file_path {
		t.Errorf(util.FormatTestMessage("got incorrect name"), file.Name(), file_path)
	}

	contents := file.Read()
	if contents != "testing" {
		t.Errorf(util.FormatTestMessage("file contents are incorrect"), contents, "testing")
	}

	file.Remove()
	if util.FileOrDirectoryExists(file.Name()) {
		t.Errorf("file should be gone, but it is not: %s", file.Name())
	}
}
func TestFileReadWrite(t *testing.T) {
	path, remover := util.CreateFileInTempDir("foo.txt")
	defer remover()

	file, err := NewFileModel(path)
	if err != nil {
		t.Logf("got an error: %s", err)
		t.FailNow()
	}

	contents := file.Read()
	if contents != "" {
		t.Errorf(util.FormatTestMessage("file should be empty"), contents, path)
	}

	file.Write("testing")

	contents = file.Read()
	if contents != "testing" {
		t.Errorf(util.FormatTestMessage("file shouldn't be empty"), contents, path)
	}
}
func TestDirectoryListing(t *testing.T) {
	path, paths, remover := util.CreatePopulatedTempDir("foo", []string{"bar.txt", "baz.txt"})
	defer remover()

	contentserver := NewContentController(path)

	req, err := http.NewRequest("GET", "http://example.com/", nil)
	if err != nil {
		t.Logf("got an error: %s", err)
		t.FailNow()
	}

	w := httptest.NewRecorder()
	contentserver.ServeHTTP(w, req)

	if w.Code != 200 {
		t.Errorf(util.FormatTestMessage("got wrong status"), w.Code, 200)
	}

	listing := make(model.DirectoryListing, len(paths))
	body := w.Body.String()

	err = json.Unmarshal([]byte(body), &listing)
	if err != nil {
		t.Errorf("got an error: %s", err)
	}

	if listing.Size() != len(paths) {
		t.Errorf(util.FormatTestMessage("got wrong size of listing"), listing.Size(), len(paths))
	}

	for i, p := range paths {
		name := listing.Get(i)["name"]
		path := filepath.Base(p)
		if name != path {
			t.Errorf(util.FormatTestMessage("wrong listing name"), name, path)
		}
	}
}
func TestFileName(t *testing.T) {
	path, remover := util.CreateFileInTempDir("foo.txt")
	defer remover()

	file, err := NewFileModel(path)
	if err != nil {
		t.Logf("got an error: %s", err)
		t.FailNow()
	}

	if file.Name() != path {
		t.Errorf(util.FormatTestMessage("got incorrect name"), file.Name(), path)
	}
}
func TestDirectoryName(t *testing.T) {
	path, remover := util.CreateTempDir("foo")
	defer remover()

	directory, err := NewDirectoryModel(path)
	if err != nil {
		t.Logf("got an error: %s", err)
		t.FailNow()
	}

	if directory.Name() != path {
		t.Errorf(util.FormatTestMessage("got incorrect name"), directory.Name(), path)
	}
}
func TestDirectoryListing(t *testing.T) {
	path, paths, remover := util.CreatePopulatedTempDir("foo", []string{"bar.txt", "baz.txt"})
	defer remover()

	directory, err := NewDirectoryModel(path)
	if err != nil {
		t.Logf("got an error: %s", err)
		t.FailNow()
	}

	listing := directory.List()

	if listing.Size() != len(paths) {
		t.Errorf(util.FormatTestMessage("listing is not expected size"), len(listing), len(paths))
	}

	for i, p := range paths {
		name := listing.Get(i)["name"]
		path := filepath.Base(p)
		if name != path {
			t.Errorf(util.FormatTestMessage("wrong listing name"), name, path)
		}
	}
}
func TestEmptyDirectoryListing(t *testing.T) {
	path, remover := util.CreateTempDir("foo")
	defer remover()

	directory, err := NewDirectoryModel(path)
	if err != nil {
		t.Logf("got an error: %s", err)
		t.FailNow()
	}

	listing := directory.List()

	if listing.Size() != 0 {
		t.Errorf(util.FormatTestMessage("listing is not empty"), len(listing), 0)
	}

}