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