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.

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