github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/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_test 18 19 import ( 20 "encoding/json" 21 "golang.org/x/net/html" 22 "io/ioutil" 23 "net/http" 24 "strings" 25 "testing" 26 27 "github.com/vantum/vantum/swarm/testutil" 28 ) 29 30 func TestError(t *testing.T) { 31 32 srv := testutil.NewTestSwarmServer(t) 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 != 400 && !strings.Contains(string(respbody), "Invalid URI "/this_should_fail_as_no_bzz_protocol_present": 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) 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) 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 != 500 || !strings.Contains(string(respbody), "500") { 100 t.Fatalf("Invalid Status Code received, expected 500, 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 109 func TestJsonResponse(t *testing.T) { 110 srv := testutil.NewTestSwarmServer(t) 111 defer srv.Close() 112 113 var resp *http.Response 114 var respbody []byte 115 116 url := srv.URL + "/bzz:/thisShouldFailWith500Code/" 117 req, err := http.NewRequest("GET", url, nil) 118 if err != nil { 119 t.Fatalf("Request failed: %v", err) 120 } 121 req.Header.Set("Accept", "application/json") 122 resp, err = http.DefaultClient.Do(req) 123 if err != nil { 124 t.Fatalf("Request failed: %v", err) 125 } 126 127 defer resp.Body.Close() 128 respbody, err = ioutil.ReadAll(resp.Body) 129 130 if resp.StatusCode != 500 { 131 t.Fatalf("Invalid Status Code received, expected 500, got %d", resp.StatusCode) 132 } 133 134 if !isJSON(string(respbody)) { 135 t.Fatalf("Expected response to be JSON, received invalid JSON: %s", string(respbody)) 136 } 137 138 } 139 140 func isJSON(s string) bool { 141 var js map[string]interface{} 142 return json.Unmarshal([]byte(s), &js) == nil 143 }