github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/api/http/error_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package http_test 13 14 import ( 15 "encoding/json" 16 "golang.org/x/net/html" 17 "io/ioutil" 18 "net/http" 19 "strings" 20 "testing" 21 22 "github.com/Sberex/go-sberex/swarm/testutil" 23 ) 24 25 func TestError(t *testing.T) { 26 27 srv := testutil.NewTestSwarmServer(t) 28 defer srv.Close() 29 30 var resp *http.Response 31 var respbody []byte 32 33 url := srv.URL + "/this_should_fail_as_no_bzz_protocol_present" 34 resp, err := http.Get(url) 35 36 if err != nil { 37 t.Fatalf("Request failed: %v", err) 38 } 39 defer resp.Body.Close() 40 respbody, err = ioutil.ReadAll(resp.Body) 41 42 if resp.StatusCode != 400 && !strings.Contains(string(respbody), "Invalid URI "/this_should_fail_as_no_bzz_protocol_present": unknown scheme") { 43 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) 44 } 45 46 _, err = html.Parse(strings.NewReader(string(respbody))) 47 if err != nil { 48 t.Fatalf("HTML validation failed for error page returned!") 49 } 50 } 51 52 func Test404Page(t *testing.T) { 53 srv := testutil.NewTestSwarmServer(t) 54 defer srv.Close() 55 56 var resp *http.Response 57 var respbody []byte 58 59 url := srv.URL + "/bzz:/1234567890123456789012345678901234567890123456789012345678901234" 60 resp, err := http.Get(url) 61 62 if err != nil { 63 t.Fatalf("Request failed: %v", err) 64 } 65 defer resp.Body.Close() 66 respbody, err = ioutil.ReadAll(resp.Body) 67 68 if resp.StatusCode != 404 || !strings.Contains(string(respbody), "404") { 69 t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode) 70 } 71 72 _, err = html.Parse(strings.NewReader(string(respbody))) 73 if err != nil { 74 t.Fatalf("HTML validation failed for error page returned!") 75 } 76 } 77 78 func Test500Page(t *testing.T) { 79 srv := testutil.NewTestSwarmServer(t) 80 defer srv.Close() 81 82 var resp *http.Response 83 var respbody []byte 84 85 url := srv.URL + "/bzz:/thisShouldFailWith500Code" 86 resp, err := http.Get(url) 87 88 if err != nil { 89 t.Fatalf("Request failed: %v", err) 90 } 91 defer resp.Body.Close() 92 respbody, err = ioutil.ReadAll(resp.Body) 93 94 if resp.StatusCode != 500 || !strings.Contains(string(respbody), "500") { 95 t.Fatalf("Invalid Status Code received, expected 500, got %d", resp.StatusCode) 96 } 97 98 _, err = html.Parse(strings.NewReader(string(respbody))) 99 if err != nil { 100 t.Fatalf("HTML validation failed for error page returned!") 101 } 102 } 103 104 func TestJsonResponse(t *testing.T) { 105 srv := testutil.NewTestSwarmServer(t) 106 defer srv.Close() 107 108 var resp *http.Response 109 var respbody []byte 110 111 url := srv.URL + "/bzz:/thisShouldFailWith500Code/" 112 req, err := http.NewRequest("GET", url, nil) 113 if err != nil { 114 t.Fatalf("Request failed: %v", err) 115 } 116 req.Header.Set("Accept", "application/json") 117 resp, err = http.DefaultClient.Do(req) 118 if err != nil { 119 t.Fatalf("Request failed: %v", err) 120 } 121 122 defer resp.Body.Close() 123 respbody, err = ioutil.ReadAll(resp.Body) 124 125 if resp.StatusCode != 500 { 126 t.Fatalf("Invalid Status Code received, expected 500, got %d", resp.StatusCode) 127 } 128 129 if !isJSON(string(respbody)) { 130 t.Fatalf("Expected response to be JSON, received invalid JSON: %s", string(respbody)) 131 } 132 133 } 134 135 func isJSON(s string) bool { 136 var js map[string]interface{} 137 return json.Unmarshal([]byte(s), &js) == nil 138 }