test_server.go 2.5 KB

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