docserver_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2015 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 docserver
  17. import (
  18. "io/ioutil"
  19. "net/http"
  20. "os"
  21. "path"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. )
  26. func TestGetAuthContent(t *testing.T) {
  27. dir, err := ioutil.TempDir("", "docserver-test")
  28. if err != nil {
  29. t.Fatal("cannot create temporary directory:", err)
  30. }
  31. defer os.RemoveAll(dir)
  32. ds := New(dir)
  33. text := "test"
  34. hash := crypto.Sha3Hash([]byte(text))
  35. if err := ioutil.WriteFile(path.Join(dir, "test.content"), []byte(text), os.ModePerm); err != nil {
  36. t.Fatal("could not write test file", err)
  37. }
  38. content, err := ds.GetAuthContent("file:///test.content", hash)
  39. if err != nil {
  40. t.Errorf("no error expected, got %v", err)
  41. }
  42. if string(content) != text {
  43. t.Errorf("incorrect content. expected %v, got %v", text, string(content))
  44. }
  45. hash = common.Hash{}
  46. content, err = ds.GetAuthContent("file:///test.content", hash)
  47. expected := "content hash mismatch 0000000000000000000000000000000000000000000000000000000000000000 != 9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658 (exp)"
  48. if err == nil {
  49. t.Errorf("expected error, got nothing")
  50. } else {
  51. if err.Error() != expected {
  52. t.Errorf("expected error '%s' got '%v'", expected, err)
  53. }
  54. }
  55. }
  56. type rt struct{}
  57. func (rt) RoundTrip(req *http.Request) (resp *http.Response, err error) { return }
  58. func TestRegisterScheme(t *testing.T) {
  59. ds := New("/tmp/")
  60. if ds.HasScheme("scheme") {
  61. t.Errorf("expected scheme not to be registered")
  62. }
  63. ds.RegisterScheme("scheme", rt{})
  64. if !ds.HasScheme("scheme") {
  65. t.Errorf("expected scheme to be registered")
  66. }
  67. }