test_server.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "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/chunk"
  25. "github.com/ethereum/go-ethereum/swarm/storage"
  26. "github.com/ethereum/go-ethereum/swarm/storage/feed"
  27. "github.com/ethereum/go-ethereum/swarm/storage/localstore"
  28. )
  29. type TestServer interface {
  30. ServeHTTP(http.ResponseWriter, *http.Request)
  31. }
  32. func NewTestSwarmServer(t *testing.T, serverFunc func(*api.API) TestServer, resolver api.Resolver) *TestSwarmServer {
  33. swarmDir, err := ioutil.TempDir("", "swarm-storage-test")
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. localStore, err := localstore.New(dir, make([]byte, 32), nil)
  38. if err != nil {
  39. os.RemoveAll(swarmDir)
  40. t.Fatal(err)
  41. }
  42. tags := chunk.NewTags()
  43. fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams(), tags)
  44. // Swarm feeds test setup
  45. feedsDir, err := ioutil.TempDir("", "swarm-feeds-test")
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. feeds, err := feed.NewTestHandler(feedsDir, &feed.HandlerParams{})
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. swarmApi := api.NewAPI(fileStore, resolver, feeds.Handler, nil, tags)
  54. apiServer := httptest.NewServer(serverFunc(swarmApi))
  55. tss := &TestSwarmServer{
  56. Server: apiServer,
  57. FileStore: fileStore,
  58. Tags: tags,
  59. dir: swarmDir,
  60. Hasher: storage.MakeHashFunc(storage.DefaultHash)(),
  61. cleanup: func() {
  62. apiServer.Close()
  63. fileStore.Close()
  64. feeds.Close()
  65. os.RemoveAll(swarmDir)
  66. os.RemoveAll(feedsDir)
  67. },
  68. CurrentTime: 42,
  69. }
  70. feed.TimestampProvider = tss
  71. return tss
  72. }
  73. type TestSwarmServer struct {
  74. *httptest.Server
  75. Hasher storage.SwarmHash
  76. FileStore *storage.FileStore
  77. Tags *chunk.Tags
  78. dir string
  79. cleanup func()
  80. CurrentTime uint64
  81. }
  82. func (t *TestSwarmServer) Close() {
  83. t.cleanup()
  84. }
  85. func (t *TestSwarmServer) Now() feed.Timestamp {
  86. return feed.Timestamp{Time: t.CurrentTime}
  87. }