added api tests TestApiDirUpload and TestApiFileUpload (FAILING)

This commit is contained in:
zelig 2015-05-31 21:57:02 +01:00
parent e7f2234c7f
commit c4bca77d96
2 changed files with 54 additions and 14 deletions

View file

@ -359,7 +359,8 @@ func (self *Api) getPath(uri string) (reader SectionReader, mimeType string, sta
dpaLogger.Debugf("Swarm: content lookup key: '%064x' (%v)", key, mimeType) dpaLogger.Debugf("Swarm: content lookup key: '%064x' (%v)", key, mimeType)
reader = self.dpa.Retrieve(key) reader = self.dpa.Retrieve(key)
} else { } else {
dpaLogger.Debugf("Swarm: getEntry(%s): not found", path) err = fmt.Errorf("manifest entry for '%s' not found", path)
dpaLogger.Debugf("Swarm: %v", err)
} }
return return
} }

View file

@ -1,15 +1,12 @@
package bzz package bzz
import ( import (
// "net/http" "bytes"
// "strings" "io/ioutil"
"testing"
// "time"
"os" "os"
"path" "path"
"runtime" "runtime"
"testing"
// "github.com/ethereum/go-ethereum/common/docserver"
) )
var ( var (
@ -36,6 +33,7 @@ func TestApiPut(t *testing.T) {
api, err := testApi() api, err := testApi()
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
return
} }
expContent := "hello" expContent := "hello"
expMimeType := "text/plain" expMimeType := "text/plain"
@ -44,30 +42,71 @@ func TestApiPut(t *testing.T) {
bzzhash, err := api.Put(expContent, expMimeType) bzzhash, err := api.Put(expContent, expMimeType)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
return
} }
bytecontent, mimeType, status, size, err := api.Get(bzzhash) testGet(t, api, bzzhash, []byte(expContent), expMimeType, expStatus, expSize)
}
func testGet(t *testing.T, api *Api, bzzhash string, expContent []byte, expMimeType string, expStatus int, expSize int) {
content, mimeType, status, size, err := api.Get(bzzhash)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
return
} }
content := string(bytecontent) if !bytes.Equal(content, expContent) {
if content != expContent { t.Errorf("incorrect content. expected '%s...', got '%s...'", string(expContent), string(content))
t.Errorf("incorrect content. expected '%s', got '%s'", expContent, content)
} }
if mimeType != expMimeType { if mimeType != expMimeType {
t.Errorf("incorrect mimeType. expected '%s', got '%s'", expMimeType, mimeType) t.Errorf("incorrect mimeType. expected '%s', got '%s'", expMimeType, mimeType)
} }
if status != expStatus { if status != expStatus {
t.Errorf("incorrect status. expected '%s', got '%s'", expStatus, status) t.Errorf("incorrect status. expected '%d', got '%d'", expStatus, status)
} }
if size != expSize { if size != expSize {
t.Errorf("incorrect size. expected '%d', got '%d'", expSize, size) t.Errorf("incorrect size. expected '%d', got '%d'", expSize, size)
} }
} }
func TestApiUpload(t *testing.T) { func TestApiDirUpload(t *testing.T) {
api, err := testApi() api, err := testApi()
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
return
} }
_ = api bzzhash, err := api.Upload(path.Join(testDir, "test0"))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
content, err := ioutil.ReadFile(path.Join(testDir, "test0", "index.html"))
testGet(t, api, path.Join(bzzhash, "index.html"), content, "text/html", 0, 202)
testGet(t, api, bzzhash, content, "text/html", 0, 202)
content, err = ioutil.ReadFile(path.Join(testDir, "test0", "index.css"))
testGet(t, api, path.Join(bzzhash, "index.css"), content, "text/plain", 0, 132)
content, err = ioutil.ReadFile(path.Join(testDir, "test0", "img", "logo.png"))
testGet(t, api, path.Join(bzzhash, "img", "logo.png"), content, "image/png", 0, 18136)
_, _, _, _, err = api.Get(bzzhash)
if err == nil {
t.Errorf("expected error: %v", err)
}
}
func TestApiFileUpload(t *testing.T) {
api, err := testApi()
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
bzzhash, err := api.Upload(path.Join(testDir, "test0", "index.html"))
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
content, err := ioutil.ReadFile(path.Join(testDir, "test0", "index.html"))
testGet(t, api, path.Join(bzzhash), content, "text/html", 0, 202)
} }