Contains the Concourse pipeline definition for building a line-server container
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

444 lines
9.5 KiB

9 years ago
9 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "os"
  7. "path"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. "github.com/zenazn/goji"
  12. )
  13. func TestSetup(t *testing.T) {
  14. Config.siteURL = "http://linx.example.org/"
  15. Config.filesDir = path.Join(os.TempDir(), randomString(10))
  16. Config.metaDir = Config.filesDir + "_meta"
  17. Config.noLogs = true
  18. Config.siteName = "linx"
  19. setup()
  20. }
  21. func TestIndex(t *testing.T) {
  22. w := httptest.NewRecorder()
  23. req, err := http.NewRequest("GET", "/", nil)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. goji.DefaultMux.ServeHTTP(w, req)
  28. if !strings.Contains(w.Body.String(), "file-uploader") {
  29. t.Fatal("String 'file-uploader' not found in index response")
  30. }
  31. }
  32. func TestNotFound(t *testing.T) {
  33. w := httptest.NewRecorder()
  34. req, err := http.NewRequest("GET", "/url/should/not/exist", nil)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. goji.DefaultMux.ServeHTTP(w, req)
  39. if w.Code != 404 {
  40. t.Fatalf("Expected 404, got %d", w.Code)
  41. }
  42. }
  43. func TestFileNotFound(t *testing.T) {
  44. w := httptest.NewRecorder()
  45. filename := randomString(10)
  46. req, err := http.NewRequest("GET", "/selif/"+filename, nil)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. goji.DefaultMux.ServeHTTP(w, req)
  51. if w.Code != 404 {
  52. t.Fatalf("Expected 404, got %d", w.Code)
  53. }
  54. }
  55. func TestDisplayNotFound(t *testing.T) {
  56. w := httptest.NewRecorder()
  57. filename := randomString(10)
  58. req, err := http.NewRequest("GET", "/"+filename, nil)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. goji.DefaultMux.ServeHTTP(w, req)
  63. if w.Code != 404 {
  64. t.Fatalf("Expected 404, got %d", w.Code)
  65. }
  66. }
  67. func TestPostBodyUpload(t *testing.T) {
  68. w := httptest.NewRecorder()
  69. filename := randomString(10) + ".ext"
  70. req, err := http.NewRequest("POST", "/upload", strings.NewReader("File content"))
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. req.Header.Set("Content-Type", "application/octet-stream")
  75. params := req.URL.Query()
  76. params.Add("qqfile", filename)
  77. req.URL.RawQuery = params.Encode()
  78. goji.DefaultMux.ServeHTTP(w, req)
  79. if w.Code != 301 {
  80. t.Fatalf("Status code is not 301, but %d", w.Code)
  81. }
  82. if w.Header().Get("Location") != "/"+filename {
  83. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  84. }
  85. }
  86. func TestPostEmptyBodyUpload(t *testing.T) {
  87. w := httptest.NewRecorder()
  88. filename := randomString(10) + ".ext"
  89. req, err := http.NewRequest("POST", "/upload", strings.NewReader(""))
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. req.Header.Set("Content-Type", "application/octet-stream")
  94. params := req.URL.Query()
  95. params.Add("qqfile", filename)
  96. req.URL.RawQuery = params.Encode()
  97. goji.DefaultMux.ServeHTTP(w, req)
  98. if w.Code == 301 {
  99. t.Fatal("Status code is 301")
  100. }
  101. if !strings.Contains(w.Body.String(), "Oops! Something went wrong.") {
  102. t.Fatal("Response doesn't contain'Oops! Something went wrong.'")
  103. }
  104. }
  105. func TestPostBodyRandomizeUpload(t *testing.T) {
  106. w := httptest.NewRecorder()
  107. filename := randomString(10) + ".ext"
  108. req, err := http.NewRequest("POST", "/upload", strings.NewReader("File content"))
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. req.Header.Set("Content-Type", "application/octet-stream")
  113. params := req.URL.Query()
  114. params.Add("qqfile", filename)
  115. params.Add("randomize", "true")
  116. req.URL.RawQuery = params.Encode()
  117. goji.DefaultMux.ServeHTTP(w, req)
  118. if w.Code != 301 {
  119. t.Fatalf("Status code is not 301, but %d", w.Code)
  120. }
  121. if w.Header().Get("Location") == "/"+filename {
  122. t.Fatalf("Was redirected to %s instead of something random", filename)
  123. }
  124. }
  125. func TestPostBodyExpireUpload(t *testing.T) {
  126. // Dependant on json info on display url to check expiry
  127. }
  128. func TestPutUpload(t *testing.T) {
  129. w := httptest.NewRecorder()
  130. filename := randomString(10) + ".ext"
  131. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. goji.DefaultMux.ServeHTTP(w, req)
  136. if w.Body.String() != Config.siteURL+filename {
  137. t.Fatal("Response was not expected URL")
  138. }
  139. }
  140. func TestPutRandomizedUpload(t *testing.T) {
  141. w := httptest.NewRecorder()
  142. filename := randomString(10) + ".ext"
  143. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. req.Header.Set("X-Randomized-Barename", "yes")
  148. goji.DefaultMux.ServeHTTP(w, req)
  149. if w.Body.String() == Config.siteURL+filename {
  150. t.Fatal("Filename was not random")
  151. }
  152. }
  153. func TestPutEmptyUpload(t *testing.T) {
  154. w := httptest.NewRecorder()
  155. filename := randomString(10) + ".ext"
  156. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. req.Header.Set("X-Randomized-Barename", "yes")
  161. goji.DefaultMux.ServeHTTP(w, req)
  162. if !strings.Contains(w.Body.String(), "Oops! Something went wrong.") {
  163. t.Fatal("Response doesn't contain'Oops! Something went wrong.'")
  164. }
  165. }
  166. func TestPutJSONUpload(t *testing.T) {
  167. type RespJSON struct {
  168. Filename string
  169. Url string
  170. Delete_Key string
  171. Expiry string
  172. Size string
  173. }
  174. var myjson RespJSON
  175. w := httptest.NewRecorder()
  176. filename := randomString(10) + ".ext"
  177. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. req.Header.Set("Accept", "application/json")
  182. goji.DefaultMux.ServeHTTP(w, req)
  183. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. if myjson.Filename != filename {
  188. t.Fatal("Filename was not provided one but " + myjson.Filename)
  189. }
  190. }
  191. func TestPutRandomizedJSONUpload(t *testing.T) {
  192. type RespJSON struct {
  193. Filename string
  194. Url string
  195. Delete_Key string
  196. Expiry string
  197. Size string
  198. }
  199. var myjson RespJSON
  200. w := httptest.NewRecorder()
  201. filename := randomString(10) + ".ext"
  202. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. req.Header.Set("Accept", "application/json")
  207. req.Header.Set("X-Randomized-Barename", "yes")
  208. goji.DefaultMux.ServeHTTP(w, req)
  209. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. if myjson.Filename == filename {
  214. t.Fatal("Filename was not random ")
  215. }
  216. }
  217. func TestPutExpireJSONUpload(t *testing.T) {
  218. type RespJSON struct {
  219. Filename string
  220. Url string
  221. Delete_Key string
  222. Expiry string
  223. Size string
  224. }
  225. var myjson RespJSON
  226. w := httptest.NewRecorder()
  227. filename := randomString(10) + ".ext"
  228. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. req.Header.Set("Accept", "application/json")
  233. req.Header.Set("X-File-Expiry", "600")
  234. goji.DefaultMux.ServeHTTP(w, req)
  235. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. expiry, err := strconv.Atoi(myjson.Expiry)
  240. if err != nil {
  241. t.Fatal("Expiry was not an integer")
  242. }
  243. if expiry < 1 {
  244. t.Fatal("Expiry was not set")
  245. }
  246. }
  247. func TestPutAndDelete(t *testing.T) {
  248. type RespJSON struct {
  249. Filename string
  250. Url string
  251. Delete_Key string
  252. Expiry string
  253. Size string
  254. }
  255. var myjson RespJSON
  256. w := httptest.NewRecorder()
  257. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. req.Header.Set("Accept", "application/json")
  262. goji.DefaultMux.ServeHTTP(w, req)
  263. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  264. if err != nil {
  265. t.Fatal(err)
  266. }
  267. // Delete it
  268. w = httptest.NewRecorder()
  269. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  270. req.Header.Set("X-Delete-Key", myjson.Delete_Key)
  271. goji.DefaultMux.ServeHTTP(w, req)
  272. if w.Code != 404 {
  273. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  274. }
  275. // Make sure it's actually gone
  276. w = httptest.NewRecorder()
  277. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  278. goji.DefaultMux.ServeHTTP(w, req)
  279. if w.Code != 404 {
  280. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  281. }
  282. // Make sure torrent is also gone
  283. w = httptest.NewRecorder()
  284. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  285. goji.DefaultMux.ServeHTTP(w, req)
  286. if w.Code != 404 {
  287. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  288. }
  289. }
  290. func TestPutAndSpecificDelete(t *testing.T) {
  291. type RespJSON struct {
  292. Filename string
  293. Url string
  294. Delete_Key string
  295. Expiry string
  296. Size string
  297. }
  298. var myjson RespJSON
  299. w := httptest.NewRecorder()
  300. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  301. if err != nil {
  302. t.Fatal(err)
  303. }
  304. req.Header.Set("Accept", "application/json")
  305. req.Header.Set("X-Delete-Key", "supersecret")
  306. goji.DefaultMux.ServeHTTP(w, req)
  307. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  308. if err != nil {
  309. t.Fatal(err)
  310. }
  311. // Delete it
  312. w = httptest.NewRecorder()
  313. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  314. req.Header.Set("X-Delete-Key", "supersecret")
  315. goji.DefaultMux.ServeHTTP(w, req)
  316. if w.Code != 404 {
  317. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  318. }
  319. // Make sure it's actually gone
  320. w = httptest.NewRecorder()
  321. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  322. goji.DefaultMux.ServeHTTP(w, req)
  323. if w.Code != 404 {
  324. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  325. }
  326. // Make sure torrent is gone too
  327. w = httptest.NewRecorder()
  328. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  329. goji.DefaultMux.ServeHTTP(w, req)
  330. if w.Code != 404 {
  331. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  332. }
  333. }
  334. func TestShutdown(t *testing.T) {
  335. os.RemoveAll(Config.filesDir)
  336. os.RemoveAll(Config.metaDir)
  337. }