1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087 |
- package v1
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "mime/multipart"
- "net/http"
- "os"
- "strings"
- "testing"
- "time"
- "devt.de/krotik/common/datautil"
- "devt.de/krotik/common/errorutil"
- "devt.de/krotik/rufs"
- "devt.de/krotik/rufs/api"
- "devt.de/krotik/rufs/config"
- )
- func TestFileSync(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointFile
- dirqueryURL := "http://localhost" + TESTPORT + EndpointDir
- progressqueryURL := "http://localhost" + TESTPORT + EndpointProgress
- // Setup a tree
- defer func() {
- // Make sure all trees are removed
- api.ResetTrees()
- }()
- tree, err := rufs.NewTree(api.TreeConfigTemplate, api.TreeCertTemplate)
- errorutil.AssertOk(err)
- api.AddTree("Hans1", tree)
- fooRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
- fooFP := footest.SSLFingerprint()
- err = tree.AddBranch("footest", fooRPC, fooFP)
- errorutil.AssertOk(err)
- err = tree.AddMapping("/", "footest", true)
- errorutil.AssertOk(err)
- // Make the target directory
- os.Mkdir("foo/sync1", 0755)
- defer func() {
- errorutil.AssertOk(os.RemoveAll("foo/sync1"))
- }()
- st, _, res := sendTestRequest(dirqueryURL+"Hans1?recursive=TRUE&checksums=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "/": [
- {
- "checksum": "",
- "isdir": true,
- "name": "sub1",
- "size": 4096
- },
- {
- "checksum": "",
- "isdir": true,
- "name": "sync1",
- "size": 4096
- },
- {
- "checksum": "73b8af47",
- "isdir": false,
- "name": "test1",
- "size": 10
- },
- {
- "checksum": "b0c1fadd",
- "isdir": false,
- "name": "test2",
- "size": 10
- }
- ],
- "/sub1": [
- {
- "checksum": "f89782b1",
- "isdir": false,
- "name": "test3",
- "size": 17
- }
- ],
- "/sync1": null
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Do the sync
- st, _, res = sendTestRequest(queryURL+"Hans1/", "PUT", []byte(`
- {
- "action" : "sync",
- "destination" : "/sync1"
- }`))
- if st != "200 OK" {
- t.Error("Unexpected response:", st, res)
- return
- }
- var resMap map[string]interface{}
- json.Unmarshal([]byte(res), &resMap)
- pid := fmt.Sprint(resMap["progress_id"])
- for {
- st, _, res = sendTestRequest(progressqueryURL+"Hans1/"+pid, "GET", nil)
- json.Unmarshal([]byte(res), &resMap)
- if st != "200 OK" ||
- len(resMap["errors"].([]interface{})) > 0 ||
- (resMap["progress"] == resMap["total_progress"] && resMap["item"] == resMap["total_items"]) {
- break
- }
- }
- if st != "200 OK" || res != `
- {
- "errors": [],
- "item": 5,
- "operation": "Copy file",
- "progress": 17,
- "subject": "/sub1/test3",
- "total_items": 5,
- "total_progress": 17
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- time.Sleep(5 * time.Millisecond) // Wait a small time so all operations are guaranteed to finish
- st, _, res = sendTestRequest(dirqueryURL+"Hans1?recursive=TRUE&checksums=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "/": [
- {
- "checksum": "",
- "isdir": true,
- "name": "sub1",
- "size": 4096
- },
- {
- "checksum": "",
- "isdir": true,
- "name": "sync1",
- "size": 4096
- },
- {
- "checksum": "73b8af47",
- "isdir": false,
- "name": "test1",
- "size": 10
- },
- {
- "checksum": "b0c1fadd",
- "isdir": false,
- "name": "test2",
- "size": 10
- }
- ],
- "/sub1": [
- {
- "checksum": "f89782b1",
- "isdir": false,
- "name": "test3",
- "size": 17
- }
- ],
- "/sync1": [
- {
- "checksum": "",
- "isdir": true,
- "name": "sub1",
- "size": 4096
- },
- {
- "checksum": "",
- "isdir": true,
- "name": "sync1",
- "size": 4096
- },
- {
- "checksum": "73b8af47",
- "isdir": false,
- "name": "test1",
- "size": 10
- },
- {
- "checksum": "b0c1fadd",
- "isdir": false,
- "name": "test2",
- "size": 10
- }
- ],
- "/sync1/sub1": [
- {
- "checksum": "f89782b1",
- "isdir": false,
- "name": "test3",
- "size": 17
- }
- ],
- "/sync1/sync1": null
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Test errors
- st, _, res = sendTestRequest(queryURL+"Hans1/", "PUT", []byte(`
- {
- "action" : "sync"
- }`))
- if st != "400 Bad Request" || res != "Parameter destination is missing from request body" {
- t.Error("Unexpected response:", st, res)
- return
- }
- tree.Reset(false)
- ProgressMap = datautil.NewMapCache(100, 0)
- err = tree.AddMapping("/", "footest", false)
- errorutil.AssertOk(err)
- st, _, res = sendTestRequest(queryURL+"Hans1/", "PUT", []byte(`
- {
- "action" : "sync",
- "destination" : "/sync1"
- }`))
- if st != "400 Bad Request" || res != "All applicable branches for the requested path were mounted as not writable" {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Check error in ProgressMap
- for k := range ProgressMap.GetAll() {
- k = strings.Split(k, "#")[1]
- _, _, res = sendTestRequest(progressqueryURL+"Hans1/"+k, "GET", nil)
- resMap = nil
- errorutil.AssertOk(json.Unmarshal([]byte(res), &resMap))
- if res := fmt.Sprint(resMap["errors"]); res != "[All applicable branches for the requested path were mounted as not writable]" {
- t.Error("Unexpected result:", res)
- }
- break
- }
- }
- func TestFileUpload(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointFile
- dirqueryURL := "http://localhost" + TESTPORT + EndpointDir
- // Setup a tree
- defer func() {
- // Make sure all trees are removed
- api.ResetTrees()
- }()
- tree, err := rufs.NewTree(api.TreeConfigTemplate, api.TreeCertTemplate)
- errorutil.AssertOk(err)
- api.AddTree("Hans1", tree)
- fooRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
- fooFP := footest.SSLFingerprint()
- err = tree.AddBranch("footest", fooRPC, fooFP)
- errorutil.AssertOk(err)
- err = tree.AddMapping("/", "footest", true)
- errorutil.AssertOk(err)
- // Send a file
- body := &bytes.Buffer{}
- writer := multipart.NewWriter(body)
- part, err := writer.CreateFormFile("uploadfile", "foo-upload.txt")
- errorutil.AssertOk(err)
- part.Write([]byte("footest"))
- part, err = writer.CreateFormFile("uploadfile", "bar-upload.txt")
- errorutil.AssertOk(err)
- part.Write([]byte("bartest"))
- writer.WriteField("redirect", "/foo/bar")
- writer.Close()
- req, err := http.NewRequest("POST", queryURL+"Hans1/", body)
- if err != nil {
- panic(err)
- }
- req.Header.Set("Content-Type", writer.FormDataContentType())
- client := &http.Client{}
- client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
- return http.ErrUseLastResponse
- }
- resp, err := client.Do(req)
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- respBody, _ := ioutil.ReadAll(resp.Body)
- respBodyStr := strings.Trim(string(respBody), " \n")
- if l, _ := resp.Location(); resp.Status != "302 Found" || l.String() != "http://localhost:9040/foo/bar" {
- t.Error("Unexpected response:", resp.Status, l.String(), respBodyStr)
- return
- }
- defer func() {
- // Remove the uploaded files after we are done
- os.RemoveAll("foo/foo-upload.txt")
- os.RemoveAll("foo/bar-upload.txt")
- }()
- // Check that the file has been written
- st, _, res := sendTestRequest(dirqueryURL+"Hans1?recursive=TRUE&checksums=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "/": [
- {
- "checksum": "bab796f6",
- "isdir": false,
- "name": "bar-upload.txt",
- "size": 7
- },
- {
- "checksum": "75f4b6fe",
- "isdir": false,
- "name": "foo-upload.txt",
- "size": 7
- },
- {
- "checksum": "",
- "isdir": true,
- "name": "sub1",
- "size": 4096
- },
- {
- "checksum": "73b8af47",
- "isdir": false,
- "name": "test1",
- "size": 10
- },
- {
- "checksum": "b0c1fadd",
- "isdir": false,
- "name": "test2",
- "size": 10
- }
- ],
- "/sub1": [
- {
- "checksum": "f89782b1",
- "isdir": false,
- "name": "test3",
- "size": 17
- }
- ]
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Check the files have been written correctly
- txt, err := ioutil.ReadFile("foo/foo-upload.txt")
- if err != nil || string(txt) != "footest" {
- t.Error("Unexpected result:", string(txt), err)
- return
- }
- txt, err = ioutil.ReadFile("foo/bar-upload.txt")
- if err != nil || string(txt) != "bartest" {
- t.Error("Unexpected result:", string(txt), err)
- return
- }
- // Test error cases
- st, _, res = sendTestRequest(queryURL, "POST", nil)
- if st != "400 Bad Request" || res != "Need a tree name and a file path" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans2", "POST", nil)
- if st != "400 Bad Request" || res != "Unknown tree: Hans2" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1", "POST", nil)
- if st != "400 Bad Request" || res != "Could not read request body: request Content-Type isn't multipart/form-data" {
- t.Error("Unexpected response:", st, res)
- return
- }
- writer = multipart.NewWriter(body)
- part, err = writer.CreateFormFile("uploadfile2", "foo-upload.txt")
- if err != nil {
- panic(err)
- }
- errorutil.AssertOk(err)
- part.Write([]byte("footest"))
- writer.Close()
- req, err = http.NewRequest("POST", queryURL+"Hans1/", body)
- if err != nil {
- panic(err)
- }
- req.Header.Set("Content-Type", writer.FormDataContentType())
- client = &http.Client{}
- resp, err = client.Do(req)
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- respBody, _ = ioutil.ReadAll(resp.Body)
- respBodyStr = strings.Trim(string(respBody), " \n")
- if resp.Status != "400 Bad Request" || respBodyStr != "Could not find 'uploadfile' form field" {
- t.Error("Unexpected response:", resp.Status, respBodyStr)
- return
- }
- // Try with a absolute redirect
- writer = multipart.NewWriter(body)
- part, err = writer.CreateFormFile("uploadfile", "foo-upload.txt")
- errorutil.AssertOk(err)
- part.Write([]byte("footest"))
- writer.WriteField("redirect", "http://bla")
- writer.Close()
- req, err = http.NewRequest("POST", queryURL+"Hans1/", body)
- if err != nil {
- panic(err)
- }
- req.Header.Set("Content-Type", writer.FormDataContentType())
- client = &http.Client{}
- resp, err = client.Do(req)
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- respBody, _ = ioutil.ReadAll(resp.Body)
- respBodyStr = strings.Trim(string(respBody), " \n")
- if resp.Status != "400 Bad Request" || respBodyStr != "Could not redirect: Redirection URL must not be an absolute URL" {
- t.Error("Unexpected response:", resp.Status, respBodyStr)
- return
- }
- // Remap branch as read-only
- tree.Reset(false)
- err = tree.AddMapping("/", "footest", false)
- errorutil.AssertOk(err)
- writer = multipart.NewWriter(body)
- part, err = writer.CreateFormFile("uploadfile", "foo-upload.txt")
- errorutil.AssertOk(err)
- part.Write([]byte("footest"))
- writer.Close()
- req, err = http.NewRequest("POST", queryURL+"Hans1/", body)
- if err != nil {
- panic(err)
- }
- req.Header.Set("Content-Type", writer.FormDataContentType())
- client = &http.Client{}
- resp, err = client.Do(req)
- if err != nil {
- panic(err)
- }
- defer resp.Body.Close()
- respBody, _ = ioutil.ReadAll(resp.Body)
- respBodyStr = strings.Trim(string(respBody), " \n")
- if resp.Status != "400 Bad Request" || respBodyStr != "Could not write file foo-upload.txt: All applicable branches for the requested path were mounted as not writable" {
- t.Error("Unexpected response:", resp.Status, respBodyStr)
- return
- }
- }
- func TestFileQuery(t *testing.T) {
- queryURL := "http://localhost" + TESTPORT + EndpointFile
- dirqueryURL := "http://localhost" + TESTPORT + EndpointDir
- progressqueryURL := "http://localhost" + TESTPORT + EndpointProgress
- // Setup a tree
- defer func() {
- // Make sure all trees are removed
- api.ResetTrees()
- }()
- tree, err := rufs.NewTree(api.TreeConfigTemplate, api.TreeCertTemplate)
- errorutil.AssertOk(err)
- api.AddTree("Hans1", tree)
- fooRPC := fmt.Sprintf("%v:%v", branchConfigs["footest"][config.RPCHost], branchConfigs["footest"][config.RPCPort])
- fooFP := footest.SSLFingerprint()
- err = tree.AddBranch("footest", fooRPC, fooFP)
- errorutil.AssertOk(err)
- err = tree.AddMapping("/", "footest", false)
- errorutil.AssertOk(err)
- barRPC := fmt.Sprintf("%v:%v", branchConfigs["bartest"][config.RPCHost], branchConfigs["bartest"][config.RPCPort])
- barFP := bartest.SSLFingerprint()
- err = tree.AddBranch("bartest", barRPC, barFP)
- errorutil.AssertOk(err)
- err = tree.AddMapping("/tmp", "bartest", true)
- errorutil.AssertOk(err)
- // Create a file for renaming and deletion
- ioutil.WriteFile("bar/newfile1", []byte("test123"), 0660)
- st, _, res := sendTestRequest(dirqueryURL+"Hans1?recursive=TRUE&checksums=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "/": [
- {
- "checksum": "",
- "isdir": true,
- "name": "sub1",
- "size": 4096
- },
- {
- "checksum": "73b8af47",
- "isdir": false,
- "name": "test1",
- "size": 10
- },
- {
- "checksum": "b0c1fadd",
- "isdir": false,
- "name": "test2",
- "size": 10
- },
- {
- "checksum": "",
- "isdir": true,
- "name": "tmp",
- "size": 0
- }
- ],
- "/sub1": [
- {
- "checksum": "f89782b1",
- "isdir": false,
- "name": "test3",
- "size": 17
- }
- ],
- "/tmp": [
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "newfile1",
- "size": 7
- },
- {
- "checksum": "5b62da0f",
- "isdir": false,
- "name": "test1",
- "size": 10
- }
- ]
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Read a file
- st, _, res = sendTestRequest(queryURL+"Hans1/test2", "GET", nil)
- if st != "200 OK" || res != `
- Test2 file`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Try to delete a file
- st, _, res = sendTestRequest(queryURL+"Hans1/test2", "DELETE", nil)
- if st != "400 Bad Request" || res != "All applicable branches for the requested path were mounted as not writable" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/test2", "DELETE", []byte(`["1.txt", "2.txt"]`))
- if st != "400 Bad Request" || res != "All applicable branches for the requested path were mounted as not writable" {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Rename the file
- st, _, res = sendTestRequest(queryURL+"Hans1/tmp/newfile1", "PUT", []byte(`
- {
- "action" : "rename",
- "newname" : "newtest1"
- }`))
- if st != "200 OK" || res != "{}" {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Create a directory
- st, _, res = sendTestRequest(queryURL+"Hans1/tmp/upload/", "PUT", []byte(`
- {
- "action" : "mkdir"
- }`))
- if st != "200 OK" || res != "{}" {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Copy a file
- st, _, res = sendTestRequest(queryURL+"Hans1/tmp/newtest1", "PUT", []byte(`
- {
- "action" : "copy",
- "destination" : "/tmp/upload/"
- }`))
- if st != "200 OK" {
- t.Error("Unexpected response:", st, res)
- return
- }
- var resMap map[string]interface{}
- json.Unmarshal([]byte(res), &resMap)
- pid := fmt.Sprint(resMap["progress_id"])
- for {
- st, _, res = sendTestRequest(progressqueryURL+"Hans1/"+pid, "GET", nil)
- json.Unmarshal([]byte(res), &resMap)
- if st != "200 OK" || resMap["progress"] == resMap["total_progress"] {
- break
- }
- }
- if st != "200 OK" || res != `
- {
- "errors": [],
- "item": 1,
- "operation": "Copy",
- "progress": 7,
- "subject": "/newtest1",
- "total_items": 1,
- "total_progress": 7
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/tmp/upload/newtest1", "PUT", []byte(`
- {
- "action" : "rename",
- "newname" : "bla.txt"
- }`))
- if st != "200 OK" {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Check the directory
- st, _, res = sendTestRequest(dirqueryURL+"Hans1/tmp?recursive=1&checksums=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "/tmp": [
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "newtest1",
- "size": 7
- },
- {
- "checksum": "5b62da0f",
- "isdir": false,
- "name": "test1",
- "size": 10
- },
- {
- "checksum": "",
- "isdir": true,
- "name": "upload",
- "size": 4096
- }
- ],
- "/tmp/upload": [
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "bla.txt",
- "size": 7
- }
- ]
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Copy multiple files
- st, _, res = sendTestRequest(queryURL+"Hans1", "PUT", []byte(`
- {
- "action" : "copy",
- "files" : ["/tmp/newtest1", "/tmp/test1"],
- "destination" : "/tmp/upload"
- }`))
- if st != "200 OK" {
- t.Error("Unexpected response:", st, res)
- return
- }
- json.Unmarshal([]byte(res), &resMap)
- pid = fmt.Sprint(resMap["progress_id"])
- for {
- st, _, res = sendTestRequest(progressqueryURL+"Hans1/"+pid, "GET", nil)
- json.Unmarshal([]byte(res), &resMap)
- if st != "200 OK" || (resMap["progress"] == resMap["total_progress"] &&
- resMap["item"] == resMap["total_items"]) {
- break
- }
- }
- if st != "200 OK" || res != `
- {
- "errors": [],
- "item": 2,
- "operation": "Copy",
- "progress": 10,
- "subject": "/test1",
- "total_items": 2,
- "total_progress": 10
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Check the directory
- st, _, res = sendTestRequest(dirqueryURL+"Hans1/tmp?recursive=1&checksums=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "/tmp": [
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "newtest1",
- "size": 7
- },
- {
- "checksum": "5b62da0f",
- "isdir": false,
- "name": "test1",
- "size": 10
- },
- {
- "checksum": "",
- "isdir": true,
- "name": "upload",
- "size": 4096
- }
- ],
- "/tmp/upload": [
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "bla.txt",
- "size": 7
- },
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "newtest1",
- "size": 7
- },
- {
- "checksum": "5b62da0f",
- "isdir": false,
- "name": "test1",
- "size": 10
- }
- ]
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Rename multiple files
- st, _, res = sendTestRequest(queryURL+"Hans1", "PUT", []byte(`
- {
- "action" : "rename",
- "files" : ["/tmp/newtest1", "/tmp/upload", "/tmp/upload1/bla.txt"],
- "newnames" : ["/tmp/newtest2", "/tmp/upload1", "/tmp/upload1/bla1.txt"]
- }`))
- if st != "200 OK" {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Check the directory
- st, _, res = sendTestRequest(dirqueryURL+"Hans1/tmp?recursive=1&checksums=1", "GET", nil)
- if st != "200 OK" || res != `
- {
- "/tmp": [
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "newtest2",
- "size": 7
- },
- {
- "checksum": "5b62da0f",
- "isdir": false,
- "name": "test1",
- "size": 10
- },
- {
- "checksum": "",
- "isdir": true,
- "name": "upload1",
- "size": 4096
- }
- ],
- "/tmp/upload1": [
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "bla1.txt",
- "size": 7
- },
- {
- "checksum": "abcc6601",
- "isdir": false,
- "name": "newtest1",
- "size": 7
- },
- {
- "checksum": "5b62da0f",
- "isdir": false,
- "name": "test1",
- "size": 10
- }
- ]
- }`[1:] {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Test error conditions
- st, _, res = sendTestRequest(queryURL+"Hans1", "PUT", []byte(`
- {
- "action" : "rename",
- "files" : ["/tmp/newtest1", "/tmp/upload", "/tmp/upload1/bla.txt"],
- "newnames" : "/tmp/newtest2"
- }`))
- if st != "400 Bad Request" || res != "Parameter newnames must be a list of filenames" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1", "PUT", []byte(`
- {
- "action" : "rename",
- "newnames" : ["/tmp/newtest2"]
- }`))
- if st != "400 Bad Request" || res != "Parameter files is missing from request body" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1", "PUT", []byte(`
- {
- "action" : "rename",
- "files" : "/tmp/newtest1",
- "newnames" : ["/tmp/newtest2"]
- }`))
- if st != "400 Bad Request" || res != "Parameter files must be a list of files" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1", "PUT", []byte(`
- {
- "action" : "copy",
- "files" : "/tmp/newtest1",
- "destination" : "/tmp/upload"
- }`))
- if st != "400 Bad Request" || res != "Parameter files must be a list of files" {
- t.Error("Unexpected response:", st, res)
- return
- }
- ProgressMap = datautil.NewMapCache(100, 0)
- st, _, res = sendTestRequest(queryURL+"Hans1/tmp/newtest3", "PUT", []byte(`
- {
- "action" : "copy",
- "destination" : "/tmp/upload/bla.txt"
- }`))
- if st != "400 Bad Request" || res != "Cannot stat /tmp/newtest3: RufsError: Remote error (file does not exist)" {
- t.Error("Unexpected response:", st, res)
- return
- }
- // Check error in ProgressMap
- for k := range ProgressMap.GetAll() {
- k = strings.Split(k, "#")[1]
- _, _, res = sendTestRequest(progressqueryURL+"Hans1/"+k, "GET", nil)
- resMap = nil
- errorutil.AssertOk(json.Unmarshal([]byte(res), &resMap))
- if res := fmt.Sprint(resMap["errors"]); res != "[Cannot stat /tmp/newtest3: RufsError: Remote error (file does not exist)]" {
- t.Error("Unexpected result:", res)
- }
- break
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/", "GET", nil)
- if st != "400 Bad Request" || res != "Need a tree name and a file path" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans2/bla", "GET", nil)
- if st != "400 Bad Request" || res != "Unknown tree: Hans2" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/bla", "GET", nil)
- if st != "400 Bad Request" || res != "Could not read file bla: RufsError: Remote error (stat /bla: no such file or directory)" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(progressqueryURL+"Hans1/", "GET", nil)
- if st != "400 Bad Request" || res != "Need a tree name and a progress ID" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(progressqueryURL+"Hans2/bla", "GET", nil)
- if st != "400 Bad Request" || res != "Unknown tree: Hans2" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(progressqueryURL+"Hans1/bla", "GET", nil)
- if st != "400 Bad Request" || res != "Unknown progress ID: bla" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL, "PUT", []byte(""))
- if st != "400 Bad Request" || res != "Need a tree name and a file path" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans2/bla", "PUT", []byte(""))
- if st != "400 Bad Request" || res != "Unknown tree: Hans2" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/bla", "PUT", []byte(`
- aaa{
- "action" : "copy",
- "destination" : "/tmp/upload/bla.txt"
- }`))
- if st != "400 Bad Request" || res != "Could not decode request body: invalid character 'a' looking for beginning of value" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/bla", "PUT", []byte(`
- {
- "action2" : "copy",
- "destination" : "/tmp/upload/bla.txt"
- }`))
- if st != "400 Bad Request" || res != "Action command is missing from request body" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/bla", "PUT", []byte(`
- {
- "action" : "copy",
- "destination2" : "/tmp/upload/bla.txt"
- }`))
- if st != "400 Bad Request" || res != "Parameter destination is missing from request body" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/bla", "PUT", []byte(`
- {
- "action" : "copy2",
- "destination" : "/tmp/upload/bla.txt"
- }`))
- if st != "400 Bad Request" || res != "Unknown action: copy2" {
- t.Error("Unexpected response:", st, res)
- return
- }
- st, _, res = sendTestRequest(queryURL+"Hans1/bla", "PUT", []byte(`
- {
- "action" : "rename"
- }`))
- if st != "400 Bad Request" || res != "Parameter newname is missing from request body" {
- t.Error("Unexpected response:", st, res)
- return
- }
- }
|