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.

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