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.

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