server_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "errors"
  20. "fmt"
  21. "io/ioutil"
  22. "net/http"
  23. "sync"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/swarm/api"
  27. swarm "github.com/ethereum/go-ethereum/swarm/api/client"
  28. "github.com/ethereum/go-ethereum/swarm/storage"
  29. "github.com/ethereum/go-ethereum/swarm/testutil"
  30. )
  31. func TestBzzrGetPath(t *testing.T) {
  32. var err error
  33. testmanifest := []string{
  34. `{"entries":[{"path":"a/","hash":"674af7073604ebfc0282a4ab21e5ef1a3c22913866879ebc0816f8a89896b2ed","contentType":"application/bzz-manifest+json","status":0}]}`,
  35. `{"entries":[{"path":"a","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"b/","hash":"0a87b1c3e4bf013686cdf107ec58590f2004610ee58cc2240f26939f691215f5","contentType":"application/bzz-manifest+json","status":0}]}`,
  36. `{"entries":[{"path":"b","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"c","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0}]}`,
  37. }
  38. testrequests := make(map[string]int)
  39. testrequests["/"] = 0
  40. testrequests["/a/"] = 1
  41. testrequests["/a/b/"] = 2
  42. testrequests["/x"] = 0
  43. testrequests[""] = 0
  44. expectedfailrequests := []string{"", "/x"}
  45. reader := [3]*bytes.Reader{}
  46. key := [3]storage.Key{}
  47. srv := testutil.NewTestSwarmServer(t)
  48. defer srv.Close()
  49. wg := &sync.WaitGroup{}
  50. for i, mf := range testmanifest {
  51. reader[i] = bytes.NewReader([]byte(mf))
  52. key[i], err = srv.Dpa.Store(reader[i], int64(len(mf)), wg, nil)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. wg.Wait()
  57. }
  58. _, err = http.Get(srv.URL + "/bzzr:/" + common.ToHex(key[0])[2:] + "/a")
  59. if err != nil {
  60. t.Fatalf("Failed to connect to proxy: %v", err)
  61. }
  62. for k, v := range testrequests {
  63. var resp *http.Response
  64. var respbody []byte
  65. url := srv.URL + "/bzzr:/"
  66. if k[:] != "" {
  67. url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
  68. }
  69. resp, err = http.Get(url)
  70. if err != nil {
  71. t.Fatalf("Request failed: %v", err)
  72. }
  73. defer resp.Body.Close()
  74. respbody, err = ioutil.ReadAll(resp.Body)
  75. if string(respbody) != testmanifest[v] {
  76. isexpectedfailrequest := false
  77. for _, r := range expectedfailrequests {
  78. if k[:] == r {
  79. isexpectedfailrequest = true
  80. }
  81. }
  82. if isexpectedfailrequest == false {
  83. t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(respbody))
  84. }
  85. }
  86. }
  87. nonhashtests := []string{
  88. srv.URL + "/bzz:/name",
  89. srv.URL + "/bzzi:/nonhash",
  90. srv.URL + "/bzzr:/nonhash",
  91. }
  92. nonhashresponses := []string{
  93. "error resolving name: no DNS to resolve name: \"name\"\n",
  94. "error resolving nonhash: immutable address not a content hash: \"nonhash\"\n",
  95. "error resolving nonhash: no DNS to resolve name: \"nonhash\"\n",
  96. }
  97. for i, url := range nonhashtests {
  98. var resp *http.Response
  99. var respbody []byte
  100. resp, err = http.Get(url)
  101. if err != nil {
  102. t.Fatalf("Request failed: %v", err)
  103. }
  104. defer resp.Body.Close()
  105. respbody, err = ioutil.ReadAll(resp.Body)
  106. if string(respbody) != nonhashresponses[i] {
  107. t.Fatalf("Non-Hash response body does not match, expected: %v, got: %v", nonhashresponses[i], string(respbody))
  108. }
  109. }
  110. }
  111. // TestBzzRootRedirect tests that getting the root path of a manifest without
  112. // a trailing slash gets redirected to include the trailing slash so that
  113. // relative URLs work as expected.
  114. func TestBzzRootRedirect(t *testing.T) {
  115. srv := testutil.NewTestSwarmServer(t)
  116. defer srv.Close()
  117. // create a manifest with some data at the root path
  118. client := swarm.NewClient(srv.URL)
  119. data := []byte("data")
  120. file := &swarm.File{
  121. ReadCloser: ioutil.NopCloser(bytes.NewReader(data)),
  122. ManifestEntry: api.ManifestEntry{
  123. Path: "",
  124. ContentType: "text/plain",
  125. Size: int64(len(data)),
  126. },
  127. }
  128. hash, err := client.Upload(file, "")
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. // define a CheckRedirect hook which ensures there is only a single
  133. // redirect to the correct URL
  134. redirected := false
  135. httpClient := http.Client{
  136. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  137. if redirected {
  138. return errors.New("too many redirects")
  139. }
  140. redirected = true
  141. expectedPath := "/bzz:/" + hash + "/"
  142. if req.URL.Path != expectedPath {
  143. return fmt.Errorf("expected redirect to %q, got %q", expectedPath, req.URL.Path)
  144. }
  145. return nil
  146. },
  147. }
  148. // perform the GET request and assert the response
  149. res, err := httpClient.Get(srv.URL + "/bzz:/" + hash)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. defer res.Body.Close()
  154. if !redirected {
  155. t.Fatal("expected GET /bzz:/<hash> to redirect to /bzz:/<hash>/ but it didn't")
  156. }
  157. gotData, err := ioutil.ReadAll(res.Body)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. if !bytes.Equal(gotData, data) {
  162. t.Fatalf("expected response to equal %q, got %q", data, gotData)
  163. }
  164. }