github.com/alanchchen/go-ethereum@v1.6.6-0.20170601190819-6171d01b1195/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_test
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"net/http"
    23  	"sync"
    24  	"testing"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/swarm/storage"
    28  	"github.com/ethereum/go-ethereum/swarm/testutil"
    29  )
    30  
    31  func TestBzzrGetPath(t *testing.T) {
    32  
    33  	var err error
    34  
    35  	testmanifest := []string{
    36  		`{"entries":[{"path":"a/","hash":"674af7073604ebfc0282a4ab21e5ef1a3c22913866879ebc0816f8a89896b2ed","contentType":"application/bzz-manifest+json","status":0}]}`,
    37  		`{"entries":[{"path":"a","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"b/","hash":"0a87b1c3e4bf013686cdf107ec58590f2004610ee58cc2240f26939f691215f5","contentType":"application/bzz-manifest+json","status":0}]}`,
    38  		`{"entries":[{"path":"b","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0},{"path":"c","hash":"011b4d03dd8c01f1049143cf9c4c817e4b167f1d1b83e5c6f0f10d89ba1e7bce","contentType":"","status":0}]}`,
    39  	}
    40  
    41  	testrequests := make(map[string]int)
    42  	testrequests["/"] = 0
    43  	testrequests["/a/"] = 1
    44  	testrequests["/a/b/"] = 2
    45  	testrequests["/x"] = 0
    46  	testrequests[""] = 0
    47  
    48  	expectedfailrequests := []string{"", "/x"}
    49  
    50  	reader := [3]*bytes.Reader{}
    51  
    52  	key := [3]storage.Key{}
    53  
    54  	srv := testutil.NewTestSwarmServer(t)
    55  	defer srv.Close()
    56  
    57  	wg := &sync.WaitGroup{}
    58  
    59  	for i, mf := range testmanifest {
    60  		reader[i] = bytes.NewReader([]byte(mf))
    61  		key[i], err = srv.Dpa.Store(reader[i], int64(len(mf)), wg, nil)
    62  		if err != nil {
    63  			t.Fatal(err)
    64  		}
    65  		wg.Wait()
    66  	}
    67  
    68  	_, err = http.Get(srv.URL + "/bzzr:/" + common.ToHex(key[0])[2:] + "/a")
    69  	if err != nil {
    70  		t.Fatalf("Failed to connect to proxy: %v", err)
    71  	}
    72  
    73  	for k, v := range testrequests {
    74  		var resp *http.Response
    75  		var respbody []byte
    76  
    77  		url := srv.URL + "/bzzr:/"
    78  		if k[:] != "" {
    79  			url += common.ToHex(key[0])[2:] + "/" + k[1:] + "?content_type=text/plain"
    80  		}
    81  		resp, err = http.Get(url)
    82  		if err != nil {
    83  			t.Fatalf("Request failed: %v", err)
    84  		}
    85  		defer resp.Body.Close()
    86  		respbody, err = ioutil.ReadAll(resp.Body)
    87  
    88  		if string(respbody) != testmanifest[v] {
    89  			isexpectedfailrequest := false
    90  
    91  			for _, r := range expectedfailrequests {
    92  				if k[:] == r {
    93  					isexpectedfailrequest = true
    94  				}
    95  			}
    96  			if isexpectedfailrequest == false {
    97  				t.Fatalf("Response body does not match, expected: %v, got %v", testmanifest[v], string(respbody))
    98  			}
    99  		}
   100  	}
   101  
   102  	nonhashtests := []string{
   103  		srv.URL + "/bzz:/name",
   104  		srv.URL + "/bzzi:/nonhash",
   105  		srv.URL + "/bzzr:/nonhash",
   106  	}
   107  
   108  	nonhashresponses := []string{
   109  		"error resolving name: no DNS to resolve name: \"name\"\n",
   110  		"error resolving nonhash: immutable address not a content hash: \"nonhash\"\n",
   111  		"error resolving nonhash: no DNS to resolve name: \"nonhash\"\n",
   112  	}
   113  
   114  	for i, url := range nonhashtests {
   115  		var resp *http.Response
   116  		var respbody []byte
   117  
   118  		resp, err = http.Get(url)
   119  
   120  		if err != nil {
   121  			t.Fatalf("Request failed: %v", err)
   122  		}
   123  		defer resp.Body.Close()
   124  		respbody, err = ioutil.ReadAll(resp.Body)
   125  		if string(respbody) != nonhashresponses[i] {
   126  			t.Fatalf("Non-Hash response body does not match, expected: %v, got: %v", nonhashresponses[i], string(respbody))
   127  		}
   128  	}
   129  
   130  }