github.com/gobitfly/go-ethereum@v1.8.12/swarm/testutil/http.go (about)

     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  
    17  package testutil
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"math/big"
    23  	"net/http"
    24  	"net/http/httptest"
    25  	"os"
    26  	"testing"
    27  
    28  	"github.com/ethereum/go-ethereum/core/types"
    29  	"github.com/ethereum/go-ethereum/swarm/api"
    30  	"github.com/ethereum/go-ethereum/swarm/storage"
    31  	"github.com/ethereum/go-ethereum/swarm/storage/mru"
    32  )
    33  
    34  type TestServer interface {
    35  	ServeHTTP(http.ResponseWriter, *http.Request)
    36  }
    37  
    38  type fakeBackend struct {
    39  	blocknumber int64
    40  }
    41  
    42  func (f *fakeBackend) HeaderByNumber(context context.Context, _ string, bigblock *big.Int) (*types.Header, error) {
    43  	f.blocknumber++
    44  	biggie := big.NewInt(f.blocknumber)
    45  	return &types.Header{
    46  		Number: biggie,
    47  	}, nil
    48  }
    49  
    50  func NewTestSwarmServer(t *testing.T, serverFunc func(*api.API) TestServer) *TestSwarmServer {
    51  	dir, err := ioutil.TempDir("", "swarm-storage-test")
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	storeparams := storage.NewDefaultLocalStoreParams()
    56  	storeparams.DbCapacity = 5000000
    57  	storeparams.CacheCapacity = 5000
    58  	storeparams.Init(dir)
    59  	localStore, err := storage.NewLocalStore(storeparams, nil)
    60  	if err != nil {
    61  		os.RemoveAll(dir)
    62  		t.Fatal(err)
    63  	}
    64  	fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams())
    65  
    66  	// mutable resources test setup
    67  	resourceDir, err := ioutil.TempDir("", "swarm-resource-test")
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	rhparams := &mru.HandlerParams{
    72  		QueryMaxPeriods: &mru.LookupParams{},
    73  		HeaderGetter: &fakeBackend{
    74  			blocknumber: 42,
    75  		},
    76  	}
    77  	rh, err := mru.NewTestHandler(resourceDir, rhparams)
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	a := api.NewAPI(fileStore, nil, rh)
    83  	srv := httptest.NewServer(serverFunc(a))
    84  	return &TestSwarmServer{
    85  		Server:    srv,
    86  		FileStore: fileStore,
    87  		dir:       dir,
    88  		Hasher:    storage.MakeHashFunc(storage.DefaultHash)(),
    89  		cleanup: func() {
    90  			srv.Close()
    91  			rh.Close()
    92  			os.RemoveAll(dir)
    93  			os.RemoveAll(resourceDir)
    94  		},
    95  	}
    96  }
    97  
    98  type TestSwarmServer struct {
    99  	*httptest.Server
   100  	Hasher    storage.SwarmHash
   101  	FileStore *storage.FileStore
   102  	dir       string
   103  	cleanup   func()
   104  }
   105  
   106  func (t *TestSwarmServer) Close() {
   107  	t.cleanup()
   108  }