error_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. package http_test
  17. import (
  18. "encoding/json"
  19. "io/ioutil"
  20. "net/http"
  21. "strings"
  22. "testing"
  23. "golang.org/x/net/html"
  24. "github.com/ethereum/go-ethereum/swarm/testutil"
  25. )
  26. func TestError(t *testing.T) {
  27. srv := testutil.NewTestSwarmServer(t)
  28. defer srv.Close()
  29. var resp *http.Response
  30. var respbody []byte
  31. url := srv.URL + "/this_should_fail_as_no_bzz_protocol_present"
  32. resp, err := http.Get(url)
  33. if err != nil {
  34. t.Fatalf("Request failed: %v", err)
  35. }
  36. defer resp.Body.Close()
  37. respbody, err = ioutil.ReadAll(resp.Body)
  38. if resp.StatusCode != 400 && !strings.Contains(string(respbody), "Invalid URI &#34;/this_should_fail_as_no_bzz_protocol_present&#34;: unknown scheme") {
  39. 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)
  40. }
  41. _, err = html.Parse(strings.NewReader(string(respbody)))
  42. if err != nil {
  43. t.Fatalf("HTML validation failed for error page returned!")
  44. }
  45. }
  46. func Test404Page(t *testing.T) {
  47. srv := testutil.NewTestSwarmServer(t)
  48. defer srv.Close()
  49. var resp *http.Response
  50. var respbody []byte
  51. url := srv.URL + "/bzz:/1234567890123456789012345678901234567890123456789012345678901234"
  52. resp, err := http.Get(url)
  53. if err != nil {
  54. t.Fatalf("Request failed: %v", err)
  55. }
  56. defer resp.Body.Close()
  57. respbody, err = ioutil.ReadAll(resp.Body)
  58. if resp.StatusCode != 404 || !strings.Contains(string(respbody), "404") {
  59. t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode)
  60. }
  61. _, err = html.Parse(strings.NewReader(string(respbody)))
  62. if err != nil {
  63. t.Fatalf("HTML validation failed for error page returned!")
  64. }
  65. }
  66. func Test500Page(t *testing.T) {
  67. srv := testutil.NewTestSwarmServer(t)
  68. defer srv.Close()
  69. var resp *http.Response
  70. var respbody []byte
  71. url := srv.URL + "/bzz:/thisShouldFailWith500Code"
  72. resp, err := http.Get(url)
  73. if err != nil {
  74. t.Fatalf("Request failed: %v", err)
  75. }
  76. defer resp.Body.Close()
  77. respbody, err = ioutil.ReadAll(resp.Body)
  78. if resp.StatusCode != 404 {
  79. t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode)
  80. }
  81. _, err = html.Parse(strings.NewReader(string(respbody)))
  82. if err != nil {
  83. t.Fatalf("HTML validation failed for error page returned!")
  84. }
  85. }
  86. func Test500PageWith0xHashPrefix(t *testing.T) {
  87. srv := testutil.NewTestSwarmServer(t)
  88. defer srv.Close()
  89. var resp *http.Response
  90. var respbody []byte
  91. url := srv.URL + "/bzz:/0xthisShouldFailWith500CodeAndAHelpfulMessage"
  92. resp, err := http.Get(url)
  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. if resp.StatusCode != 404 {
  99. t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode)
  100. }
  101. if !strings.Contains(string(respbody), "The requested hash seems to be prefixed with") {
  102. t.Fatalf("Did not receive the expected error message")
  103. }
  104. _, err = html.Parse(strings.NewReader(string(respbody)))
  105. if err != nil {
  106. t.Fatalf("HTML validation failed for error page returned!")
  107. }
  108. }
  109. func TestJsonResponse(t *testing.T) {
  110. srv := testutil.NewTestSwarmServer(t)
  111. defer srv.Close()
  112. var resp *http.Response
  113. var respbody []byte
  114. url := srv.URL + "/bzz:/thisShouldFailWith500Code/"
  115. req, err := http.NewRequest("GET", url, nil)
  116. if err != nil {
  117. t.Fatalf("Request failed: %v", err)
  118. }
  119. req.Header.Set("Accept", "application/json")
  120. resp, err = http.DefaultClient.Do(req)
  121. if err != nil {
  122. t.Fatalf("Request failed: %v", err)
  123. }
  124. defer resp.Body.Close()
  125. respbody, err = ioutil.ReadAll(resp.Body)
  126. if resp.StatusCode != 404 {
  127. t.Fatalf("Invalid Status Code received, expected 404, got %d", resp.StatusCode)
  128. }
  129. if !isJSON(string(respbody)) {
  130. t.Fatalf("Expected response to be JSON, received invalid JSON: %s", string(respbody))
  131. }
  132. }
  133. func isJSON(s string) bool {
  134. var js map[string]interface{}
  135. return json.Unmarshal([]byte(s), &js) == nil
  136. }