github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/api/http/response_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package http 26 27 import ( 28 "encoding/json" 29 "io/ioutil" 30 "net/http" 31 "strings" 32 "testing" 33 34 "golang.org/x/net/html" 35 36 "github.com/ethereum/go-ethereum/swarm/testutil" 37 ) 38 39 func TestError(t *testing.T) { 40 srv := testutil.NewTestSwarmServer(t, serverFunc) 41 defer srv.Close() 42 43 var resp *http.Response 44 var respbody []byte 45 46 url := srv.URL + "/this_should_fail_as_no_bzz_protocol_present" 47 resp, err := http.Get(url) 48 49 if err != nil { 50 t.Fatalf("Request failed: %v", err) 51 } 52 defer resp.Body.Close() 53 respbody, err = ioutil.ReadAll(resp.Body) 54 55 if resp.StatusCode != 404 && !strings.Contains(string(respbody), "Invalid URI "/this_should_fail_as_no_bzz_protocol_present": unknown scheme") { 56 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) 57 } 58 59 _, err = html.Parse(strings.NewReader(string(respbody))) 60 if err != nil { 61 t.Fatalf("HTML validation failed for error page returned!") 62 } 63 } 64 65 func Test404Page(t *testing.T) { 66 srv := testutil.NewTestSwarmServer(t, serverFunc) 67 defer srv.Close() 68 69 var resp *http.Response 70 var respbody []byte 71 72 url := srv.URL + "/bzz:/1234567890123456789012345678901234567890123456789012345678901234" 73 resp, err := http.Get(url) 74 75 if err != nil { 76 t.Fatalf("Request failed: %v", err) 77 } 78 defer resp.Body.Close() 79 respbody, err = ioutil.ReadAll(resp.Body) 80 81 if resp.StatusCode != 404 || !strings.Contains(string(respbody), "404") { 82 t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode) 83 } 84 85 _, err = html.Parse(strings.NewReader(string(respbody))) 86 if err != nil { 87 t.Fatalf("HTML validation failed for error page returned!") 88 } 89 } 90 91 func Test500Page(t *testing.T) { 92 srv := testutil.NewTestSwarmServer(t, serverFunc) 93 defer srv.Close() 94 95 var resp *http.Response 96 var respbody []byte 97 98 url := srv.URL + "/bzz:/thisShouldFailWith500Code" 99 resp, err := http.Get(url) 100 101 if err != nil { 102 t.Fatalf("Request failed: %v", err) 103 } 104 defer resp.Body.Close() 105 respbody, err = ioutil.ReadAll(resp.Body) 106 107 if resp.StatusCode != 404 { 108 t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode) 109 } 110 111 _, err = html.Parse(strings.NewReader(string(respbody))) 112 if err != nil { 113 t.Fatalf("HTML validation failed for error page returned!") 114 } 115 } 116 func Test500PageWith0xHashPrefix(t *testing.T) { 117 srv := testutil.NewTestSwarmServer(t, serverFunc) 118 defer srv.Close() 119 120 var resp *http.Response 121 var respbody []byte 122 123 url := srv.URL + "/bzz:/0xthisShouldFailWith500CodeAndAHelpfulMessage" 124 resp, err := http.Get(url) 125 126 if err != nil { 127 t.Fatalf("Request failed: %v", err) 128 } 129 defer resp.Body.Close() 130 respbody, err = ioutil.ReadAll(resp.Body) 131 132 if resp.StatusCode != 404 { 133 t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode) 134 } 135 136 if !strings.Contains(string(respbody), "The requested hash seems to be prefixed with") { 137 t.Fatalf("Did not receive the expected error message") 138 } 139 140 _, err = html.Parse(strings.NewReader(string(respbody))) 141 if err != nil { 142 t.Fatalf("HTML validation failed for error page returned!") 143 } 144 } 145 146 func TestJsonResponse(t *testing.T) { 147 srv := testutil.NewTestSwarmServer(t, serverFunc) 148 defer srv.Close() 149 150 var resp *http.Response 151 var respbody []byte 152 153 url := srv.URL + "/bzz:/thisShouldFailWith500Code/" 154 req, err := http.NewRequest("GET", url, nil) 155 if err != nil { 156 t.Fatalf("Request failed: %v", err) 157 } 158 req.Header.Set("Accept", "application/json") 159 resp, err = http.DefaultClient.Do(req) 160 if err != nil { 161 t.Fatalf("Request failed: %v", err) 162 } 163 164 defer resp.Body.Close() 165 respbody, err = ioutil.ReadAll(resp.Body) 166 167 if resp.StatusCode != 404 { 168 t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode) 169 } 170 171 if !isJSON(string(respbody)) { 172 t.Fatalf("Expected response to be JSON, received invalid JSON: %s", string(respbody)) 173 } 174 175 } 176 177 func isJSON(s string) bool { 178 var js map[string]interface{} 179 return json.Unmarshal([]byte(s), &js) == nil 180 }