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.

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