http.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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/httptest"
  20. "os"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/swarm/api"
  23. httpapi "github.com/ethereum/go-ethereum/swarm/api/http"
  24. "github.com/ethereum/go-ethereum/swarm/storage"
  25. )
  26. func NewTestSwarmServer(t *testing.T) *TestSwarmServer {
  27. dir, err := ioutil.TempDir("", "swarm-storage-test")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. storeparams := &storage.StoreParams{
  32. ChunkDbPath: dir,
  33. DbCapacity: 5000000,
  34. CacheCapacity: 5000,
  35. Radius: 0,
  36. }
  37. localStore, err := storage.NewLocalStore(storage.MakeHashFunc("SHA3"), storeparams)
  38. if err != nil {
  39. os.RemoveAll(dir)
  40. t.Fatal(err)
  41. }
  42. chunker := storage.NewTreeChunker(storage.NewChunkerParams())
  43. dpa := &storage.DPA{
  44. Chunker: chunker,
  45. ChunkStore: localStore,
  46. }
  47. dpa.Start()
  48. a := api.NewApi(dpa, nil)
  49. srv := httptest.NewServer(httpapi.NewServer(a))
  50. return &TestSwarmServer{
  51. Server: srv,
  52. Dpa: dpa,
  53. dir: dir,
  54. }
  55. }
  56. type TestSwarmServer struct {
  57. *httptest.Server
  58. Dpa *storage.DPA
  59. dir string
  60. }
  61. func (t *TestSwarmServer) Close() {
  62. t.Server.Close()
  63. t.Dpa.Stop()
  64. os.RemoveAll(t.dir)
  65. }