needle_parse_test.go 5.5 KB

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