server_test.go 5.7 KB

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