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.

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