api_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2016 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 api
  17. import (
  18. "io"
  19. "io/ioutil"
  20. "os"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/logger"
  23. "github.com/ethereum/go-ethereum/logger/glog"
  24. "github.com/ethereum/go-ethereum/swarm/storage"
  25. )
  26. func testApi(t *testing.T, f func(*Api)) {
  27. datadir, err := ioutil.TempDir("", "bzz-test")
  28. if err != nil {
  29. t.Fatalf("unable to create temp dir: %v", err)
  30. }
  31. os.RemoveAll(datadir)
  32. defer os.RemoveAll(datadir)
  33. dpa, err := storage.NewLocalDPA(datadir)
  34. if err != nil {
  35. return
  36. }
  37. api := NewApi(dpa, nil)
  38. dpa.Start()
  39. f(api)
  40. dpa.Stop()
  41. }
  42. type testResponse struct {
  43. reader storage.LazySectionReader
  44. *Response
  45. }
  46. func checkResponse(t *testing.T, resp *testResponse, exp *Response) {
  47. if resp.MimeType != exp.MimeType {
  48. t.Errorf("incorrect mimeType. expected '%s', got '%s'", exp.MimeType, resp.MimeType)
  49. }
  50. if resp.Status != exp.Status {
  51. t.Errorf("incorrect status. expected '%d', got '%d'", exp.Status, resp.Status)
  52. }
  53. if resp.Size != exp.Size {
  54. t.Errorf("incorrect size. expected '%d', got '%d'", exp.Size, resp.Size)
  55. }
  56. if resp.reader != nil {
  57. content := make([]byte, resp.Size)
  58. read, _ := resp.reader.Read(content)
  59. if int64(read) != exp.Size {
  60. t.Errorf("incorrect content length. expected '%d...', got '%d...'", read, exp.Size)
  61. }
  62. resp.Content = string(content)
  63. }
  64. if resp.Content != exp.Content {
  65. // if !bytes.Equal(resp.Content, exp.Content)
  66. t.Errorf("incorrect content. expected '%s...', got '%s...'", string(exp.Content), string(resp.Content))
  67. }
  68. }
  69. // func expResponse(content []byte, mimeType string, status int) *Response {
  70. func expResponse(content string, mimeType string, status int) *Response {
  71. glog.V(logger.Detail).Infof("expected content (%v): %v ", len(content), content)
  72. return &Response{mimeType, status, int64(len(content)), content}
  73. }
  74. // func testGet(t *testing.T, api *Api, bzzhash string) *testResponse {
  75. func testGet(t *testing.T, api *Api, bzzhash string) *testResponse {
  76. reader, mimeType, status, err := api.Get(bzzhash, true)
  77. if err != nil {
  78. t.Fatalf("unexpected error: %v", err)
  79. }
  80. quitC := make(chan bool)
  81. size, err := reader.Size(quitC)
  82. if err != nil {
  83. t.Fatalf("unexpected error: %v", err)
  84. }
  85. glog.V(logger.Detail).Infof("reader size: %v ", size)
  86. s := make([]byte, size)
  87. _, err = reader.Read(s)
  88. if err != io.EOF {
  89. t.Fatalf("unexpected error: %v", err)
  90. }
  91. reader.Seek(0, 0)
  92. return &testResponse{reader, &Response{mimeType, status, size, string(s)}}
  93. // return &testResponse{reader, &Response{mimeType, status, reader.Size(), nil}}
  94. }
  95. func TestApiPut(t *testing.T) {
  96. testApi(t, func(api *Api) {
  97. content := "hello"
  98. exp := expResponse(content, "text/plain", 0)
  99. // exp := expResponse([]byte(content), "text/plain", 0)
  100. bzzhash, err := api.Put(content, exp.MimeType)
  101. if err != nil {
  102. t.Fatalf("unexpected error: %v", err)
  103. }
  104. resp := testGet(t, api, bzzhash)
  105. checkResponse(t, resp, exp)
  106. })
  107. }