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.

829 lines
18 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "mime/multipart"
  6. "net/http"
  7. "net/http/httptest"
  8. "net/url"
  9. "os"
  10. "path"
  11. "strconv"
  12. "strings"
  13. "testing"
  14. "time"
  15. "github.com/zenazn/goji"
  16. )
  17. type RespOkJSON struct {
  18. Filename string
  19. Url string
  20. Delete_Key string
  21. Expiry string
  22. Size string
  23. }
  24. type RespErrJSON struct {
  25. Error string
  26. }
  27. func TestSetup(t *testing.T) {
  28. Config.siteURL = "http://linx.example.org/"
  29. Config.filesDir = path.Join(os.TempDir(), generateBarename())
  30. Config.metaDir = Config.filesDir + "_meta"
  31. Config.maxSize = 1024 * 1024 * 1024
  32. Config.noLogs = true
  33. Config.siteName = "linx"
  34. setup()
  35. }
  36. func TestIndex(t *testing.T) {
  37. w := httptest.NewRecorder()
  38. req, err := http.NewRequest("GET", "/", nil)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. goji.DefaultMux.ServeHTTP(w, req)
  43. if !strings.Contains(w.Body.String(), "Click or Drop file") {
  44. t.Fatal("String 'Click or Drop file' not found in index response")
  45. }
  46. }
  47. func TestNotFound(t *testing.T) {
  48. w := httptest.NewRecorder()
  49. req, err := http.NewRequest("GET", "/url/should/not/exist", nil)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. goji.DefaultMux.ServeHTTP(w, req)
  54. if w.Code != 404 {
  55. t.Fatalf("Expected 404, got %d", w.Code)
  56. }
  57. }
  58. func TestFileNotFound(t *testing.T) {
  59. w := httptest.NewRecorder()
  60. filename := generateBarename()
  61. req, err := http.NewRequest("GET", "/selif/"+filename, nil)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. goji.DefaultMux.ServeHTTP(w, req)
  66. if w.Code != 404 {
  67. t.Fatalf("Expected 404, got %d", w.Code)
  68. }
  69. }
  70. func TestDisplayNotFound(t *testing.T) {
  71. w := httptest.NewRecorder()
  72. filename := generateBarename()
  73. req, err := http.NewRequest("GET", "/"+filename, nil)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. goji.DefaultMux.ServeHTTP(w, req)
  78. if w.Code != 404 {
  79. t.Fatalf("Expected 404, got %d", w.Code)
  80. }
  81. }
  82. func TestPostCodeUpload(t *testing.T) {
  83. w := httptest.NewRecorder()
  84. filename := generateBarename()
  85. extension := "txt"
  86. form := url.Values{}
  87. form.Add("content", "File content")
  88. form.Add("filename", filename)
  89. form.Add("extension", extension)
  90. req, err := http.NewRequest("POST", "/upload/", nil)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. req.PostForm = form
  95. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  96. req.Header.Set("Referer", Config.siteURL)
  97. goji.DefaultMux.ServeHTTP(w, req)
  98. if w.Code != 301 {
  99. t.Fatalf("Status code is not 301, but %d", w.Code)
  100. }
  101. if w.Header().Get("Location") != "/"+filename+"."+extension {
  102. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  103. }
  104. }
  105. func TestPostCodeUploadWhitelistedHeader(t *testing.T) {
  106. w := httptest.NewRecorder()
  107. filename := generateBarename()
  108. extension := "txt"
  109. form := url.Values{}
  110. form.Add("content", "File content")
  111. form.Add("filename", filename)
  112. form.Add("extension", extension)
  113. req, err := http.NewRequest("POST", "/upload/", nil)
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. req.PostForm = form
  118. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  119. req.Header.Set("Linx-Expiry", "0")
  120. goji.DefaultMux.ServeHTTP(w, req)
  121. if w.Code != 301 {
  122. t.Fatalf("Status code is not 301, but %d", w.Code)
  123. }
  124. }
  125. func TestPostCodeUploadNoReferrer(t *testing.T) {
  126. w := httptest.NewRecorder()
  127. filename := generateBarename()
  128. extension := "txt"
  129. form := url.Values{}
  130. form.Add("content", "File content")
  131. form.Add("filename", filename)
  132. form.Add("extension", extension)
  133. req, err := http.NewRequest("POST", "/upload/", nil)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. req.PostForm = form
  138. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  139. goji.DefaultMux.ServeHTTP(w, req)
  140. if w.Code != 400 {
  141. t.Fatalf("Status code is not 400, but %d", w.Code)
  142. }
  143. }
  144. func TestPostCodeUploadBadOrigin(t *testing.T) {
  145. w := httptest.NewRecorder()
  146. filename := generateBarename()
  147. extension := "txt"
  148. form := url.Values{}
  149. form.Add("content", "File content")
  150. form.Add("filename", filename)
  151. form.Add("extension", extension)
  152. req, err := http.NewRequest("POST", "/upload/", nil)
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. req.PostForm = form
  157. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  158. req.Header.Set("Referer", Config.siteURL)
  159. req.Header.Set("Origin", "http://example.com/")
  160. goji.DefaultMux.ServeHTTP(w, req)
  161. if w.Code != 400 {
  162. t.Fatalf("Status code is not 400, but %d", w.Code)
  163. }
  164. }
  165. func TestPostCodeExpiryJSONUpload(t *testing.T) {
  166. w := httptest.NewRecorder()
  167. form := url.Values{}
  168. form.Add("content", "File content")
  169. form.Add("filename", "")
  170. form.Add("expires", "60")
  171. req, err := http.NewRequest("POST", "/upload/", nil)
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. req.PostForm = form
  176. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  177. req.Header.Set("Accept", "application/json")
  178. req.Header.Set("Referer", Config.siteURL)
  179. goji.DefaultMux.ServeHTTP(w, req)
  180. if w.Code != 200 {
  181. t.Log(w.Body.String())
  182. t.Fatalf("Status code is not 200, but %d", w.Code)
  183. }
  184. var myjson RespOkJSON
  185. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. curTime := time.Now().Unix()
  194. if myExp < curTime {
  195. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  196. }
  197. if myjson.Size != "12" {
  198. t.Fatalf("File size was not 12 but %s", myjson.Size)
  199. }
  200. }
  201. func TestPostUpload(t *testing.T) {
  202. w := httptest.NewRecorder()
  203. filename := generateBarename() + ".txt"
  204. var b bytes.Buffer
  205. mw := multipart.NewWriter(&b)
  206. fw, err := mw.CreateFormFile("file", filename)
  207. if err != nil {
  208. t.Fatal(err)
  209. }
  210. fw.Write([]byte("File content"))
  211. mw.Close()
  212. req, err := http.NewRequest("POST", "/upload/", &b)
  213. req.Header.Set("Content-Type", mw.FormDataContentType())
  214. req.Header.Set("Referer", Config.siteURL)
  215. if err != nil {
  216. t.Fatal(err)
  217. }
  218. goji.DefaultMux.ServeHTTP(w, req)
  219. if w.Code != 301 {
  220. t.Fatalf("Status code is not 301, but %d", w.Code)
  221. }
  222. if w.Header().Get("Location") != "/"+filename {
  223. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  224. }
  225. }
  226. func TestPostJSONUpload(t *testing.T) {
  227. w := httptest.NewRecorder()
  228. filename := generateBarename() + ".txt"
  229. var b bytes.Buffer
  230. mw := multipart.NewWriter(&b)
  231. fw, err := mw.CreateFormFile("file", filename)
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. fw.Write([]byte("File content"))
  236. mw.Close()
  237. req, err := http.NewRequest("POST", "/upload/", &b)
  238. req.Header.Set("Content-Type", mw.FormDataContentType())
  239. req.Header.Set("Accept", "application/json")
  240. req.Header.Set("Referer", Config.siteURL)
  241. if err != nil {
  242. t.Fatal(err)
  243. }
  244. goji.DefaultMux.ServeHTTP(w, req)
  245. if w.Code != 200 {
  246. t.Log(w.Body.String())
  247. t.Fatalf("Status code is not 200, but %d", w.Code)
  248. }
  249. var myjson RespOkJSON
  250. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  251. if err != nil {
  252. t.Fatal(err)
  253. }
  254. if myjson.Filename != filename {
  255. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  256. }
  257. if myjson.Expiry != "0" {
  258. t.Fatalf("File expiry is not 0 but %s", myjson.Expiry)
  259. }
  260. if myjson.Size != "12" {
  261. t.Fatalf("File size was not 12 but %s", myjson.Size)
  262. }
  263. }
  264. func TestPostExpiresJSONUpload(t *testing.T) {
  265. w := httptest.NewRecorder()
  266. filename := generateBarename() + ".txt"
  267. var b bytes.Buffer
  268. mw := multipart.NewWriter(&b)
  269. fw, err := mw.CreateFormFile("file", filename)
  270. if err != nil {
  271. t.Fatal(err)
  272. }
  273. fw.Write([]byte("File content"))
  274. exp, err := mw.CreateFormField("expires")
  275. if err != nil {
  276. t.Fatal(err)
  277. }
  278. exp.Write([]byte("60"))
  279. mw.Close()
  280. req, err := http.NewRequest("POST", "/upload/", &b)
  281. req.Header.Set("Content-Type", mw.FormDataContentType())
  282. req.Header.Set("Accept", "application/json")
  283. req.Header.Set("Referer", Config.siteURL)
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. goji.DefaultMux.ServeHTTP(w, req)
  288. if w.Code != 200 {
  289. t.Log(w.Body.String())
  290. t.Fatalf("Status code is not 200, but %d", w.Code)
  291. }
  292. var myjson RespOkJSON
  293. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. if myjson.Filename != filename {
  298. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  299. }
  300. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  301. if err != nil {
  302. t.Fatal(err)
  303. }
  304. curTime := time.Now().Unix()
  305. if myExp < curTime {
  306. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  307. }
  308. if myjson.Size != "12" {
  309. t.Fatalf("File size was not 12 but %s", myjson.Size)
  310. }
  311. }
  312. func TestPostRandomizeJSONUpload(t *testing.T) {
  313. w := httptest.NewRecorder()
  314. filename := generateBarename() + ".txt"
  315. var b bytes.Buffer
  316. mw := multipart.NewWriter(&b)
  317. fw, err := mw.CreateFormFile("file", filename)
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. fw.Write([]byte("File content"))
  322. rnd, err := mw.CreateFormField("randomize")
  323. if err != nil {
  324. t.Fatal(err)
  325. }
  326. rnd.Write([]byte("true"))
  327. mw.Close()
  328. req, err := http.NewRequest("POST", "/upload/", &b)
  329. req.Header.Set("Content-Type", mw.FormDataContentType())
  330. req.Header.Set("Accept", "application/json")
  331. req.Header.Set("Referer", Config.siteURL)
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. goji.DefaultMux.ServeHTTP(w, req)
  336. if w.Code != 200 {
  337. t.Log(w.Body.String())
  338. t.Fatalf("Status code is not 200, but %d", w.Code)
  339. }
  340. var myjson RespOkJSON
  341. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  342. if err != nil {
  343. t.Fatal(err)
  344. }
  345. if myjson.Filename == filename {
  346. t.Fatalf("Filename (%s) is not random (%s)", filename, myjson.Filename)
  347. }
  348. if myjson.Size != "12" {
  349. t.Fatalf("File size was not 12 but %s", myjson.Size)
  350. }
  351. }
  352. func TestPostEmptyUpload(t *testing.T) {
  353. w := httptest.NewRecorder()
  354. filename := generateBarename() + ".txt"
  355. var b bytes.Buffer
  356. mw := multipart.NewWriter(&b)
  357. fw, err := mw.CreateFormFile("file", filename)
  358. if err != nil {
  359. t.Fatal(err)
  360. }
  361. fw.Write([]byte(""))
  362. mw.Close()
  363. req, err := http.NewRequest("POST", "/upload/", &b)
  364. req.Header.Set("Content-Type", mw.FormDataContentType())
  365. req.Header.Set("Referer", Config.siteURL)
  366. if err != nil {
  367. t.Fatal(err)
  368. }
  369. goji.DefaultMux.ServeHTTP(w, req)
  370. if w.Code != 500 {
  371. t.Log(w.Body.String())
  372. t.Fatalf("Status code is not 500, but %d", w.Code)
  373. }
  374. if !strings.Contains(w.Body.String(), "Empty file") {
  375. t.Fatal("Response did not contain 'Empty file'")
  376. }
  377. }
  378. func TestPostEmptyJSONUpload(t *testing.T) {
  379. w := httptest.NewRecorder()
  380. filename := generateBarename() + ".txt"
  381. var b bytes.Buffer
  382. mw := multipart.NewWriter(&b)
  383. fw, err := mw.CreateFormFile("file", filename)
  384. if err != nil {
  385. t.Fatal(err)
  386. }
  387. fw.Write([]byte(""))
  388. mw.Close()
  389. req, err := http.NewRequest("POST", "/upload/", &b)
  390. req.Header.Set("Content-Type", mw.FormDataContentType())
  391. req.Header.Set("Accept", "application/json")
  392. req.Header.Set("Referer", Config.siteURL)
  393. if err != nil {
  394. t.Fatal(err)
  395. }
  396. goji.DefaultMux.ServeHTTP(w, req)
  397. if w.Code != 500 {
  398. t.Log(w.Body.String())
  399. t.Fatalf("Status code is not 500, but %d", w.Code)
  400. }
  401. var myjson RespErrJSON
  402. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  403. if err != nil {
  404. t.Fatal(err)
  405. }
  406. if myjson.Error != "Could not upload file: Empty file" {
  407. t.Fatal("Json 'error' was not 'Empty file' but " + myjson.Error)
  408. }
  409. }
  410. func TestPutUpload(t *testing.T) {
  411. w := httptest.NewRecorder()
  412. filename := generateBarename() + ".ext"
  413. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  414. if err != nil {
  415. t.Fatal(err)
  416. }
  417. goji.DefaultMux.ServeHTTP(w, req)
  418. if w.Body.String() != Config.siteURL+filename {
  419. t.Fatal("Response was not expected URL")
  420. }
  421. }
  422. func TestPutRandomizedUpload(t *testing.T) {
  423. w := httptest.NewRecorder()
  424. filename := generateBarename() + ".ext"
  425. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  426. if err != nil {
  427. t.Fatal(err)
  428. }
  429. req.Header.Set("Linx-Randomize", "yes")
  430. goji.DefaultMux.ServeHTTP(w, req)
  431. if w.Body.String() == Config.siteURL+filename {
  432. t.Fatal("Filename was not random")
  433. }
  434. }
  435. func TestPutNoExtensionUpload(t *testing.T) {
  436. w := httptest.NewRecorder()
  437. filename := generateBarename()
  438. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  439. if err != nil {
  440. t.Fatal(err)
  441. }
  442. req.Header.Set("Linx-Randomize", "yes")
  443. goji.DefaultMux.ServeHTTP(w, req)
  444. if w.Body.String() == Config.siteURL+filename {
  445. t.Fatal("Filename was not random")
  446. }
  447. }
  448. func TestPutEmptyUpload(t *testing.T) {
  449. w := httptest.NewRecorder()
  450. filename := generateBarename() + ".ext"
  451. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  452. if err != nil {
  453. t.Fatal(err)
  454. }
  455. req.Header.Set("Linx-Randomize", "yes")
  456. goji.DefaultMux.ServeHTTP(w, req)
  457. if !strings.Contains(w.Body.String(), "Empty file") {
  458. t.Fatal("Response doesn't contain'Empty file'")
  459. }
  460. }
  461. func TestPutJSONUpload(t *testing.T) {
  462. var myjson RespOkJSON
  463. w := httptest.NewRecorder()
  464. filename := generateBarename() + ".ext"
  465. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  466. if err != nil {
  467. t.Fatal(err)
  468. }
  469. req.Header.Set("Accept", "application/json")
  470. goji.DefaultMux.ServeHTTP(w, req)
  471. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  472. if err != nil {
  473. t.Fatal(err)
  474. }
  475. if myjson.Filename != filename {
  476. t.Fatal("Filename was not provided one but " + myjson.Filename)
  477. }
  478. }
  479. func TestPutRandomizedJSONUpload(t *testing.T) {
  480. var myjson RespOkJSON
  481. w := httptest.NewRecorder()
  482. filename := generateBarename() + ".ext"
  483. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  484. if err != nil {
  485. t.Fatal(err)
  486. }
  487. req.Header.Set("Accept", "application/json")
  488. req.Header.Set("Linx-Randomize", "yes")
  489. goji.DefaultMux.ServeHTTP(w, req)
  490. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  491. if err != nil {
  492. t.Fatal(err)
  493. }
  494. if myjson.Filename == filename {
  495. t.Fatal("Filename was not random ")
  496. }
  497. }
  498. func TestPutExpireJSONUpload(t *testing.T) {
  499. var myjson RespOkJSON
  500. w := httptest.NewRecorder()
  501. filename := generateBarename() + ".ext"
  502. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  503. if err != nil {
  504. t.Fatal(err)
  505. }
  506. req.Header.Set("Accept", "application/json")
  507. req.Header.Set("Linx-Expiry", "600")
  508. goji.DefaultMux.ServeHTTP(w, req)
  509. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  510. if err != nil {
  511. t.Fatal(err)
  512. }
  513. expiry, err := strconv.Atoi(myjson.Expiry)
  514. if err != nil {
  515. t.Fatal("Expiry was not an integer")
  516. }
  517. if expiry < 1 {
  518. t.Fatal("Expiry was not set")
  519. }
  520. }
  521. func TestPutAndDelete(t *testing.T) {
  522. var myjson RespOkJSON
  523. w := httptest.NewRecorder()
  524. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  525. if err != nil {
  526. t.Fatal(err)
  527. }
  528. req.Header.Set("Accept", "application/json")
  529. goji.DefaultMux.ServeHTTP(w, req)
  530. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  531. if err != nil {
  532. t.Fatal(err)
  533. }
  534. // Delete it
  535. w = httptest.NewRecorder()
  536. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  537. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  538. goji.DefaultMux.ServeHTTP(w, req)
  539. if w.Code != 200 {
  540. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  541. }
  542. // Make sure it's actually gone
  543. w = httptest.NewRecorder()
  544. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  545. goji.DefaultMux.ServeHTTP(w, req)
  546. if w.Code != 404 {
  547. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  548. }
  549. // Make sure torrent is also gone
  550. w = httptest.NewRecorder()
  551. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  552. goji.DefaultMux.ServeHTTP(w, req)
  553. if w.Code != 404 {
  554. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  555. }
  556. }
  557. func TestPutAndOverwrite(t *testing.T) {
  558. var myjson RespOkJSON
  559. w := httptest.NewRecorder()
  560. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  561. if err != nil {
  562. t.Fatal(err)
  563. }
  564. req.Header.Set("Accept", "application/json")
  565. goji.DefaultMux.ServeHTTP(w, req)
  566. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  567. if err != nil {
  568. t.Fatal(err)
  569. }
  570. // Overwrite it
  571. w = httptest.NewRecorder()
  572. req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
  573. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  574. goji.DefaultMux.ServeHTTP(w, req)
  575. if w.Code != 200 {
  576. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  577. }
  578. // Make sure it's the new file
  579. w = httptest.NewRecorder()
  580. req, err = http.NewRequest("GET", "/selif/"+myjson.Filename, nil)
  581. goji.DefaultMux.ServeHTTP(w, req)
  582. if w.Code == 404 {
  583. t.Fatal("Status code was 404")
  584. }
  585. if w.Body.String() != "New file content" {
  586. t.Fatal("File did not contain 'New file content")
  587. }
  588. }
  589. func TestPutAndSpecificDelete(t *testing.T) {
  590. var myjson RespOkJSON
  591. w := httptest.NewRecorder()
  592. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  593. if err != nil {
  594. t.Fatal(err)
  595. }
  596. req.Header.Set("Accept", "application/json")
  597. req.Header.Set("Linx-Delete-Key", "supersecret")
  598. goji.DefaultMux.ServeHTTP(w, req)
  599. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  600. if err != nil {
  601. t.Fatal(err)
  602. }
  603. // Delete it
  604. w = httptest.NewRecorder()
  605. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  606. req.Header.Set("Linx-Delete-Key", "supersecret")
  607. goji.DefaultMux.ServeHTTP(w, req)
  608. if w.Code != 200 {
  609. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  610. }
  611. // Make sure it's actually gone
  612. w = httptest.NewRecorder()
  613. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  614. goji.DefaultMux.ServeHTTP(w, req)
  615. if w.Code != 404 {
  616. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  617. }
  618. // Make sure torrent is gone too
  619. w = httptest.NewRecorder()
  620. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  621. goji.DefaultMux.ServeHTTP(w, req)
  622. if w.Code != 404 {
  623. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  624. }
  625. }
  626. func TestShutdown(t *testing.T) {
  627. os.RemoveAll(Config.filesDir)
  628. os.RemoveAll(Config.metaDir)
  629. }