mirror of
https://github.com/restic/rest-server.git
synced 2025-10-19 07:33:21 +00:00
improved repository with ioutil and added tests
This commit is contained in:
parent
e69d5b7615
commit
016bbf619a
3 changed files with 65 additions and 6 deletions
|
@ -151,8 +151,8 @@ func GetBlob(w http.ResponseWriter, r *http.Request, c *Context) {
|
|||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
blob, errr := repo.ReadBlob(bt, id)
|
||||
if errr != nil {
|
||||
blob, err := repo.ReadBlob(bt, id)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
@ -191,7 +191,7 @@ func PostBlob(w http.ResponseWriter, r *http.Request, c *Context) {
|
|||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(201)
|
||||
w.WriteHeader(200)
|
||||
}
|
||||
|
||||
func DeleteBlob(w http.ResponseWriter, r *http.Request, c *Context) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
@ -77,9 +78,11 @@ func (r *Repository) HasBlob(bt backend.Type, id backend.ID) bool {
|
|||
|
||||
func (r *Repository) ReadBlob(bt backend.Type, id backend.ID) (io.ReadSeeker, error) {
|
||||
file := filepath.Join(r.path, string(bt), id.String())
|
||||
f, err := os.Open(file)
|
||||
defer f.Close()
|
||||
return f, err
|
||||
blob, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewReader(blob), nil
|
||||
}
|
||||
|
||||
func (r *Repository) WriteBlob(bt backend.Type, id backend.ID, data []byte) error {
|
||||
|
|
56
repository_test.go
Normal file
56
repository_test.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/restic/restic/backend"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRepositoryConfig(t *testing.T) {
|
||||
path := "/tmp/repository"
|
||||
repository, _ := NewRepository(path)
|
||||
defer os.RemoveAll(path)
|
||||
|
||||
_, e1 := os.Stat(path)
|
||||
require.NoError(t, e1, "repository not created")
|
||||
|
||||
require.False(t, repository.HasConfig())
|
||||
|
||||
_, e2 := repository.ReadConfig()
|
||||
require.Error(t, e2, "reading config should fail")
|
||||
|
||||
e3 := repository.WriteConfig([]byte("test"))
|
||||
require.NoError(t, e3, "writing config should succeed")
|
||||
|
||||
require.True(t, repository.HasConfig())
|
||||
|
||||
config, _ := repository.ReadConfig()
|
||||
require.Equal(t, config, []byte("test"), "reading config should succeed")
|
||||
}
|
||||
|
||||
func TestRepositoryBlob(t *testing.T) {
|
||||
path := "/tmp/repository"
|
||||
repository, _ := NewRepository(path)
|
||||
//defer os.RemoveAll(path)
|
||||
|
||||
_, e1 := os.Stat(path)
|
||||
require.NoError(t, e1, "repository not created")
|
||||
|
||||
require.False(t, repository.HasBlob(backend.Data, BlobID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")))
|
||||
|
||||
_, e2 := repository.ReadBlob(backend.Data, BlobID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
|
||||
require.Error(t, e2, "reading blob should fail")
|
||||
|
||||
e3 := repository.WriteBlob(backend.Data, BlobID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), []byte("test"))
|
||||
require.NoError(t, e3, "saving blob should succeed")
|
||||
|
||||
require.True(t, repository.HasBlob(backend.Data, BlobID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")))
|
||||
|
||||
blob, _ := repository.ReadBlob(backend.Data, BlobID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
|
||||
bytes, e4 := ioutil.ReadAll(blob)
|
||||
require.NoError(t, e4, e4.Error())
|
||||
require.Equal(t, bytes, []byte("test"), "reading blob should succeed")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue