docserver_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package docserver
  17. import (
  18. "io/ioutil"
  19. "net/http"
  20. "os"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. )
  25. func TestGetAuthContent(t *testing.T) {
  26. text := "test"
  27. hash := common.Hash{}
  28. copy(hash[:], crypto.Sha3([]byte(text)))
  29. ioutil.WriteFile("/tmp/test.content", []byte(text), os.ModePerm)
  30. ds := New("/tmp/")
  31. content, err := ds.GetAuthContent("file:///test.content", hash)
  32. if err != nil {
  33. t.Errorf("no error expected, got %v", err)
  34. }
  35. if string(content) != text {
  36. t.Errorf("incorrect content. expected %v, got %v", text, string(content))
  37. }
  38. hash = common.Hash{}
  39. content, err = ds.GetAuthContent("file:///test.content", hash)
  40. expected := "content hash mismatch 0000000000000000000000000000000000000000000000000000000000000000 != 9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658 (exp)"
  41. if err == nil {
  42. t.Errorf("expected error, got nothing")
  43. } else {
  44. if err.Error() != expected {
  45. t.Errorf("expected error '%s' got '%v'", expected, err)
  46. }
  47. }
  48. }
  49. type rt struct{}
  50. func (rt) RoundTrip(req *http.Request) (resp *http.Response, err error) { return }
  51. func TestRegisterScheme(t *testing.T) {
  52. ds := New("/tmp/")
  53. if ds.HasScheme("scheme") {
  54. t.Errorf("expected scheme not to be registered")
  55. }
  56. ds.RegisterScheme("scheme", rt{})
  57. if !ds.HasScheme("scheme") {
  58. t.Errorf("expected scheme to be registered")
  59. }
  60. }