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.

1121 lines
23 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
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. testExpiries := []string{"86400", "-150"}
  350. for _, expiry := range testExpiries {
  351. w := httptest.NewRecorder()
  352. filename := generateBarename() + ".txt"
  353. var b bytes.Buffer
  354. mw := multipart.NewWriter(&b)
  355. fw, err := mw.CreateFormFile("file", filename)
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. fw.Write([]byte("File content"))
  360. mw.Close()
  361. req, err := http.NewRequest("POST", "/upload/", &b)
  362. req.Header.Set("Content-Type", mw.FormDataContentType())
  363. req.Header.Set("Accept", "application/json")
  364. req.Header.Set("Linx-Expiry", expiry)
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. mux.ServeHTTP(w, req)
  369. if w.Code != 200 {
  370. t.Log(w.Body.String())
  371. t.Fatalf("Status code is not 200, but %d", w.Code)
  372. }
  373. var myjson RespOkJSON
  374. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  375. if err != nil {
  376. fmt.Println(w.Body.String())
  377. t.Fatal(err)
  378. }
  379. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  380. if err != nil {
  381. t.Fatal(err)
  382. }
  383. expected := time.Now().Add(time.Duration(Config.maxExpiry) * time.Second).Unix()
  384. if myExp != expected {
  385. t.Fatalf("File expiry is not %d but %s", expected, myjson.Expiry)
  386. }
  387. }
  388. Config.maxExpiry = 0
  389. }
  390. func TestPostExpiresJSONUpload(t *testing.T) {
  391. mux := setup()
  392. w := httptest.NewRecorder()
  393. filename := generateBarename() + ".txt"
  394. var b bytes.Buffer
  395. mw := multipart.NewWriter(&b)
  396. fw, err := mw.CreateFormFile("file", filename)
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. fw.Write([]byte("File content"))
  401. exp, err := mw.CreateFormField("expires")
  402. if err != nil {
  403. t.Fatal(err)
  404. }
  405. exp.Write([]byte("60"))
  406. mw.Close()
  407. req, err := http.NewRequest("POST", "/upload/", &b)
  408. req.Header.Set("Content-Type", mw.FormDataContentType())
  409. req.Header.Set("Accept", "application/json")
  410. req.Header.Set("Referer", Config.siteURL)
  411. if err != nil {
  412. t.Fatal(err)
  413. }
  414. mux.ServeHTTP(w, req)
  415. if w.Code != 200 {
  416. t.Log(w.Body.String())
  417. t.Fatalf("Status code is not 200, but %d", w.Code)
  418. }
  419. var myjson RespOkJSON
  420. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  421. if err != nil {
  422. t.Fatal(err)
  423. }
  424. if myjson.Filename != filename {
  425. t.Fatalf("Filename is not '%s' but '%s' ", filename, myjson.Filename)
  426. }
  427. myExp, err := strconv.ParseInt(myjson.Expiry, 10, 64)
  428. if err != nil {
  429. t.Fatal(err)
  430. }
  431. curTime := time.Now().Unix()
  432. if myExp < curTime {
  433. t.Fatalf("File expiry (%d) is smaller than current time (%d)", myExp, curTime)
  434. }
  435. if myjson.Size != "12" {
  436. t.Fatalf("File size was not 12 but %s", myjson.Size)
  437. }
  438. }
  439. func TestPostRandomizeJSONUpload(t *testing.T) {
  440. mux := setup()
  441. w := httptest.NewRecorder()
  442. filename := generateBarename() + ".txt"
  443. var b bytes.Buffer
  444. mw := multipart.NewWriter(&b)
  445. fw, err := mw.CreateFormFile("file", filename)
  446. if err != nil {
  447. t.Fatal(err)
  448. }
  449. fw.Write([]byte("File content"))
  450. rnd, err := mw.CreateFormField("randomize")
  451. if err != nil {
  452. t.Fatal(err)
  453. }
  454. rnd.Write([]byte("true"))
  455. mw.Close()
  456. req, err := http.NewRequest("POST", "/upload/", &b)
  457. req.Header.Set("Content-Type", mw.FormDataContentType())
  458. req.Header.Set("Accept", "application/json")
  459. req.Header.Set("Referer", Config.siteURL)
  460. if err != nil {
  461. t.Fatal(err)
  462. }
  463. mux.ServeHTTP(w, req)
  464. if w.Code != 200 {
  465. t.Log(w.Body.String())
  466. t.Fatalf("Status code is not 200, but %d", w.Code)
  467. }
  468. var myjson RespOkJSON
  469. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  470. if err != nil {
  471. t.Fatal(err)
  472. }
  473. if myjson.Filename == filename {
  474. t.Fatalf("Filename (%s) is not random (%s)", filename, myjson.Filename)
  475. }
  476. if myjson.Size != "12" {
  477. t.Fatalf("File size was not 12 but %s", myjson.Size)
  478. }
  479. }
  480. func TestPostEmptyUpload(t *testing.T) {
  481. mux := setup()
  482. w := httptest.NewRecorder()
  483. filename := generateBarename() + ".txt"
  484. var b bytes.Buffer
  485. mw := multipart.NewWriter(&b)
  486. fw, err := mw.CreateFormFile("file", filename)
  487. if err != nil {
  488. t.Fatal(err)
  489. }
  490. fw.Write([]byte(""))
  491. mw.Close()
  492. req, err := http.NewRequest("POST", "/upload/", &b)
  493. req.Header.Set("Content-Type", mw.FormDataContentType())
  494. req.Header.Set("Referer", Config.siteURL)
  495. if err != nil {
  496. t.Fatal(err)
  497. }
  498. mux.ServeHTTP(w, req)
  499. if w.Code != 500 {
  500. t.Log(w.Body.String())
  501. t.Fatalf("Status code is not 500, but %d", w.Code)
  502. }
  503. if !strings.Contains(w.Body.String(), "Empty file") {
  504. t.Fatal("Response did not contain 'Empty file'")
  505. }
  506. }
  507. func TestPostEmptyJSONUpload(t *testing.T) {
  508. mux := setup()
  509. w := httptest.NewRecorder()
  510. filename := generateBarename() + ".txt"
  511. var b bytes.Buffer
  512. mw := multipart.NewWriter(&b)
  513. fw, err := mw.CreateFormFile("file", filename)
  514. if err != nil {
  515. t.Fatal(err)
  516. }
  517. fw.Write([]byte(""))
  518. mw.Close()
  519. req, err := http.NewRequest("POST", "/upload/", &b)
  520. req.Header.Set("Content-Type", mw.FormDataContentType())
  521. req.Header.Set("Accept", "application/json")
  522. req.Header.Set("Referer", Config.siteURL)
  523. if err != nil {
  524. t.Fatal(err)
  525. }
  526. mux.ServeHTTP(w, req)
  527. if w.Code != 500 {
  528. t.Log(w.Body.String())
  529. t.Fatalf("Status code is not 500, but %d", w.Code)
  530. }
  531. var myjson RespErrJSON
  532. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  533. if err != nil {
  534. t.Fatal(err)
  535. }
  536. if myjson.Error != "Could not upload file: Empty file" {
  537. t.Fatal("Json 'error' was not 'Empty file' but " + myjson.Error)
  538. }
  539. }
  540. func TestPutUpload(t *testing.T) {
  541. mux := setup()
  542. w := httptest.NewRecorder()
  543. filename := generateBarename() + ".ext"
  544. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  545. if err != nil {
  546. t.Fatal(err)
  547. }
  548. mux.ServeHTTP(w, req)
  549. if w.Body.String() != fmt.Sprintf("%s\n", Config.siteURL+filename) {
  550. t.Fatal("Response was not expected URL")
  551. }
  552. }
  553. func TestPutRandomizedUpload(t *testing.T) {
  554. mux := setup()
  555. w := httptest.NewRecorder()
  556. filename := generateBarename() + ".ext"
  557. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  558. if err != nil {
  559. t.Fatal(err)
  560. }
  561. req.Header.Set("Linx-Randomize", "yes")
  562. mux.ServeHTTP(w, req)
  563. if w.Body.String() == Config.siteURL+filename {
  564. t.Fatal("Filename was not random")
  565. }
  566. }
  567. func TestPutNoExtensionUpload(t *testing.T) {
  568. mux := setup()
  569. w := httptest.NewRecorder()
  570. filename := generateBarename()
  571. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  572. if err != nil {
  573. t.Fatal(err)
  574. }
  575. req.Header.Set("Linx-Randomize", "yes")
  576. mux.ServeHTTP(w, req)
  577. if w.Body.String() == Config.siteURL+filename {
  578. t.Fatal("Filename was not random")
  579. }
  580. }
  581. func TestPutEmptyUpload(t *testing.T) {
  582. mux := setup()
  583. w := httptest.NewRecorder()
  584. filename := generateBarename() + ".ext"
  585. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  586. if err != nil {
  587. t.Fatal(err)
  588. }
  589. req.Header.Set("Linx-Randomize", "yes")
  590. mux.ServeHTTP(w, req)
  591. if !strings.Contains(w.Body.String(), "Empty file") {
  592. t.Fatal("Response doesn't contain'Empty file'")
  593. }
  594. }
  595. func TestPutJSONUpload(t *testing.T) {
  596. var myjson RespOkJSON
  597. mux := setup()
  598. w := httptest.NewRecorder()
  599. filename := generateBarename() + ".ext"
  600. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  601. if err != nil {
  602. t.Fatal(err)
  603. }
  604. req.Header.Set("Accept", "application/json")
  605. mux.ServeHTTP(w, req)
  606. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  607. if err != nil {
  608. t.Fatal(err)
  609. }
  610. if myjson.Filename != filename {
  611. t.Fatal("Filename was not provided one but " + myjson.Filename)
  612. }
  613. }
  614. func TestPutRandomizedJSONUpload(t *testing.T) {
  615. var myjson RespOkJSON
  616. mux := setup()
  617. w := httptest.NewRecorder()
  618. filename := generateBarename() + ".ext"
  619. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  620. if err != nil {
  621. t.Fatal(err)
  622. }
  623. req.Header.Set("Accept", "application/json")
  624. req.Header.Set("Linx-Randomize", "yes")
  625. mux.ServeHTTP(w, req)
  626. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  627. if err != nil {
  628. t.Fatal(err)
  629. }
  630. if myjson.Filename == filename {
  631. t.Fatal("Filename was not random ")
  632. }
  633. }
  634. func TestPutExpireJSONUpload(t *testing.T) {
  635. var myjson RespOkJSON
  636. mux := setup()
  637. w := httptest.NewRecorder()
  638. filename := generateBarename() + ".ext"
  639. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  640. if err != nil {
  641. t.Fatal(err)
  642. }
  643. req.Header.Set("Accept", "application/json")
  644. req.Header.Set("Linx-Expiry", "600")
  645. mux.ServeHTTP(w, req)
  646. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  647. if err != nil {
  648. t.Fatal(err)
  649. }
  650. expiry, err := strconv.Atoi(myjson.Expiry)
  651. if err != nil {
  652. t.Fatal("Expiry was not an integer")
  653. }
  654. if expiry < 1 {
  655. t.Fatal("Expiry was not set")
  656. }
  657. }
  658. func TestPutAndDelete(t *testing.T) {
  659. var myjson RespOkJSON
  660. mux := setup()
  661. w := httptest.NewRecorder()
  662. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  663. if err != nil {
  664. t.Fatal(err)
  665. }
  666. req.Header.Set("Accept", "application/json")
  667. mux.ServeHTTP(w, req)
  668. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  669. if err != nil {
  670. t.Fatal(err)
  671. }
  672. // Delete it
  673. w = httptest.NewRecorder()
  674. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  675. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  676. mux.ServeHTTP(w, req)
  677. if w.Code != 200 {
  678. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  679. }
  680. // Make sure it's actually gone
  681. w = httptest.NewRecorder()
  682. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  683. mux.ServeHTTP(w, req)
  684. if w.Code != 404 {
  685. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  686. }
  687. // Make sure torrent is also gone
  688. w = httptest.NewRecorder()
  689. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  690. mux.ServeHTTP(w, req)
  691. if w.Code != 404 {
  692. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  693. }
  694. }
  695. func TestPutAndOverwrite(t *testing.T) {
  696. var myjson RespOkJSON
  697. mux := setup()
  698. w := httptest.NewRecorder()
  699. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  700. if err != nil {
  701. t.Fatal(err)
  702. }
  703. req.Header.Set("Accept", "application/json")
  704. mux.ServeHTTP(w, req)
  705. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  706. if err != nil {
  707. t.Fatal(err)
  708. }
  709. // Overwrite it
  710. w = httptest.NewRecorder()
  711. req, err = http.NewRequest("PUT", "/upload/"+myjson.Filename, strings.NewReader("New file content"))
  712. req.Header.Set("Linx-Delete-Key", myjson.Delete_Key)
  713. mux.ServeHTTP(w, req)
  714. if w.Code != 200 {
  715. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  716. }
  717. // Make sure it's the new file
  718. w = httptest.NewRecorder()
  719. req, err = http.NewRequest("GET", "/selif/"+myjson.Filename, nil)
  720. mux.ServeHTTP(w, req)
  721. if w.Code == 404 {
  722. t.Fatal("Status code was 404")
  723. }
  724. if w.Body.String() != "New file content" {
  725. t.Fatal("File did not contain 'New file content")
  726. }
  727. }
  728. func TestPutAndSpecificDelete(t *testing.T) {
  729. var myjson RespOkJSON
  730. mux := setup()
  731. w := httptest.NewRecorder()
  732. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  733. if err != nil {
  734. t.Fatal(err)
  735. }
  736. req.Header.Set("Accept", "application/json")
  737. req.Header.Set("Linx-Delete-Key", "supersecret")
  738. mux.ServeHTTP(w, req)
  739. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  740. if err != nil {
  741. t.Fatal(err)
  742. }
  743. // Delete it
  744. w = httptest.NewRecorder()
  745. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  746. req.Header.Set("Linx-Delete-Key", "supersecret")
  747. mux.ServeHTTP(w, req)
  748. if w.Code != 200 {
  749. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  750. }
  751. // Make sure it's actually gone
  752. w = httptest.NewRecorder()
  753. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  754. mux.ServeHTTP(w, req)
  755. if w.Code != 404 {
  756. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  757. }
  758. // Make sure torrent is gone too
  759. w = httptest.NewRecorder()
  760. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  761. mux.ServeHTTP(w, req)
  762. if w.Code != 404 {
  763. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  764. }
  765. }
  766. func TestExtension(t *testing.T) {
  767. barename, extension := barePlusExt("test.jpg.gz")
  768. if barename != "testjpg" {
  769. t.Fatal("Barename was not testjpg, but " + barename)
  770. }
  771. if extension != "gz" {
  772. t.Fatal("Extension was not gz, but " + extension)
  773. }
  774. barename, extension = barePlusExt("test.tar.gz")
  775. if barename != "test" {
  776. t.Fatal("Barename was not test, but " + barename)
  777. }
  778. if extension != "tar.gz" {
  779. t.Fatal("Extension was not tar.gz, but " + extension)
  780. }
  781. }
  782. func TestInferSiteURL(t *testing.T) {
  783. oldSiteURL := Config.siteURL
  784. oldSitePath := Config.sitePath
  785. Config.siteURL = ""
  786. Config.sitePath = "/linxtest/"
  787. mux := setup()
  788. w := httptest.NewRecorder()
  789. req, err := http.NewRequest("GET", "/API/", nil)
  790. req.Host = "example.com:8080"
  791. if err != nil {
  792. t.Fatal(err)
  793. }
  794. mux.ServeHTTP(w, req)
  795. if !strings.Contains(w.Body.String(), "http://example.com:8080/upload/") {
  796. t.Fatal("Site URL not found properly embedded in response")
  797. }
  798. Config.siteURL = oldSiteURL
  799. Config.sitePath = oldSitePath
  800. }
  801. func TestInferSiteURLProxied(t *testing.T) {
  802. oldSiteURL := Config.siteURL
  803. Config.siteURL = ""
  804. mux := setup()
  805. w := httptest.NewRecorder()
  806. req, err := http.NewRequest("GET", "/API/", nil)
  807. req.Header.Add("X-Forwarded-Proto", "https")
  808. req.Host = "example.com:8080"
  809. if err != nil {
  810. t.Fatal(err)
  811. }
  812. mux.ServeHTTP(w, req)
  813. if !strings.Contains(w.Body.String(), "https://example.com:8080/upload/") {
  814. t.Fatal("Site URL not found properly embedded in response")
  815. }
  816. Config.siteURL = oldSiteURL
  817. }
  818. func TestInferSiteURLHTTPS(t *testing.T) {
  819. oldSiteURL := Config.siteURL
  820. oldCertFile := Config.certFile
  821. Config.siteURL = ""
  822. Config.certFile = "/dev/null"
  823. mux := setup()
  824. w := httptest.NewRecorder()
  825. req, err := http.NewRequest("GET", "/API/", nil)
  826. req.Host = "example.com"
  827. if err != nil {
  828. t.Fatal(err)
  829. }
  830. mux.ServeHTTP(w, req)
  831. if !strings.Contains(w.Body.String(), "https://example.com/upload/") {
  832. t.Fatal("Site URL not found properly embedded in response")
  833. }
  834. Config.siteURL = oldSiteURL
  835. Config.certFile = oldCertFile
  836. }
  837. func TestInferSiteURLHTTPSFastCGI(t *testing.T) {
  838. oldSiteURL := Config.siteURL
  839. Config.siteURL = ""
  840. mux := setup()
  841. w := httptest.NewRecorder()
  842. req, err := http.NewRequest("GET", "/API/", nil)
  843. req.Host = "example.com"
  844. req.TLS = &tls.ConnectionState{HandshakeComplete: true}
  845. if err != nil {
  846. t.Fatal(err)
  847. }
  848. mux.ServeHTTP(w, req)
  849. if !strings.Contains(w.Body.String(), "https://example.com/upload/") {
  850. t.Fatal("Site URL not found properly embedded in response")
  851. }
  852. Config.siteURL = oldSiteURL
  853. }
  854. func TestShutdown(t *testing.T) {
  855. os.RemoveAll(Config.filesDir)
  856. os.RemoveAll(Config.metaDir)
  857. }