server_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package http
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "net/http"
  6. "sync"
  7. "testing"
  8. "time"
  9. "github.com/ethereum/go-ethereum/common"
  10. "github.com/ethereum/go-ethereum/swarm/api"
  11. "github.com/ethereum/go-ethereum/swarm/storage"
  12. )
  13. func TestBzzrGetPath(t *testing.T) {
  14. var err error
  15. maxproxyattempts := 3
  16. testmanifest := []string{
  17. `{"entries":[{"path":"a/","hash":"674af7073604ebfc0282a4ab21e5ef1a3c22913866879ebc0816f8a89896b2ed","contentType":"application/bzz-manifest+json","status":0}]}`,
  18. `{"entries":[{"path":"a","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"b/","hash":"0a87b1c3e4bf013686cdf107ec58590f2004610ee58cc2240f26939f691215f5","contentType":"application/bzz-manifest+json","status":0}]}`,
  19. `{"entries":[{"path":"b","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"c","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0}]}`,
  20. }
  21. testrequests := make(map[string]int)
  22. testrequests["/"] = 0
  23. testrequests["/a"] = 1
  24. testrequests["/a/b"] = 2
  25. testrequests["/x"] = 0
  26. testrequests[""] = 0
  27. expectedfailrequests := []string{"", "/x"}
  28. reader := [3]*bytes.Reader{}
  29. key := [3]storage.Key{}
  30. dir, _ := ioutil.TempDir("", "bzz-storage-test")
  31. storeparams := &storage.StoreParams{
  32. ChunkDbPath: dir,
  33. DbCapacity: 5000000,
  34. CacheCapacity: 5000,
  35. Radius: 0,
  36. }
  37. localStore, err := storage.NewLocalStore(storage.MakeHashFunc("SHA3"), storeparams)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. chunker := storage.NewTreeChunker(storage.NewChunkerParams())
  42. dpa := &storage.DPA{
  43. Chunker: chunker,
  44. ChunkStore: localStore,
  45. }
  46. dpa.Start()
  47. defer dpa.Stop()
  48. wg := &sync.WaitGroup{}
  49. for i, mf := range testmanifest {
  50. reader[i] = bytes.NewReader([]byte(mf))
  51. key[i], err = dpa.Store(reader[i], int64(len(mf)), wg, nil)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. wg.Wait()
  56. }
  57. a := api.NewApi(dpa, nil)
  58. /// \todo iterate port numbers up if fail
  59. StartHttpServer(a, &Server{Addr: "127.0.0.1:8504", CorsString: ""})
  60. // how to wait for ListenAndServe to have initialized? This is pretty cruuuude
  61. // if we fix it we don't need maxproxyattempts anymore either
  62. time.Sleep(1000 * time.Millisecond)
  63. for i := 0; i <= maxproxyattempts; i++ {
  64. _, err := http.Get("http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/a")
  65. if i == maxproxyattempts {
  66. t.Fatalf("Failed to connect to proxy after %v attempts: %v", i, err)
  67. } else if err != nil {
  68. time.Sleep(100 * time.Millisecond)
  69. continue
  70. }
  71. break
  72. }
  73. for k, v := range testrequests {
  74. var resp *http.Response
  75. var respbody []byte
  76. url := "http://127.0.0.1:8504/bzzr:/"
  77. if k[:] != "" {
  78. url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
  79. }
  80. resp, err = http.Get(url)
  81. defer resp.Body.Close()
  82. respbody, err = ioutil.ReadAll(resp.Body)
  83. if string(respbody) != testmanifest[v] {
  84. isexpectedfailrequest := false
  85. for _, r := range expectedfailrequests {
  86. if k[:] == r {
  87. isexpectedfailrequest = true
  88. }
  89. }
  90. if isexpectedfailrequest == false {
  91. t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(respbody))
  92. }
  93. }
  94. }
  95. }