needle_parse_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. mc.needleHandling = func(n *needle.Needle, originalSize int, err error) {
  66. assert.Equal(t, nil, err, "upload: %v", err)
  67. assert.Equal(t, "text/plain", string(n.Mime), "mime detection failed: %v", string(n.Mime))
  68. assert.Equal(t, true, n.IsCompressed(), "this should be compressed")
  69. assert.Equal(t, true, util.IsZstdContent(n.Data), "this should be zstd")
  70. fmt.Printf("needle: %v, dataSize:%d originalSize:%d\n", n, len(n.Data), originalSize)
  71. }
  72. zstdData, _ := util.ZstdData([]byte(textContent))
  73. Upload("http://localhost:8080/389,0f084d17353afda0", "t.txt", false, bytes.NewReader(zstdData), true, "text/plain", nil, "")
  74. }
  75. {
  76. mc.needleHandling = func(n *needle.Needle, originalSize int, err error) {
  77. assert.Equal(t, nil, err, "upload: %v", err)
  78. assert.Equal(t, "application/zstd", string(n.Mime), "mime detection failed: %v", string(n.Mime))
  79. assert.Equal(t, false, n.IsCompressed(), "this should not be compressed")
  80. assert.Equal(t, true, util.IsZstdContent(n.Data), "this should still be zstd")
  81. fmt.Printf("needle: %v, dataSize:%d originalSize:%d\n", n, len(n.Data), originalSize)
  82. }
  83. zstdData, _ := util.ZstdData([]byte(textContent))
  84. Upload("http://localhost:8080/389,0f084d17353afda0", "t.txt", false, bytes.NewReader(zstdData), false, "application/zstd", nil, "")
  85. }
  86. }
  87. var textContent = `Redistribution and use in source and binary forms, with or without
  88. modification, are permitted provided that the following conditions are
  89. met:
  90. * Redistributions of source code must retain the above copyright
  91. notice, this list of conditions and the following disclaimer.
  92. * Redistributions in binary form must reproduce the above
  93. copyright notice, this list of conditions and the following disclaimer
  94. in the documentation and/or other materials provided with the
  95. distribution.
  96. * Neither the name of Google Inc. nor the names of its
  97. contributors may be used to endorse or promote products derived from
  98. this software without specific prior written permission.
  99. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  100. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  101. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  102. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  103. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  104. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  105. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  106. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  107. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  108. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  109. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  110. `