dir_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package v1
  2. import (
  3. "fmt"
  4. "testing"
  5. "devt.de/krotik/common/errorutil"
  6. "devt.de/krotik/rufs"
  7. "devt.de/krotik/rufs/api"
  8. "devt.de/krotik/rufs/config"
  9. )
  10. func TestDirQuery(t *testing.T) {
  11. adminQueryURL := "http://localhost" + TESTPORT + EndpointAdmin
  12. queryURL := "http://localhost" + TESTPORT + EndpointDir
  13. // Setup a tree
  14. defer func() {
  15. // Make sure all trees are removed
  16. api.ResetTrees()
  17. }()
  18. tree, err := rufs.NewTree(api.TreeConfigTemplate, api.TreeCertTemplate)
  19. errorutil.AssertOk(err)
  20. api.AddTree("Hans1", tree)
  21. fooRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
  22. fooFP := footest.SSLFingerprint()
  23. err = tree.AddBranch("footest", fooRPC, fooFP)
  24. errorutil.AssertOk(err)
  25. err = tree.AddMapping("/", "footest", false)
  26. errorutil.AssertOk(err)
  27. st, _, res := sendTestRequest(adminQueryURL, "GET", nil)
  28. if st != "200 OK" || res != fmt.Sprintf(`
  29. {
  30. "Hans1": {
  31. "branches": [
  32. {
  33. "branch": "footest",
  34. "fingerprint": %#v,
  35. "rpc": %#v
  36. }
  37. ],
  38. "tree": [
  39. {
  40. "branch": "footest",
  41. "path": "/",
  42. "writeable": false
  43. }
  44. ]
  45. }
  46. }`[1:], fooFP, fooRPC) {
  47. t.Error("Unexpected response:", st, res)
  48. return
  49. }
  50. // Get a directory listing
  51. st, _, res = sendTestRequest(queryURL+"Hans1", "GET", nil)
  52. if st != "200 OK" || res != `
  53. {
  54. "/": [
  55. {
  56. "isdir": true,
  57. "name": "sub1",
  58. "size": 4096
  59. },
  60. {
  61. "isdir": false,
  62. "name": "test1",
  63. "size": 10
  64. },
  65. {
  66. "isdir": false,
  67. "name": "test2",
  68. "size": 10
  69. }
  70. ]
  71. }`[1:] {
  72. t.Error("Unexpected response:", st, res)
  73. return
  74. }
  75. st, _, res = sendTestRequest(queryURL+"Hans1/sub1", "GET", nil)
  76. if st != "200 OK" || res != `
  77. {
  78. "/sub1": [
  79. {
  80. "isdir": false,
  81. "name": "test3",
  82. "size": 17
  83. }
  84. ]
  85. }`[1:] {
  86. t.Error("Unexpected response:", st, res)
  87. return
  88. }
  89. // Test recursive with checksums
  90. st, _, res = sendTestRequest(queryURL+"Hans1?recursive=TRUE&checksums=1", "GET", nil)
  91. if st != "200 OK" || res != `
  92. {
  93. "/": [
  94. {
  95. "checksum": "",
  96. "isdir": true,
  97. "name": "sub1",
  98. "size": 4096
  99. },
  100. {
  101. "checksum": "73b8af47",
  102. "isdir": false,
  103. "name": "test1",
  104. "size": 10
  105. },
  106. {
  107. "checksum": "b0c1fadd",
  108. "isdir": false,
  109. "name": "test2",
  110. "size": 10
  111. }
  112. ],
  113. "/sub1": [
  114. {
  115. "checksum": "f89782b1",
  116. "isdir": false,
  117. "name": "test3",
  118. "size": 17
  119. }
  120. ]
  121. }`[1:] {
  122. t.Error("Unexpected response:", st, res)
  123. return
  124. }
  125. // Test error cases
  126. st, _, res = sendTestRequest(queryURL, "GET", nil)
  127. if st != "400 Bad Request" || res != "Need at least a tree name" {
  128. t.Error("Unexpected response:", st, res)
  129. return
  130. }
  131. st, _, res = sendTestRequest(queryURL+"dave", "GET", nil)
  132. if st != "400 Bad Request" || res != "Unknown tree: dave" {
  133. t.Error("Unexpected response:", st, res)
  134. return
  135. }
  136. }