github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/swarm/api/http/server_test.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 http
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"net/http"
    23  	"sync"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/atheioschain/go-atheios/common"
    28  	"github.com/atheioschain/go-atheios/swarm/api"
    29  	"github.com/atheioschain/go-atheios/swarm/storage"
    30  )
    31  
    32  func TestBzzrGetPath(t *testing.T) {
    33  
    34  	var err error
    35  
    36  	maxproxyattempts := 3
    37  
    38  	testmanifest := []string{
    39  		`{"entries":[{"path":"a/","hash":"674af7073604ebfc0282a4ab21e5ef1a3c22913866879ebc0816f8a89896b2ed","contentType":"application/bzz-manifest+json","status":0}]}`,
    40  		`{"entries":[{"path":"a","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"b/","hash":"0a87b1c3e4bf013686cdf107ec58590f2004610ee58cc2240f26939f691215f5","contentType":"application/bzz-manifest+json","status":0}]}`,
    41  		`{"entries":[{"path":"b","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"c","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0}]}`,
    42  	}
    43  
    44  	testrequests := make(map[string]int)
    45  	testrequests["/"] = 0
    46  	testrequests["/a"] = 1
    47  	testrequests["/a/b"] = 2
    48  	testrequests["/x"] = 0
    49  	testrequests[""] = 0
    50  
    51  	expectedfailrequests := []string{"", "/x"}
    52  
    53  	reader := [3]*bytes.Reader{}
    54  
    55  	key := [3]storage.Key{}
    56  
    57  	dir, _ := ioutil.TempDir("", "bzz-storage-test")
    58  
    59  	storeparams := &storage.StoreParams{
    60  		ChunkDbPath:   dir,
    61  		DbCapacity:    5000000,
    62  		CacheCapacity: 5000,
    63  		Radius:        0,
    64  	}
    65  
    66  	localStore, err := storage.NewLocalStore(storage.MakeHashFunc("SHA3"), storeparams)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	chunker := storage.NewTreeChunker(storage.NewChunkerParams())
    71  	dpa := &storage.DPA{
    72  		Chunker:    chunker,
    73  		ChunkStore: localStore,
    74  	}
    75  	dpa.Start()
    76  	defer dpa.Stop()
    77  
    78  	wg := &sync.WaitGroup{}
    79  
    80  	for i, mf := range testmanifest {
    81  		reader[i] = bytes.NewReader([]byte(mf))
    82  		key[i], err = dpa.Store(reader[i], int64(len(mf)), wg, nil)
    83  		if err != nil {
    84  			t.Fatal(err)
    85  		}
    86  		wg.Wait()
    87  	}
    88  
    89  	a := api.NewApi(dpa, nil)
    90  
    91  	/// \todo iterate port numbers up if fail
    92  	StartHttpServer(a, &Server{Addr: "127.0.0.1:8504", CorsString: ""})
    93  	// how to wait for ListenAndServe to have initialized? This is pretty cruuuude
    94  	// if we fix it we don't need maxproxyattempts anymore either
    95  	time.Sleep(1000 * time.Millisecond)
    96  	for i := 0; i <= maxproxyattempts; i++ {
    97  		_, err := http.Get("http://127.0.0.1:8504/bzzr:/" + common.ToHex(key[0])[2:] + "/a")
    98  		if i == maxproxyattempts {
    99  			t.Fatalf("Failed to connect to proxy after %v attempts: %v", i, err)
   100  		} else if err != nil {
   101  			time.Sleep(100 * time.Millisecond)
   102  			continue
   103  		}
   104  		break
   105  	}
   106  
   107  	for k, v := range testrequests {
   108  		var resp *http.Response
   109  		var respbody []byte
   110  
   111  		url := "http://127.0.0.1:8504/bzzr:/"
   112  		if k[:] != "" {
   113  			url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
   114  		}
   115  		resp, err = http.Get(url)
   116  		defer resp.Body.Close()
   117  		respbody, err = ioutil.ReadAll(resp.Body)
   118  
   119  		if string(respbody) != testmanifest[v] {
   120  			isexpectedfailrequest := false
   121  
   122  			for _, r := range expectedfailrequests {
   123  				if k[:] == r {
   124  					isexpectedfailrequest = true
   125  				}
   126  			}
   127  			if isexpectedfailrequest == false {
   128  				t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(respbody))
   129  			}
   130  		}
   131  	}
   132  
   133  }