You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.5 KiB

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