github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/swarm/api/http/test_server.go (about)

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