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.

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