http.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 testutil
  17. import (
  18. "io/ioutil"
  19. "net/http"
  20. "net/http/httptest"
  21. "os"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/swarm/api"
  24. "github.com/ethereum/go-ethereum/swarm/storage"
  25. "github.com/ethereum/go-ethereum/swarm/storage/feeds"
  26. )
  27. type TestServer interface {
  28. ServeHTTP(http.ResponseWriter, *http.Request)
  29. }
  30. func NewTestSwarmServer(t *testing.T, serverFunc func(*api.API) TestServer, resolver api.Resolver) *TestSwarmServer {
  31. dir, err := ioutil.TempDir("", "swarm-storage-test")
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. storeparams := storage.NewDefaultLocalStoreParams()
  36. storeparams.DbCapacity = 5000000
  37. storeparams.CacheCapacity = 5000
  38. storeparams.Init(dir)
  39. localStore, err := storage.NewLocalStore(storeparams, nil)
  40. if err != nil {
  41. os.RemoveAll(dir)
  42. t.Fatal(err)
  43. }
  44. fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams())
  45. // Swarm Feeds test setup
  46. feedsDir, err := ioutil.TempDir("", "swarm-feeds-test")
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. rhparams := &feeds.HandlerParams{}
  51. rh, err := feeds.NewTestHandler(feedsDir, rhparams)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. a := api.NewAPI(fileStore, resolver, rh.Handler, nil)
  56. srv := httptest.NewServer(serverFunc(a))
  57. tss := &TestSwarmServer{
  58. Server: srv,
  59. FileStore: fileStore,
  60. dir: dir,
  61. Hasher: storage.MakeHashFunc(storage.DefaultHash)(),
  62. cleanup: func() {
  63. srv.Close()
  64. rh.Close()
  65. os.RemoveAll(dir)
  66. os.RemoveAll(feedsDir)
  67. },
  68. CurrentTime: 42,
  69. }
  70. feeds.TimestampProvider = tss
  71. return tss
  72. }
  73. type TestSwarmServer struct {
  74. *httptest.Server
  75. Hasher storage.SwarmHash
  76. FileStore *storage.FileStore
  77. dir string
  78. cleanup func()
  79. CurrentTime uint64
  80. }
  81. func (t *TestSwarmServer) Close() {
  82. t.cleanup()
  83. }
  84. func (t *TestSwarmServer) Now() feeds.Timestamp {
  85. return feeds.Timestamp{Time: t.CurrentTime}
  86. }