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.

893 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. req.Header.Set("Origin", strings.TrimSuffix(Config.siteURL, "/"))
  216. mux.ServeHTTP(w, req)
  217. if w.Code != 200 {
  218. t.Log(w.Body.String())
  219. t.Fatalf("Status code is not 200, but %d", w.Code)
  220. }
  221. var myjson RespOkJSON
  222. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  227. if err != nil {
  228. t.Fatal(err)
  229. }
  230. curTime := time.Now().Unix()
  231. if myExp < curTime {
  232. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  233. }
  234. if myjson.Size != "12" {
  235. t.Fatalf("File size was not 12 but %s", myjson.Size)
  236. }
  237. }
  238. func TestPostUpload(t *testing.T) {
  239. mux := setup()
  240. w := httptest.NewRecorder()
  241. filename := generateBarename() + ".txt"
  242. var b bytes.Buffer
  243. mw := multipart.NewWriter(&b)
  244. fw, err := mw.CreateFormFile("file", filename)
  245. if err != nil {
  246. t.Fatal(err)
  247. }
  248. fw.Write([]byte("File content"))
  249. mw.Close()
  250. req, err := http.NewRequest("POST", "/upload/", &b)
  251. req.Header.Set("Content-Type", mw.FormDataContentType())
  252. req.Header.Set("Referer", Config.siteURL)
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. mux.ServeHTTP(w, req)
  257. if w.Code != 303 {
  258. t.Fatalf("Status code is not 303, but %d", w.Code)
  259. }
  260. if w.Header().Get("Location") != "/"+filename {
  261. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  262. }
  263. }
  264. func TestPostJSONUpload(t *testing.T) {
  265. mux := setup()
  266. w := httptest.NewRecorder()
  267. filename := generateBarename() + ".txt"
  268. var b bytes.Buffer
  269. mw := multipart.NewWriter(&b)
  270. fw, err := mw.CreateFormFile("file", filename)
  271. if err != nil {
  272. t.Fatal(err)
  273. }
  274. fw.Write([]byte("File content"))
  275. mw.Close()
  276. req, err := http.NewRequest("POST", "/upload/", &b)
  277. req.Header.Set("Content-Type", mw.FormDataContentType())
  278. req.Header.Set("Accept", "application/json")
  279. req.Header.Set("Referer", Config.siteURL)
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. mux.ServeHTTP(w, req)
  284. if w.Code != 200 {
  285. t.Log(w.Body.String())
  286. t.Fatalf("Status code is not 200, but %d", w.Code)
  287. }
  288. var myjson RespOkJSON
  289. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  290. if err != nil {
  291. t.Fatal(err)
  292. }
  293. if myjson.Filename != filename {
  294. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  295. }
  296. if myjson.Expiry != "0" {
  297. t.Fatalf("File expiry is not 0 but %s", myjson.Expiry)
  298. }
  299. if myjson.Size != "12" {
  300. t.Fatalf("File size was not 12 but %s", myjson.Size)
  301. }
  302. }
  303. func TestPostExpiresJSONUpload(t *testing.T) {
  304. mux := setup()
  305. w := httptest.NewRecorder()
  306. filename := generateBarename() + ".txt"
  307. var b bytes.Buffer
  308. mw := multipart.NewWriter(&b)
  309. fw, err := mw.CreateFormFile("file", filename)
  310. if err != nil {
  311. t.Fatal(err)
  312. }
  313. fw.Write([]byte("File content"))
  314. exp, err := mw.CreateFormField("expires")
  315. if err != nil {
  316. t.Fatal(err)
  317. }
  318. exp.Write([]byte("60"))
  319. mw.Close()
  320. req, err := http.NewRequest("POST", "/upload/", &b)
  321. req.Header.Set("Content-Type", mw.FormDataContentType())
  322. req.Header.Set("Accept", "application/json")
  323. req.Header.Set("Referer", Config.siteURL)
  324. if err != nil {
  325. t.Fatal(err)
  326. }
  327. mux.ServeHTTP(w, req)
  328. if w.Code != 200 {
  329. t.Log(w.Body.String())
  330. t.Fatalf("Status code is not 200, but %d", w.Code)
  331. }
  332. var myjson RespOkJSON
  333. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  334. if err != nil {
  335. t.Fatal(err)
  336. }
  337. if myjson.Filename != filename {
  338. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  339. }
  340. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  341. if err != nil {
  342. t.Fatal(err)
  343. }
  344. curTime := time.Now().Unix()
  345. if myExp < curTime {
  346. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  347. }
  348. if myjson.Size != "12" {
  349. t.Fatalf("File size was not 12 but %s", myjson.Size)
  350. }
  351. }
  352. func TestPostRandomizeJSONUpload(t *testing.T) {
  353. mux := setup()
  354. w := httptest.NewRecorder()
  355. filename := generateBarename() + ".txt"
  356. var b bytes.Buffer
  357. mw := multipart.NewWriter(&b)
  358. fw, err := mw.CreateFormFile("file", filename)
  359. if err != nil {
  360. t.Fatal(err)
  361. }
  362. fw.Write([]byte("File content"))
  363. rnd, err := mw.CreateFormField("randomize")
  364. if err != nil {
  365. t.Fatal(err)
  366. }
  367. rnd.Write([]byte("true"))
  368. mw.Close()
  369. req, err := http.NewRequest("POST", "/upload/", &b)
  370. req.Header.Set("Content-Type", mw.FormDataContentType())
  371. req.Header.Set("Accept", "application/json")
  372. req.Header.Set("Referer", Config.siteURL)
  373. if err != nil {
  374. t.Fatal(err)
  375. }
  376. mux.ServeHTTP(w, req)
  377. if w.Code != 200 {
  378. t.Log(w.Body.String())
  379. t.Fatalf("Status code is not 200, but %d", w.Code)
  380. }
  381. var myjson RespOkJSON
  382. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  383. if err != nil {
  384. t.Fatal(err)
  385. }
  386. if myjson.Filename == filename {
  387. t.Fatalf("Filename (%s) is not random (%s)", filename, myjson.Filename)
  388. }
  389. if myjson.Size != "12" {
  390. t.Fatalf("File size was not 12 but %s", myjson.Size)
  391. }
  392. }
  393. func TestPostEmptyUpload(t *testing.T) {
  394. mux := setup()
  395. w := httptest.NewRecorder()
  396. filename := generateBarename() + ".txt"
  397. var b bytes.Buffer
  398. mw := multipart.NewWriter(&b)
  399. fw, err := mw.CreateFormFile("file", filename)
  400. if err != nil {
  401. t.Fatal(err)
  402. }
  403. fw.Write([]byte(""))
  404. mw.Close()
  405. req, err := http.NewRequest("POST", "/upload/", &b)
  406. req.Header.Set("Content-Type", mw.FormDataContentType())
  407. req.Header.Set("Referer", Config.siteURL)
  408. if err != nil {
  409. t.Fatal(err)
  410. }
  411. mux.ServeHTTP(w, req)
  412. if w.Code != 500 {
  413. t.Log(w.Body.String())
  414. t.Fatalf("Status code is not 500, but %d", w.Code)
  415. }
  416. if !strings.Contains(w.Body.String(), "Empty file") {
  417. t.Fatal("Response did not contain 'Empty file'")
  418. }
  419. }
  420. func TestPostEmptyJSONUpload(t *testing.T) {
  421. mux := setup()
  422. w := httptest.NewRecorder()
  423. filename := generateBarename() + ".txt"
  424. var b bytes.Buffer
  425. mw := multipart.NewWriter(&b)
  426. fw, err := mw.CreateFormFile("file", filename)
  427. if err != nil {
  428. t.Fatal(err)
  429. }
  430. fw.Write([]byte(""))
  431. mw.Close()
  432. req, err := http.NewRequest("POST", "/upload/", &b)
  433. req.Header.Set("Content-Type", mw.FormDataContentType())
  434. req.Header.Set("Accept", "application/json")
  435. req.Header.Set("Referer", Config.siteURL)
  436. if err != nil {
  437. t.Fatal(err)
  438. }
  439. mux.ServeHTTP(w, req)
  440. if w.Code != 500 {
  441. t.Log(w.Body.String())
  442. t.Fatalf("Status code is not 500, but %d", w.Code)
  443. }
  444. var myjson RespErrJSON
  445. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  446. if err != nil {
  447. t.Fatal(err)
  448. }
  449. if myjson.Error != "Could not upload file: Empty file" {
  450. t.Fatal("Json 'error' was not 'Empty file' but " + myjson.Error)
  451. }
  452. }
  453. func TestPutUpload(t *testing.T) {
  454. mux := setup()
  455. w := httptest.NewRecorder()
  456. filename := generateBarename() + ".ext"
  457. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  458. if err != nil {
  459. t.Fatal(err)
  460. }
  461. mux.ServeHTTP(w, req)
  462. if w.Body.String() != Config.siteURL+filename {
  463. t.Fatal("Response was not expected URL")
  464. }
  465. }
  466. func TestPutRandomizedUpload(t *testing.T) {
  467. mux := setup()
  468. w := httptest.NewRecorder()
  469. filename := generateBarename() + ".ext"
  470. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  471. if err != nil {
  472. t.Fatal(err)
  473. }
  474. req.Header.Set("Linx-Randomize", "yes")
  475. mux.ServeHTTP(w, req)
  476. if w.Body.String() == Config.siteURL+filename {
  477. t.Fatal("Filename was not random")
  478. }
  479. }
  480. func TestPutNoExtensionUpload(t *testing.T) {
  481. mux := setup()
  482. w := httptest.NewRecorder()
  483. filename := generateBarename()
  484. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  485. if err != nil {
  486. t.Fatal(err)
  487. }
  488. req.Header.Set("Linx-Randomize", "yes")
  489. mux.ServeHTTP(w, req)
  490. if w.Body.String() == Config.siteURL+filename {
  491. t.Fatal("Filename was not random")
  492. }
  493. }
  494. func TestPutEmptyUpload(t *testing.T) {
  495. mux := setup()
  496. w := httptest.NewRecorder()
  497. filename := generateBarename() + ".ext"
  498. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  499. if err != nil {
  500. t.Fatal(err)
  501. }
  502. req.Header.Set("Linx-Randomize", "yes")
  503. mux.ServeHTTP(w, req)
  504. if !strings.Contains(w.Body.String(), "Empty file") {
  505. t.Fatal("Response doesn't contain'Empty file'")
  506. }
  507. }
  508. func TestPutJSONUpload(t *testing.T) {
  509. var myjson RespOkJSON
  510. mux := setup()
  511. w := httptest.NewRecorder()
  512. filename := generateBarename() + ".ext"
  513. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  514. if err != nil {
  515. t.Fatal(err)
  516. }
  517. req.Header.Set("Accept", "application/json")
  518. mux.ServeHTTP(w, req)
  519. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  520. if err != nil {
  521. t.Fatal(err)
  522. }
  523. if myjson.Filename != filename {
  524. t.Fatal("Filename was not provided one but " + myjson.Filename)
  525. }
  526. }
  527. func TestPutRandomizedJSONUpload(t *testing.T) {
  528. var myjson RespOkJSON
  529. mux := setup()
  530. w := httptest.NewRecorder()
  531. filename := generateBarename() + ".ext"
  532. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  533. if err != nil {
  534. t.Fatal(err)
  535. }
  536. req.Header.Set("Accept", "application/json")
  537. req.Header.Set("Linx-Randomize", "yes")
  538. mux.ServeHTTP(w, req)
  539. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  540. if err != nil {
  541. t.Fatal(err)
  542. }
  543. if myjson.Filename == filename {
  544. t.Fatal("Filename was not random ")
  545. }
  546. }
  547. func TestPutExpireJSONUpload(t *testing.T) {
  548. var myjson RespOkJSON
  549. mux := setup()
  550. w := httptest.NewRecorder()
  551. filename := generateBarename() + ".ext"
  552. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  553. if err != nil {
  554. t.Fatal(err)
  555. }
  556. req.Header.Set("Accept", "application/json")
  557. req.Header.Set("Linx-Expiry", "600")
  558. mux.ServeHTTP(w, req)
  559. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  560. if err != nil {
  561. t.Fatal(err)
  562. }
  563. expiry, err := strconv.Atoi(myjson.Expiry)
  564. if err != nil {
  565. t.Fatal("Expiry was not an integer")
  566. }
  567. if expiry < 1 {
  568. t.Fatal("Expiry was not set")
  569. }
  570. }
  571. func TestPutAndDelete(t *testing.T) {
  572. var myjson RespOkJSON
  573. mux := setup()
  574. w := httptest.NewRecorder()
  575. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  576. if err != nil {
  577. t.Fatal(err)
  578. }
  579. req.Header.Set("Accept", "application/json")
  580. mux.ServeHTTP(w, req)
  581. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  582. if err != nil {
  583. t.Fatal(err)
  584. }
  585. // Delete it
  586. w = httptest.NewRecorder()
  587. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  588. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  589. mux.ServeHTTP(w, req)
  590. if w.Code != 200 {
  591. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  592. }
  593. // Make sure it's actually gone
  594. w = httptest.NewRecorder()
  595. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  596. mux.ServeHTTP(w, req)
  597. if w.Code != 404 {
  598. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  599. }
  600. // Make sure torrent is also gone
  601. w = httptest.NewRecorder()
  602. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  603. mux.ServeHTTP(w, req)
  604. if w.Code != 404 {
  605. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  606. }
  607. }
  608. func TestPutAndOverwrite(t *testing.T) {
  609. var myjson RespOkJSON
  610. mux := setup()
  611. w := httptest.NewRecorder()
  612. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  613. if err != nil {
  614. t.Fatal(err)
  615. }
  616. req.Header.Set("Accept", "application/json")
  617. mux.ServeHTTP(w, req)
  618. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  619. if err != nil {
  620. t.Fatal(err)
  621. }
  622. // Overwrite it
  623. w = httptest.NewRecorder()
  624. req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
  625. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  626. mux.ServeHTTP(w, req)
  627. if w.Code != 200 {
  628. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  629. }
  630. // Make sure it's the new file
  631. w = httptest.NewRecorder()
  632. req, err = http.NewRequest("GET", "/selif/"+myjson.Filename, nil)
  633. mux.ServeHTTP(w, req)
  634. if w.Code == 404 {
  635. t.Fatal("Status code was 404")
  636. }
  637. if w.Body.String() != "New file content" {
  638. t.Fatal("File did not contain 'New file content")
  639. }
  640. }
  641. func TestPutAndSpecificDelete(t *testing.T) {
  642. var myjson RespOkJSON
  643. mux := setup()
  644. w := httptest.NewRecorder()
  645. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  646. if err != nil {
  647. t.Fatal(err)
  648. }
  649. req.Header.Set("Accept", "application/json")
  650. req.Header.Set("Linx-Delete-Key", "supersecret")
  651. mux.ServeHTTP(w, req)
  652. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  653. if err != nil {
  654. t.Fatal(err)
  655. }
  656. // Delete it
  657. w = httptest.NewRecorder()
  658. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  659. req.Header.Set("Linx-Delete-Key", "supersecret")
  660. mux.ServeHTTP(w, req)
  661. if w.Code != 200 {
  662. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  663. }
  664. // Make sure it's actually gone
  665. w = httptest.NewRecorder()
  666. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  667. mux.ServeHTTP(w, req)
  668. if w.Code != 404 {
  669. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  670. }
  671. // Make sure torrent is gone too
  672. w = httptest.NewRecorder()
  673. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  674. mux.ServeHTTP(w, req)
  675. if w.Code != 404 {
  676. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  677. }
  678. }
  679. func TestShutdown(t *testing.T) {
  680. os.RemoveAll(Config.filesDir)
  681. os.RemoveAll(Config.metaDir)
  682. }