error_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "golang.org/x/net/html"
  20. "io/ioutil"
  21. "net/http"
  22. "strings"
  23. "testing"
  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 != 500 || !strings.Contains(string(respbody), "500") {
  79. t.Fatalf("Invalid Status Code received, expected 500, 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 TestJsonResponse(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:/thisShouldFailWith500Code/"
  92. req, err := http.NewRequest("GET", url, nil)
  93. if err != nil {
  94. t.Fatalf("Request failed: %v", err)
  95. }
  96. req.Header.Set("Accept", "application/json")
  97. resp, err = http.DefaultClient.Do(req)
  98. if err != nil {
  99. t.Fatalf("Request failed: %v", err)
  100. }
  101. defer resp.Body.Close()
  102. respbody, err = ioutil.ReadAll(resp.Body)
  103. if resp.StatusCode != 500 {
  104. t.Fatalf("Invalid Status Code received, expected 500, got %d", resp.StatusCode)
  105. }
  106. if !isJSON(string(respbody)) {
  107. t.Fatalf("Expected response to be JSON, received invalid JSON: %s", string(respbody))
  108. }
  109. }
  110. func isJSON(s string) bool {
  111. var js map[string]interface{}
  112. return json.Unmarshal([]byte(s), &js) == nil
  113. }