github.com/gobitfly/go-ethereum@v1.8.12/swarm/api/http/error_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  	"encoding/json"
    21  	"io/ioutil"
    22  	"net/http"
    23  	"strings"
    24  	"testing"
    25  
    26  	"golang.org/x/net/html"
    27  
    28  	"github.com/ethereum/go-ethereum/swarm/testutil"
    29  )
    30  
    31  func TestError(t *testing.T) {
    32  
    33  	srv := testutil.NewTestSwarmServer(t, serverFunc)
    34  	defer srv.Close()
    35  
    36  	var resp *http.Response
    37  	var respbody []byte
    38  
    39  	url := srv.URL + "/this_should_fail_as_no_bzz_protocol_present"
    40  	resp, err := http.Get(url)
    41  
    42  	if err != nil {
    43  		t.Fatalf("Request failed: %v", err)
    44  	}
    45  	defer resp.Body.Close()
    46  	respbody, err = ioutil.ReadAll(resp.Body)
    47  
    48  	if resp.StatusCode != 400 && !strings.Contains(string(respbody), "Invalid URI &#34;/this_should_fail_as_no_bzz_protocol_present&#34;: unknown scheme") {
    49  		t.Fatalf("Response body does not match, expected: %v, to contain: %v; received code %d, expected code: %d", string(respbody), "Invalid bzz URI: unknown scheme", 400, resp.StatusCode)
    50  	}
    51  
    52  	_, err = html.Parse(strings.NewReader(string(respbody)))
    53  	if err != nil {
    54  		t.Fatalf("HTML validation failed for error page returned!")
    55  	}
    56  }
    57  
    58  func Test404Page(t *testing.T) {
    59  	srv := testutil.NewTestSwarmServer(t, serverFunc)
    60  	defer srv.Close()
    61  
    62  	var resp *http.Response
    63  	var respbody []byte
    64  
    65  	url := srv.URL + "/bzz:/1234567890123456789012345678901234567890123456789012345678901234"
    66  	resp, err := http.Get(url)
    67  
    68  	if err != nil {
    69  		t.Fatalf("Request failed: %v", err)
    70  	}
    71  	defer resp.Body.Close()
    72  	respbody, err = ioutil.ReadAll(resp.Body)
    73  
    74  	if resp.StatusCode != 404 || !strings.Contains(string(respbody), "404") {
    75  		t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode)
    76  	}
    77  
    78  	_, err = html.Parse(strings.NewReader(string(respbody)))
    79  	if err != nil {
    80  		t.Fatalf("HTML validation failed for error page returned!")
    81  	}
    82  }
    83  
    84  func Test500Page(t *testing.T) {
    85  	srv := testutil.NewTestSwarmServer(t, serverFunc)
    86  	defer srv.Close()
    87  
    88  	var resp *http.Response
    89  	var respbody []byte
    90  
    91  	url := srv.URL + "/bzz:/thisShouldFailWith500Code"
    92  	resp, err := http.Get(url)
    93  
    94  	if err != nil {
    95  		t.Fatalf("Request failed: %v", err)
    96  	}
    97  	defer resp.Body.Close()
    98  	respbody, err = ioutil.ReadAll(resp.Body)
    99  
   100  	if resp.StatusCode != 404 {
   101  		t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode)
   102  	}
   103  
   104  	_, err = html.Parse(strings.NewReader(string(respbody)))
   105  	if err != nil {
   106  		t.Fatalf("HTML validation failed for error page returned!")
   107  	}
   108  }
   109  func Test500PageWith0xHashPrefix(t *testing.T) {
   110  	srv := testutil.NewTestSwarmServer(t, serverFunc)
   111  	defer srv.Close()
   112  
   113  	var resp *http.Response
   114  	var respbody []byte
   115  
   116  	url := srv.URL + "/bzz:/0xthisShouldFailWith500CodeAndAHelpfulMessage"
   117  	resp, err := http.Get(url)
   118  
   119  	if err != nil {
   120  		t.Fatalf("Request failed: %v", err)
   121  	}
   122  	defer resp.Body.Close()
   123  	respbody, err = ioutil.ReadAll(resp.Body)
   124  
   125  	if resp.StatusCode != 404 {
   126  		t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode)
   127  	}
   128  
   129  	if !strings.Contains(string(respbody), "The requested hash seems to be prefixed with") {
   130  		t.Fatalf("Did not receive the expected error message")
   131  	}
   132  
   133  	_, err = html.Parse(strings.NewReader(string(respbody)))
   134  	if err != nil {
   135  		t.Fatalf("HTML validation failed for error page returned!")
   136  	}
   137  }
   138  
   139  func TestJsonResponse(t *testing.T) {
   140  	srv := testutil.NewTestSwarmServer(t, serverFunc)
   141  	defer srv.Close()
   142  
   143  	var resp *http.Response
   144  	var respbody []byte
   145  
   146  	url := srv.URL + "/bzz:/thisShouldFailWith500Code/"
   147  	req, err := http.NewRequest("GET", url, nil)
   148  	if err != nil {
   149  		t.Fatalf("Request failed: %v", err)
   150  	}
   151  	req.Header.Set("Accept", "application/json")
   152  	resp, err = http.DefaultClient.Do(req)
   153  	if err != nil {
   154  		t.Fatalf("Request failed: %v", err)
   155  	}
   156  
   157  	defer resp.Body.Close()
   158  	respbody, err = ioutil.ReadAll(resp.Body)
   159  
   160  	if resp.StatusCode != 404 {
   161  		t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode)
   162  	}
   163  
   164  	if !isJSON(string(respbody)) {
   165  		t.Fatalf("Expected response to be JSON, received invalid JSON: %s", string(respbody))
   166  	}
   167  
   168  }
   169  
   170  func isJSON(s string) bool {
   171  	var js map[string]interface{}
   172  	return json.Unmarshal([]byte(s), &js) == nil
   173  }