mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
swarm/api/http proxy server test for retrieval of subpath through get
Removed nil entry assignment on subtrie leaf in recursive key retrieval Cleaned up path-or-no-path condition in proxy server get handler
This commit is contained in:
parent
922f1055a4
commit
90daa7a68b
3 changed files with 121 additions and 20 deletions
|
|
@ -177,14 +177,9 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
|
|||
path = trailingSlashes.ReplaceAllString(path, "")
|
||||
if raw {
|
||||
var reader storage.LazySectionReader
|
||||
//
|
||||
parsedurl, _ := api.Parse(path)
|
||||
//if err != nil {
|
||||
//
|
||||
//}
|
||||
|
||||
// resolving host
|
||||
if (parsedurl == path) {
|
||||
if parsedurl == path {
|
||||
key, err := a.Resolve(parsedurl, nameresolver)
|
||||
if err != nil {
|
||||
glog.V(logger.Error).Infof("%v", err)
|
||||
|
|
@ -193,19 +188,13 @@ func handler(w http.ResponseWriter, r *http.Request, a *api.Api) {
|
|||
}
|
||||
reader = a.Retrieve(key)
|
||||
} else {
|
||||
var status int
|
||||
readertmp, _, status, err := a.Get(path, nameresolver)
|
||||
reader = readertmp
|
||||
if err != nil {
|
||||
if _, ok := err.(api.ErrResolve); ok {
|
||||
glog.V(logger.Debug).Infof("%v", err)
|
||||
status = http.StatusBadRequest
|
||||
} else {
|
||||
glog.V(logger.Debug).Infof("error retrieving '%s': %v", uri, err)
|
||||
status = http.StatusNotFound
|
||||
}
|
||||
http.Error(w, err.Error(), status)
|
||||
return
|
||||
}
|
||||
reader = readertmp
|
||||
}
|
||||
|
||||
// retrieving content
|
||||
|
|
|
|||
111
swarm/api/http/server_test.go
Normal file
111
swarm/api/http/server_test.go
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"bytes"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||
"github.com/ethereum/go-ethereum/swarm/api"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/logger/glog"
|
||||
)
|
||||
|
||||
func TestBzzrGetPath(t *testing.T) {
|
||||
|
||||
glog.SetToStderr(true)
|
||||
glog.SetV(6)
|
||||
|
||||
var err error
|
||||
|
||||
maxproxyattempts := 3
|
||||
|
||||
testmanifest := []string{
|
||||
`{"entries":[{"path":"a/","hash":"674af7073604ebfc0282a4ab21e5ef1a3c22913866879ebc0816f8a89896b2ed","contentType":"application/bzz-manifest+json","status":0}]}`,
|
||||
`{"entries":[{"path":"a","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"b/","hash":"0a87b1c3e4bf013686cdf107ec58590f2004610ee58cc2240f26939f691215f5","contentType":"application/bzz-manifest+json","status":0}]}`,
|
||||
`{"entries":[{"path":"b","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"c","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0}]}`,
|
||||
}
|
||||
|
||||
testrequests := make(map[string]int)
|
||||
testrequests["/"] = 0
|
||||
testrequests["/a"] = 1
|
||||
testrequests["/a/b"] = 2
|
||||
|
||||
reader := [3]*bytes.Reader{}
|
||||
|
||||
key := [3]storage.Key{}
|
||||
|
||||
dir, _ := ioutil.TempDir("", "bzz-storage-test")
|
||||
|
||||
storeparams := &storage.StoreParams{
|
||||
dir,
|
||||
5000000,
|
||||
5000,
|
||||
0,
|
||||
}
|
||||
|
||||
localStore, err := storage.NewLocalStore(storage.MakeHashFunc("SHA3"), storeparams)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chunker := storage.NewTreeChunker(storage.NewChunkerParams())
|
||||
dpa := &storage.DPA{
|
||||
Chunker: chunker,
|
||||
ChunkStore: localStore,
|
||||
}
|
||||
dpa.Start()
|
||||
defer dpa.Stop()
|
||||
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
for i, mf := range testmanifest {
|
||||
reader[i] = bytes.NewReader([]byte(mf))
|
||||
key[i], err = dpa.Store(reader[i], int64(len(mf)), wg, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
a := api.NewApi(dpa, nil)
|
||||
|
||||
// iterate port numbers up if fail
|
||||
StartHttpServer(a, "8504")
|
||||
// how to wait for ListenAndServe to have initialized? This is pretty cruuuude
|
||||
// if we fix it we don't need maxproxyattempts anymore either
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
for i := 0; i <= maxproxyattempts; i++ {
|
||||
_, err := http.Get("http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/a")
|
||||
if i == maxproxyattempts {
|
||||
t.Fatalf("Failed to connect to proxy after %v attempts: %v", i, err)
|
||||
} else if err != nil {
|
||||
t.Logf("Proxy connect failed: %v", err)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
|
||||
for k, v := range testrequests {
|
||||
var body []byte
|
||||
var resp *http.Response
|
||||
url := "http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
|
||||
t.Logf("Sending proxy GET: %v", url)
|
||||
resp, err = http.Get(url)
|
||||
defer resp.Body.Close()
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
|
||||
if string(body) != testmanifest[v] {
|
||||
t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(body))
|
||||
}
|
||||
|
||||
t.Log(string(body))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -302,7 +302,8 @@ func (self *manifestTrie) findPrefixOf(path string, quitC chan bool) (entry *man
|
|||
if (len(path) >= epl) && (path[:epl] == entry.Path) {
|
||||
glog.V(logger.Detail).Infof("entry.ContentType = %v", entry.ContentType)
|
||||
if entry.ContentType == manifestType {
|
||||
if self.loadSubTrie(entry, quitC) != nil {
|
||||
err := self.loadSubTrie(entry, quitC)
|
||||
if err != nil {
|
||||
return nil, 0
|
||||
}
|
||||
entry, pos = entry.subtrie.findPrefixOf(path[epl:], quitC)
|
||||
|
|
@ -312,9 +313,9 @@ func (self *manifestTrie) findPrefixOf(path string, quitC chan bool) (entry *man
|
|||
} else {
|
||||
pos = epl
|
||||
}
|
||||
} else {
|
||||
} /*else {
|
||||
entry = nil
|
||||
}
|
||||
}*/
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue