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.

363 lines
7.5 KiB

9 years ago
9 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "os"
  7. "path"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. "github.com/zenazn/goji"
  12. )
  13. func TestSetup(t *testing.T) {
  14. Config.siteURL = "http://linx.example.org/"
  15. Config.filesDir = path.Join(os.TempDir(), randomString(10))
  16. Config.metaDir = Config.filesDir + "_meta"
  17. Config.noLogs = true
  18. Config.siteName = "linx"
  19. setup()
  20. }
  21. func TestIndex(t *testing.T) {
  22. w := httptest.NewRecorder()
  23. req, err := http.NewRequest("GET", "/", nil)
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. goji.DefaultMux.ServeHTTP(w, req)
  28. if !strings.Contains(w.Body.String(), "Click or Drop file") {
  29. t.Fatal("String 'Click or Drop file' not found in index response")
  30. }
  31. }
  32. func TestNotFound(t *testing.T) {
  33. w := httptest.NewRecorder()
  34. req, err := http.NewRequest("GET", "/url/should/not/exist", nil)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. goji.DefaultMux.ServeHTTP(w, req)
  39. if w.Code != 404 {
  40. t.Fatalf("Expected 404, got %d", w.Code)
  41. }
  42. }
  43. func TestFileNotFound(t *testing.T) {
  44. w := httptest.NewRecorder()
  45. filename := randomString(10)
  46. req, err := http.NewRequest("GET", "/selif/"+filename, nil)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. goji.DefaultMux.ServeHTTP(w, req)
  51. if w.Code != 404 {
  52. t.Fatalf("Expected 404, got %d", w.Code)
  53. }
  54. }
  55. func TestDisplayNotFound(t *testing.T) {
  56. w := httptest.NewRecorder()
  57. filename := randomString(10)
  58. req, err := http.NewRequest("GET", "/"+filename, nil)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. goji.DefaultMux.ServeHTTP(w, req)
  63. if w.Code != 404 {
  64. t.Fatalf("Expected 404, got %d", w.Code)
  65. }
  66. }
  67. func TestPutUpload(t *testing.T) {
  68. w := httptest.NewRecorder()
  69. filename := randomString(10) + ".ext"
  70. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. goji.DefaultMux.ServeHTTP(w, req)
  75. if w.Body.String() != Config.siteURL+filename {
  76. t.Fatal("Response was not expected URL")
  77. }
  78. }
  79. func TestPutRandomizedUpload(t *testing.T) {
  80. w := httptest.NewRecorder()
  81. filename := randomString(10) + ".ext"
  82. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. req.Header.Set("X-Randomized-Barename", "yes")
  87. goji.DefaultMux.ServeHTTP(w, req)
  88. if w.Body.String() == Config.siteURL+filename {
  89. t.Fatal("Filename was not random")
  90. }
  91. }
  92. func TestPutEmptyUpload(t *testing.T) {
  93. w := httptest.NewRecorder()
  94. filename := randomString(10) + ".ext"
  95. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. req.Header.Set("X-Randomized-Barename", "yes")
  100. goji.DefaultMux.ServeHTTP(w, req)
  101. if !strings.Contains(w.Body.String(), "Empty file") {
  102. t.Fatal("Response doesn't contain'Empty file'")
  103. }
  104. }
  105. func TestPutJSONUpload(t *testing.T) {
  106. type RespJSON struct {
  107. Filename string
  108. Url string
  109. Delete_Key string
  110. Expiry string
  111. Size string
  112. }
  113. var myjson RespJSON
  114. w := httptest.NewRecorder()
  115. filename := randomString(10) + ".ext"
  116. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. req.Header.Set("Accept", "application/json")
  121. goji.DefaultMux.ServeHTTP(w, req)
  122. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if myjson.Filename != filename {
  127. t.Fatal("Filename was not provided one but " + myjson.Filename)
  128. }
  129. }
  130. func TestPutRandomizedJSONUpload(t *testing.T) {
  131. type RespJSON struct {
  132. Filename string
  133. Url string
  134. Delete_Key string
  135. Expiry string
  136. Size string
  137. }
  138. var myjson RespJSON
  139. w := httptest.NewRecorder()
  140. filename := randomString(10) + ".ext"
  141. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. req.Header.Set("Accept", "application/json")
  146. req.Header.Set("X-Randomized-Barename", "yes")
  147. goji.DefaultMux.ServeHTTP(w, req)
  148. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. if myjson.Filename == filename {
  153. t.Fatal("Filename was not random ")
  154. }
  155. }
  156. func TestPutExpireJSONUpload(t *testing.T) {
  157. type RespJSON struct {
  158. Filename string
  159. Url string
  160. Delete_Key string
  161. Expiry string
  162. Size string
  163. }
  164. var myjson RespJSON
  165. w := httptest.NewRecorder()
  166. filename := randomString(10) + ".ext"
  167. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. req.Header.Set("Accept", "application/json")
  172. req.Header.Set("X-File-Expiry", "600")
  173. goji.DefaultMux.ServeHTTP(w, req)
  174. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. expiry, err := strconv.Atoi(myjson.Expiry)
  179. if err != nil {
  180. t.Fatal("Expiry was not an integer")
  181. }
  182. if expiry < 1 {
  183. t.Fatal("Expiry was not set")
  184. }
  185. }
  186. func TestPutAndDelete(t *testing.T) {
  187. type RespJSON struct {
  188. Filename string
  189. Url string
  190. Delete_Key string
  191. Expiry string
  192. Size string
  193. }
  194. var myjson RespJSON
  195. w := httptest.NewRecorder()
  196. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. req.Header.Set("Accept", "application/json")
  201. goji.DefaultMux.ServeHTTP(w, req)
  202. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. // Delete it
  207. w = httptest.NewRecorder()
  208. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  209. req.Header.Set("X-Delete-Key", myjson.Delete_Key)
  210. goji.DefaultMux.ServeHTTP(w, req)
  211. if w.Code != 200 {
  212. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  213. }
  214. // Make sure it's actually gone
  215. w = httptest.NewRecorder()
  216. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  217. goji.DefaultMux.ServeHTTP(w, req)
  218. if w.Code != 404 {
  219. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  220. }
  221. // Make sure torrent is also gone
  222. w = httptest.NewRecorder()
  223. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  224. goji.DefaultMux.ServeHTTP(w, req)
  225. if w.Code != 404 {
  226. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  227. }
  228. }
  229. func TestPutAndSpecificDelete(t *testing.T) {
  230. type RespJSON struct {
  231. Filename string
  232. Url string
  233. Delete_Key string
  234. Expiry string
  235. Size string
  236. }
  237. var myjson RespJSON
  238. w := httptest.NewRecorder()
  239. req, err := http.NewRequest("PUT", "/upload", strings.NewReader("File content"))
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. req.Header.Set("Accept", "application/json")
  244. req.Header.Set("X-Delete-Key", "supersecret")
  245. goji.DefaultMux.ServeHTTP(w, req)
  246. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. // Delete it
  251. w = httptest.NewRecorder()
  252. req, err = http.NewRequest("DELETE", "/"+myjson.Filename, nil)
  253. req.Header.Set("X-Delete-Key", "supersecret")
  254. goji.DefaultMux.ServeHTTP(w, req)
  255. if w.Code != 200 {
  256. t.Fatal("Status code was not 200, but " + strconv.Itoa(w.Code))
  257. }
  258. // Make sure it's actually gone
  259. w = httptest.NewRecorder()
  260. req, err = http.NewRequest("GET", "/"+myjson.Filename, nil)
  261. goji.DefaultMux.ServeHTTP(w, req)
  262. if w.Code != 404 {
  263. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  264. }
  265. // Make sure torrent is gone too
  266. w = httptest.NewRecorder()
  267. req, err = http.NewRequest("GET", "/"+myjson.Filename+"/torrent", nil)
  268. goji.DefaultMux.ServeHTTP(w, req)
  269. if w.Code != 404 {
  270. t.Fatal("Status code was not 404, but " + strconv.Itoa(w.Code))
  271. }
  272. }
  273. func TestShutdown(t *testing.T) {
  274. os.RemoveAll(Config.filesDir)
  275. os.RemoveAll(Config.metaDir)
  276. }