server_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_test
  17. import (
  18. "bytes"
  19. "io/ioutil"
  20. "net/http"
  21. "sync"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/swarm/storage"
  25. "github.com/ethereum/go-ethereum/swarm/testutil"
  26. )
  27. func TestBzzrGetPath(t *testing.T) {
  28. var err error
  29. testmanifest := []string{
  30. `{"entries":[{"path":"a/","hash":"674af7073604ebfc0282a4ab21e5ef1a3c22913866879ebc0816f8a89896b2ed","contentType":"application/bzz-manifest+json","status":0}]}`,
  31. `{"entries":[{"path":"a","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"b/","hash":"0a87b1c3e4bf013686cdf107ec58590f2004610ee58cc2240f26939f691215f5","contentType":"application/bzz-manifest+json","status":0}]}`,
  32. `{"entries":[{"path":"b","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"c","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0}]}`,
  33. }
  34. testrequests := make(map[string]int)
  35. testrequests["/"] = 0
  36. testrequests["/a/"] = 1
  37. testrequests["/a/b/"] = 2
  38. testrequests["/x"] = 0
  39. testrequests[""] = 0
  40. expectedfailrequests := []string{"", "/x"}
  41. reader := [3]*bytes.Reader{}
  42. key := [3]storage.Key{}
  43. srv := testutil.NewTestSwarmServer(t)
  44. defer srv.Close()
  45. wg := &sync.WaitGroup{}
  46. for i, mf := range testmanifest {
  47. reader[i] = bytes.NewReader([]byte(mf))
  48. key[i], err = srv.Dpa.Store(reader[i], int64(len(mf)), wg, nil)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. wg.Wait()
  53. }
  54. _, err = http.Get(srv.URL + "/bzzr:/" + common.ToHex(key[0])[2:] + "/a")
  55. if err != nil {
  56. t.Fatalf("Failed to connect to proxy: %v", err)
  57. }
  58. for k, v := range testrequests {
  59. var resp *http.Response
  60. var respbody []byte
  61. url := srv.URL + "/bzzr:/"
  62. if k[:] != "" {
  63. url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
  64. }
  65. resp, err = http.Get(url)
  66. if err != nil {
  67. t.Fatalf("Request failed: %v", err)
  68. }
  69. defer resp.Body.Close()
  70. respbody, err = ioutil.ReadAll(resp.Body)
  71. if string(respbody) != testmanifest[v] {
  72. isexpectedfailrequest := false
  73. for _, r := range expectedfailrequests {
  74. if k[:] == r {
  75. isexpectedfailrequest = true
  76. }
  77. }
  78. if isexpectedfailrequest == false {
  79. t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(respbody))
  80. }
  81. }
  82. }
  83. nonhashtests := []string{
  84. srv.URL + "/bzz:/name",
  85. srv.URL + "/bzzi:/nonhash",
  86. srv.URL + "/bzzr:/nonhash",
  87. }
  88. nonhashresponses := []string{
  89. "error resolving name: no DNS to resolve name: \"name\"\n",
  90. "error resolving nonhash: immutable address not a content hash: \"nonhash\"\n",
  91. "error resolving nonhash: no DNS to resolve name: \"nonhash\"\n",
  92. }
  93. for i, url := range nonhashtests {
  94. var resp *http.Response
  95. var respbody []byte
  96. resp, err = http.Get(url)
  97. if err != nil {
  98. t.Fatalf("Request failed: %v", err)
  99. }
  100. defer resp.Body.Close()
  101. respbody, err = ioutil.ReadAll(resp.Body)
  102. if string(respbody) != nonhashresponses[i] {
  103. t.Fatalf("Non-Hash response body does not match, expected: %v, got: %v", nonhashresponses[i], string(respbody))
  104. }
  105. }
  106. }