|
| 1 | +package routes |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +type GenericResponse struct { |
| 13 | + Data string `json:"data"` |
| 14 | + Success bool `json:"success"` |
| 15 | +} |
| 16 | + |
| 17 | +type DumpIndexersResponse struct { |
| 18 | + Data []string `json:"data"` |
| 19 | + Success bool `json:"success"` |
| 20 | +} |
| 21 | + |
| 22 | +type HashItem struct { |
| 23 | + FileName string |
| 24 | + Size int64 |
| 25 | + IsDirectory bool |
| 26 | + ModTime time.Time |
| 27 | + Hash string |
| 28 | + SubFiles map[string]HashItem |
| 29 | +} |
| 30 | + |
| 31 | +type IndexerServer struct { |
| 32 | + Online bool |
| 33 | + Hashes int |
| 34 | + Files HashItem |
| 35 | + LastSync time.Time |
| 36 | +} |
| 37 | + |
| 38 | +type GetAllFilesResponse struct { |
| 39 | + Data map[string]IndexerServer `json:"data"` |
| 40 | + Success bool `json:"success"` |
| 41 | +} |
| 42 | + |
| 43 | +type SearchFileResponse struct { |
| 44 | + Data []FileTarget `json:"data"` |
| 45 | + Success bool `json:"success"` |
| 46 | +} |
| 47 | + |
| 48 | +type GetFileResponse struct { |
| 49 | + Data struct { |
| 50 | + FileName string `json:"FileName"` |
| 51 | + Size int `json:"Size"` |
| 52 | + IsDirectory bool `json:"IsDirectory"` |
| 53 | + ModTime string `json:"ModTime"` |
| 54 | + Hash string `json:"Hash"` |
| 55 | + SubFiles struct { |
| 56 | + } `json:"SubFiles"` |
| 57 | + } `json:"data"` |
| 58 | + Success bool `json:"success"` |
| 59 | +} |
| 60 | + |
| 61 | +func SendGetRequest(t *testing.T, url string) []byte { |
| 62 | + res, err := http.Get(url) |
| 63 | + if err != nil { |
| 64 | + t.Errorf("Failed to get send request: %s", err) |
| 65 | + } |
| 66 | + |
| 67 | + defer res.Body.Close() |
| 68 | + body, err := io.ReadAll(res.Body) |
| 69 | + if err != nil { |
| 70 | + t.Errorf("Failed to read response: %s", err) |
| 71 | + } |
| 72 | + |
| 73 | + return body |
| 74 | +} |
| 75 | + |
| 76 | +func SendPostRequest(t *testing.T, url string, reqBody interface{}) []byte { |
| 77 | + json_data, err := json.Marshal(reqBody) |
| 78 | + if err != nil { |
| 79 | + t.Errorf("Failed to marshal POST request: %s", err) |
| 80 | + } |
| 81 | + |
| 82 | + res, err := http.Post(url, "application/json", bytes.NewBuffer(json_data)) |
| 83 | + if err != nil { |
| 84 | + t.Errorf("Failed to get send request: %s", err) |
| 85 | + } |
| 86 | + |
| 87 | + defer res.Body.Close() |
| 88 | + body, err := io.ReadAll(res.Body) |
| 89 | + if err != nil { |
| 90 | + t.Errorf("Failed to read response: %s", err) |
| 91 | + } |
| 92 | + |
| 93 | + return body |
| 94 | +} |
| 95 | + |
| 96 | +// this tests both files/read and files/download |
| 97 | +func TestDumpIndexers(t *testing.T) { |
| 98 | + // Run download tests |
| 99 | + response := SendGetRequest(t, "http://localhost:8081/indexer/getAll/indexers") |
| 100 | + var result DumpIndexersResponse |
| 101 | + if err := json.Unmarshal(response, &result); err != nil { |
| 102 | + t.Errorf("Failed to unmarshal JSON: %s", err) |
| 103 | + } |
| 104 | + |
| 105 | + if result.Success { |
| 106 | + if len(result.Data) == 1 && result.Data[0] == "http://localhost:8000" { |
| 107 | + t.Log("Dump test passed") |
| 108 | + } |
| 109 | + } else { |
| 110 | + t.Errorf("Dump test failed") |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestGetAllFiles(t *testing.T) { |
| 115 | + // Test if hello.txt exists (lightweight Exists route) |
| 116 | + response := SendGetRequest(t, "http://localhost:8081/indexer/getAll/files") |
| 117 | + var result GetAllFilesResponse |
| 118 | + if err := json.Unmarshal(response, &result); err != nil { |
| 119 | + t.Errorf("Failed to unmarshal JSON: %s", err) |
| 120 | + } |
| 121 | + |
| 122 | + if result.Success { |
| 123 | + if result.Data["http://localhost:8000"].Online == true && result.Data["http://localhost:8000"].Hashes == 3 { |
| 124 | + t.Log("GetAllFiles test passed") |
| 125 | + } else { |
| 126 | + t.Errorf("GetAllFiles test failed (2)") |
| 127 | + } |
| 128 | + } else { |
| 129 | + t.Errorf("GetAllFiles test failed") |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestGetFile(t *testing.T) { |
| 134 | + // Test if ReadDir in main dir is functioning |
| 135 | + response := SendGetRequest(t, "http://localhost:8081/indexer/get?server=http://localhost:8000&hash=1a9ef7e5588a84b048a7f60468c270888dada51adfd1f9276662d4a275e73198") |
| 136 | + var result GetFileResponse |
| 137 | + if err := json.Unmarshal(response, &result); err != nil { |
| 138 | + t.Errorf("Failed to unmarshal JSON: %s", err) |
| 139 | + } |
| 140 | + |
| 141 | + if result.Success { |
| 142 | + if result.Data.FileName == "hello.txt" && result.Data.Size == 15 { |
| 143 | + t.Log("GetFile test passed") |
| 144 | + } else { |
| 145 | + t.Errorf("GetFile test failed (wrong size of element, is indexer running default config?)") |
| 146 | + } |
| 147 | + } else { |
| 148 | + t.Errorf("GetFile test failed") |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestSearchFile(t *testing.T) { |
| 153 | + // Test if StatFile in main dir is functioning |
| 154 | + response := SendGetRequest(t, "http://localhost:8081/indexer/search?server=http://localhost:8000&query=hello.txt") |
| 155 | + var result SearchFileResponse |
| 156 | + if err := json.Unmarshal(response, &result); err != nil { |
| 157 | + t.Errorf("Failed to unmarshal JSON: %s", err) |
| 158 | + } |
| 159 | + |
| 160 | + if result.Success { |
| 161 | + if result.Data[0].PercentMatched == 1 && result.Data[0].Hash == "1a9ef7e5588a84b048a7f60468c270888dada51adfd1f9276662d4a275e73198" { // hash of Hello DistriFS! |
| 162 | + t.Log("SearchFile test passed") |
| 163 | + } else { |
| 164 | + t.Error("SearchFile test failed (wrong hash or % matched, is server running default config?)") |
| 165 | + } |
| 166 | + } else { |
| 167 | + t.Errorf("SearchFile test failed") |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestPutFile(t *testing.T) { |
| 172 | + response := SendGetRequest(t, "http://localhost:8081/indexer/suggest?server=http://localhost:8000") |
| 173 | + var result GenericResponse |
| 174 | + if err := json.Unmarshal(response, &result); err != nil { |
| 175 | + t.Errorf("Failed to unmarshal JSON: %s", err) |
| 176 | + } |
| 177 | + |
| 178 | + if result.Success || result.Data == "server_already_exists" { |
| 179 | + t.Log("PutFile test passed") |
| 180 | + } else { |
| 181 | + t.Errorf("PutFile test failed") |
| 182 | + } |
| 183 | +} |
0 commit comments