docserver_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package docserver
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "os"
  6. "testing"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/crypto"
  9. )
  10. func TestGetAuthContent(t *testing.T) {
  11. text := "test"
  12. hash := common.Hash{}
  13. copy(hash[:], crypto.Sha3([]byte(text)))
  14. ioutil.WriteFile("/tmp/test.content", []byte(text), os.ModePerm)
  15. ds := New("/tmp/")
  16. content, err := ds.GetAuthContent("file:///test.content", hash)
  17. if err != nil {
  18. t.Errorf("no error expected, got %v", err)
  19. }
  20. if string(content) != text {
  21. t.Errorf("incorrect content. expected %v, got %v", text, string(content))
  22. }
  23. hash = common.Hash{}
  24. content, err = ds.GetAuthContent("file:///test.content", hash)
  25. expected := "content hash mismatch"
  26. if err == nil {
  27. t.Errorf("expected error, got nothing")
  28. } else {
  29. if err.Error() != expected {
  30. t.Errorf("expected error '%s' got '%v'", expected, err)
  31. }
  32. }
  33. }
  34. type rt struct{}
  35. func (rt) RoundTrip(req *http.Request) (resp *http.Response, err error) { return }
  36. func TestRegisterScheme(t *testing.T) {
  37. ds := New("/tmp/")
  38. if ds.HasScheme("scheme") {
  39. t.Errorf("expected scheme not to be registered")
  40. }
  41. ds.RegisterScheme("scheme", rt{})
  42. if !ds.HasScheme("scheme") {
  43. t.Errorf("expected scheme to be registered")
  44. }
  45. }