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.

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