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.

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