needle_parse_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package operation
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. type MockClient struct {
  13. needleHandling func(n *needle.Needle, originalSize int, e error)
  14. }
  15. func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
  16. n, originalSize, _, err := needle.CreateNeedleFromRequest(req, false, 1024*1024)
  17. if m.needleHandling != nil {
  18. m.needleHandling(n, originalSize, err)
  19. }
  20. return &http.Response{
  21. StatusCode: http.StatusNoContent,
  22. }, io.EOF
  23. }
  24. /*
  25. The mime type is always the value passed in.
  26. Compress or not depends on the content detection, file name extension, and compression ratio.
  27. If the content is already compressed, need to know the content size.
  28. */
  29. func TestCreateNeedleFromRequest(t *testing.T) {
  30. mc := &MockClient{}
  31. tmp := HttpClient
  32. HttpClient = mc
  33. defer func() {
  34. HttpClient = tmp
  35. }()
  36. {
  37. mc.needleHandling = func(n *needle.Needle, originalSize int, err error) {
  38. assert.Equal(t, nil, err, "upload: %v", err)
  39. assert.Equal(t, "", string(n.Mime), "mime detection failed: %v", string(n.Mime))
  40. assert.Equal(t, true, n.IsCompressed(), "this should be compressed")
  41. assert.Equal(t, true, util.IsGzippedContent(n.Data), "this should be gzip")
  42. fmt.Printf("needle: %v, originalSize: %d\n", n, originalSize)
  43. }
  44. uploadResult, err, data := Upload("http://localhost:8080/389,0f084d17353afda0", "t.txt", false, bytes.NewReader([]byte(textContent)), false, "", nil, "")
  45. if len(data) != len(textContent) {
  46. t.Errorf("data actual %d expected %d", len(data), len(textContent))
  47. }
  48. if err != nil {
  49. fmt.Printf("err: %v\n", err)
  50. }
  51. fmt.Printf("uploadResult: %+v\n", uploadResult)
  52. }
  53. {
  54. mc.needleHandling = func(n *needle.Needle, originalSize int, err error) {
  55. assert.Equal(t, nil, err, "upload: %v", err)
  56. assert.Equal(t, "text/plain", string(n.Mime), "mime detection failed: %v", string(n.Mime))
  57. assert.Equal(t, true, n.IsCompressed(), "this should be compressed")
  58. assert.Equal(t, true, util.IsGzippedContent(n.Data), "this should be gzip")
  59. fmt.Printf("needle: %v, dataSize:%d originalSize:%d\n", n, len(n.Data), originalSize)
  60. }
  61. gzippedData, _ := util.GzipData([]byte(textContent))
  62. Upload("http://localhost:8080/389,0f084d17353afda0", "t.txt", false, bytes.NewReader(gzippedData), true, "text/plain", nil, "")
  63. }
  64. /*
  65. {
  66. mc.needleHandling = func(n *needle.Needle, originalSize int, err error) {
  67. assert.Equal(t, nil, err, "upload: %v", err)
  68. assert.Equal(t, "text/plain", string(n.Mime), "mime detection failed: %v", string(n.Mime))
  69. assert.Equal(t, true, n.IsCompressed(), "this should be compressed")
  70. assert.Equal(t, true, util.IsZstdContent(n.Data), "this should be zstd")
  71. fmt.Printf("needle: %v, dataSize:%d originalSize:%d\n", n, len(n.Data), originalSize)
  72. }
  73. zstdData, _ := util.ZstdData([]byte(textContent))
  74. Upload("http://localhost:8080/389,0f084d17353afda0", "t.txt", false, bytes.NewReader(zstdData), true, "text/plain", nil, "")
  75. }
  76. {
  77. mc.needleHandling = func(n *needle.Needle, originalSize int, err error) {
  78. assert.Equal(t, nil, err, "upload: %v", err)
  79. assert.Equal(t, "application/zstd", string(n.Mime), "mime detection failed: %v", string(n.Mime))
  80. assert.Equal(t, false, n.IsCompressed(), "this should not be compressed")
  81. assert.Equal(t, true, util.IsZstdContent(n.Data), "this should still be zstd")
  82. fmt.Printf("needle: %v, dataSize:%d originalSize:%d\n", n, len(n.Data), originalSize)
  83. }
  84. zstdData, _ := util.ZstdData([]byte(textContent))
  85. Upload("http://localhost:8080/389,0f084d17353afda0", "t.txt", false, bytes.NewReader(zstdData), false, "application/zstd", nil, "")
  86. }
  87. */
  88. }
  89. var textContent = `Redistribution and use in source and binary forms, with or without
  90. modification, are permitted provided that the following conditions are
  91. met:
  92. * Redistributions of source code must retain the above copyright
  93. notice, this list of conditions and the following disclaimer.
  94. * Redistributions in binary form must reproduce the above
  95. copyright notice, this list of conditions and the following disclaimer
  96. in the documentation and/or other materials provided with the
  97. distribution.
  98. * Neither the name of Google Inc. nor the names of its
  99. contributors may be used to endorse or promote products derived from
  100. this software without specific prior written permission.
  101. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  102. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  103. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  104. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  105. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  106. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  107. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  108. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  109. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  110. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  111. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  112. `