server_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package http
  17. import (
  18. "bytes"
  19. "io/ioutil"
  20. "net/http"
  21. "sync"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/swarm/api"
  26. "github.com/ethereum/go-ethereum/swarm/storage"
  27. )
  28. func TestBzzrGetPath(t *testing.T) {
  29. var err error
  30. maxproxyattempts := 3
  31. testmanifest := []string{
  32. `{"entries":[{"path":"a/","hash":"674af7073604ebfc0282a4ab21e5ef1a3c22913866879ebc0816f8a89896b2ed","contentType":"application/bzz-manifest+json","status":0}]}`,
  33. `{"entries":[{"path":"a","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"b/","hash":"0a87b1c3e4bf013686cdf107ec58590f2004610ee58cc2240f26939f691215f5","contentType":"application/bzz-manifest+json","status":0}]}`,
  34. `{"entries":[{"path":"b","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"c","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0}]}`,
  35. }
  36. testrequests := make(map[string]int)
  37. testrequests["/"] = 0
  38. testrequests["/a"] = 1
  39. testrequests["/a/b"] = 2
  40. testrequests["/x"] = 0
  41. testrequests[""] = 0
  42. expectedfailrequests := []string{"", "/x"}
  43. reader := [3]*bytes.Reader{}
  44. key := [3]storage.Key{}
  45. dir, _ := ioutil.TempDir("", "bzz-storage-test")
  46. storeparams := &storage.StoreParams{
  47. ChunkDbPath: dir,
  48. DbCapacity: 5000000,
  49. CacheCapacity: 5000,
  50. Radius: 0,
  51. }
  52. localStore, err := storage.NewLocalStore(storage.MakeHashFunc("SHA3"), storeparams)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. chunker := storage.NewTreeChunker(storage.NewChunkerParams())
  57. dpa := &storage.DPA{
  58. Chunker: chunker,
  59. ChunkStore: localStore,
  60. }
  61. dpa.Start()
  62. defer dpa.Stop()
  63. wg := &sync.WaitGroup{}
  64. for i, mf := range testmanifest {
  65. reader[i] = bytes.NewReader([]byte(mf))
  66. key[i], err = dpa.Store(reader[i], int64(len(mf)), wg, nil)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. wg.Wait()
  71. }
  72. a := api.NewApi(dpa, nil)
  73. /// \todo iterate port numbers up if fail
  74. StartHttpServer(a, &Server{Addr: "127.0.0.1:8504", CorsString: ""})
  75. // how to wait for ListenAndServe to have initialized? This is pretty cruuuude
  76. // if we fix it we don't need maxproxyattempts anymore either
  77. time.Sleep(1000 * time.Millisecond)
  78. for i := 0; i <= maxproxyattempts; i++ {
  79. _, err := http.Get("http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/a")
  80. if i == maxproxyattempts {
  81. t.Fatalf("Failed to connect to proxy after %v attempts: %v", i, err)
  82. } else if err != nil {
  83. time.Sleep(100 * time.Millisecond)
  84. continue
  85. }
  86. break
  87. }
  88. for k, v := range testrequests {
  89. var resp *http.Response
  90. var respbody []byte
  91. url := "http://127.0.0.1:8504/bzzr:/"
  92. if k[:] != "" {
  93. url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
  94. }
  95. resp, err = http.Get(url)
  96. if err != nil {
  97. t.Fatalf("Request failed: %v", err)
  98. }
  99. defer resp.Body.Close()
  100. respbody, err = ioutil.ReadAll(resp.Body)
  101. if string(respbody) != testmanifest[v] {
  102. isexpectedfailrequest := false
  103. for _, r := range expectedfailrequests {
  104. if k[:] == r {
  105. isexpectedfailrequest = true
  106. }
  107. }
  108. if isexpectedfailrequest == false {
  109. t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(respbody))
  110. }
  111. }
  112. }
  113. }