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