needle_parse_test.go 5.6 KB

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