zip_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package v1
  2. import (
  3. "archive/zip"
  4. "bytes"
  5. "fmt"
  6. "testing"
  7. "devt.de/krotik/common/errorutil"
  8. "devt.de/krotik/rufs"
  9. "devt.de/krotik/rufs/api"
  10. "devt.de/krotik/rufs/config"
  11. )
  12. func TestZipDownload(t *testing.T) {
  13. queryURL := "http://localhost" + TESTPORT + EndpointDir
  14. zipURL := "http://localhost" + TESTPORT + EndpointZip
  15. // Setup a tree
  16. defer func() {
  17. // Make sure all trees are removed
  18. api.ResetTrees()
  19. }()
  20. tree, err := rufs.NewTree(api.TreeConfigTemplate, api.TreeCertTemplate)
  21. errorutil.AssertOk(err)
  22. api.AddTree("Hans1", tree)
  23. fooRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
  24. fooFP := footest.SSLFingerprint()
  25. err = tree.AddBranch("footest", fooRPC, fooFP)
  26. errorutil.AssertOk(err)
  27. err = tree.AddMapping("/", "footest", false)
  28. errorutil.AssertOk(err)
  29. st, _, res := sendTestRequest(queryURL+"Hans1?recursive=1", "GET", nil)
  30. if st != "200 OK" || res != `
  31. {
  32. "/": [
  33. {
  34. "isdir": true,
  35. "name": "sub1",
  36. "size": 4096
  37. },
  38. {
  39. "isdir": false,
  40. "name": "test1",
  41. "size": 10
  42. },
  43. {
  44. "isdir": false,
  45. "name": "test2",
  46. "size": 10
  47. }
  48. ],
  49. "/sub1": [
  50. {
  51. "isdir": false,
  52. "name": "test3",
  53. "size": 17
  54. }
  55. ]
  56. }`[1:] {
  57. t.Error("Unexpected response:", st, res)
  58. return
  59. }
  60. st, _, res = sendTestRequest(zipURL+"Hans1", "FORMPOST",
  61. []byte(`files=["/test1", "/test2", "/sub1/test3"]`))
  62. if st != "200 OK" {
  63. t.Error("Unexpected response:", st, res)
  64. return
  65. }
  66. r, err := zip.NewReader(bytes.NewReader([]byte(res)), int64(len(res)))
  67. if err != nil {
  68. t.Error(err)
  69. return
  70. }
  71. if res := len(r.File); res != 3 {
  72. t.Error("Unexpected result:", res)
  73. return
  74. }
  75. if r.File[0].Name != "/test1" {
  76. t.Error("Unexpected result:", r.File[0].Name)
  77. return
  78. }
  79. if r.File[1].Name != "/test2" {
  80. t.Error("Unexpected result:", r.File[1].Name)
  81. return
  82. }
  83. if r.File[2].Name != "/sub1/test3" {
  84. t.Error("Unexpected result:", r.File[2].Name)
  85. return
  86. }
  87. if r.File[2].UncompressedSize != 17 {
  88. t.Error("Unexpected result:", r.File[2].UncompressedSize)
  89. return
  90. }
  91. // Test error cases
  92. st, _, res = sendTestRequest(zipURL, "POST", nil)
  93. if st != "400 Bad Request" || res != "Need a tree name" {
  94. t.Error("Unexpected response:", st, res)
  95. return
  96. }
  97. st, _, res = sendTestRequest(zipURL+"dave", "POST", nil)
  98. if st != "400 Bad Request" || res != "Unknown tree: dave" {
  99. t.Error("Unexpected response:", st, res)
  100. return
  101. }
  102. st, _, res = sendTestRequest(zipURL+"Hans1", "POST", nil)
  103. if st != "400 Bad Request" || res != "Could not decode request body: Field 'files' should be a list of files as JSON encoded string" {
  104. t.Error("Unexpected response:", st, res)
  105. return
  106. }
  107. }