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.

341 lines
7.4 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. t.Log(os.TempDir())
  17. Config.metaDir = Config.filesDir + "_meta"
  18. Config.noLogs = true
  19. Config.siteName = "linx"
  20. setup()
  21. }
  22. func TestIndex(t *testing.T) {
  23. w := httptest.NewRecorder()
  24. req, err := http.NewRequest("GET", "/", nil)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. goji.DefaultMux.ServeHTTP(w, req)
  29. if !strings.Contains(w.Body.String(), "file-uploader") {
  30. t.Fatal("String 'file-uploader' not found in index response")
  31. }
  32. }
  33. func TestNotFound(t *testing.T) {
  34. w := httptest.NewRecorder()
  35. req, err := http.NewRequest("GET", "/url/should/not/exist", nil)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. goji.DefaultMux.ServeHTTP(w, req)
  40. if w.Code != 404 {
  41. t.Fatalf("Expected 404, got %d", w.Code)
  42. }
  43. }
  44. func TestFileNotFound(t *testing.T) {
  45. w := httptest.NewRecorder()
  46. filename := randomString(10)
  47. req, err := http.NewRequest("GET", "/selif/"+filename, nil)
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. goji.DefaultMux.ServeHTTP(w, req)
  52. if w.Code != 404 {
  53. t.Fatalf("Expected 404, got %d", w.Code)
  54. }
  55. }
  56. func TestDisplayNotFound(t *testing.T) {
  57. w := httptest.NewRecorder()
  58. filename := randomString(10)
  59. req, err := http.NewRequest("GET", "/"+filename, nil)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. goji.DefaultMux.ServeHTTP(w, req)
  64. if w.Code != 404 {
  65. t.Fatalf("Expected 404, got %d", w.Code)
  66. }
  67. }
  68. func TestPostBodyUpload(t *testing.T) {
  69. w := httptest.NewRecorder()
  70. filename := randomString(10) + ".ext"
  71. req, err := http.NewRequest("POST", "/upload", strings.NewReader("File content"))
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. req.Header.Set("Content-Type", "application/octet-stream")
  76. params := req.URL.Query()
  77. params.Add("qqfile", filename)
  78. req.URL.RawQuery = params.Encode()
  79. goji.DefaultMux.ServeHTTP(w, req)
  80. if w.Code != 301 {
  81. t.Fatalf("Status code is not 301, but %d", w.Code)
  82. }
  83. if w.Header().Get("Location") != "/"+filename {
  84. t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  85. }
  86. }
  87. func TestPostEmptyBodyUpload(t *testing.T) {
  88. w := httptest.NewRecorder()
  89. filename := randomString(10) + ".ext"
  90. req, err := http.NewRequest("POST", "/upload", strings.NewReader(""))
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. req.Header.Set("Content-Type", "application/octet-stream")
  95. params := req.URL.Query()
  96. params.Add("qqfile", filename)
  97. req.URL.RawQuery = params.Encode()
  98. goji.DefaultMux.ServeHTTP(w, req)
  99. if w.Code == 301 {
  100. t.Fatal("Status code is 301")
  101. }
  102. if !strings.Contains(w.Body.String(), "Oops! Something went wrong.") {
  103. t.Fatal("Response doesn't contain'Oops! Something went wrong.'")
  104. }
  105. }
  106. func TestPostBodyRandomizeUpload(t *testing.T) {
  107. w := httptest.NewRecorder()
  108. filename := randomString(10) + ".ext"
  109. req, err := http.NewRequest("POST", "/upload", strings.NewReader("File content"))
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. req.Header.Set("Content-Type", "application/octet-stream")
  114. params := req.URL.Query()
  115. params.Add("qqfile", filename)
  116. params.Add("randomize", "true")
  117. req.URL.RawQuery = params.Encode()
  118. goji.DefaultMux.ServeHTTP(w, req)
  119. if w.Code != 301 {
  120. t.Fatalf("Status code is not 301, but %d", w.Code)
  121. }
  122. if w.Header().Get("Location") == "/"+filename {
  123. t.Fatalf("Was redirected to %s instead of something random", filename)
  124. }
  125. }
  126. func TestPostBodyExpireUpload(t *testing.T) {
  127. // Dependant on json info on display url to check expiry
  128. }
  129. func TestPutUpload(t *testing.T) {
  130. w := httptest.NewRecorder()
  131. filename := randomString(10) + ".ext"
  132. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. goji.DefaultMux.ServeHTTP(w, req)
  137. if w.Body.String() != Config.siteURL+filename {
  138. t.Fatal("Response was not expected URL")
  139. }
  140. // if w.Code != 301 {
  141. // t.Fatalf("Status code is not 301, but %d", w.Code)
  142. // }
  143. // if w.Header().Get("Location") != "/"+filename {
  144. // t.Fatalf("Was redirected to %s instead of /%s", w.Header().Get("Location"), filename)
  145. // }
  146. }
  147. func TestPutRandomizedUpload(t *testing.T) {
  148. w := httptest.NewRecorder()
  149. filename := randomString(10) + ".ext"
  150. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. req.Header.Set("X-Randomized-Barename", "yes")
  155. goji.DefaultMux.ServeHTTP(w, req)
  156. if w.Body.String() == Config.siteURL+filename {
  157. t.Fatal("Filename was not random")
  158. }
  159. }
  160. func TestPutEmptyUpload(t *testing.T) {
  161. w := httptest.NewRecorder()
  162. filename := randomString(10) + ".ext"
  163. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader(""))
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. req.Header.Set("X-Randomized-Barename", "yes")
  168. goji.DefaultMux.ServeHTTP(w, req)
  169. if !strings.Contains(w.Body.String(), "Oops! Something went wrong.") {
  170. t.Fatal("Response doesn't contain'Oops! Something went wrong.'")
  171. }
  172. }
  173. func TestPutJSONUpload(t *testing.T) {
  174. type RespJSON struct {
  175. Filename string `json: filename`
  176. Url string `json: url`
  177. DeleteKey string `json: delete_key`
  178. Expiry string `json: expiry`
  179. Size string `json: size`
  180. }
  181. var myjson RespJSON
  182. w := httptest.NewRecorder()
  183. filename := randomString(10) + ".ext"
  184. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. req.Header.Set("Accept", "application/json")
  189. goji.DefaultMux.ServeHTTP(w, req)
  190. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. if myjson.Filename != filename {
  195. t.Fatal("Filename was not provided one but " + myjson.Filename)
  196. }
  197. }
  198. func TestPutRandomizedJSONUpload(t *testing.T) {
  199. type RespJSON struct {
  200. Filename string `json: filename`
  201. Url string `json: url`
  202. DeleteKey string `json: delete_key`
  203. Expiry string `json: expiry`
  204. Size string `json: size`
  205. }
  206. var myjson RespJSON
  207. w := httptest.NewRecorder()
  208. filename := randomString(10) + ".ext"
  209. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  210. if err != nil {
  211. t.Fatal(err)
  212. }
  213. req.Header.Set("Accept", "application/json")
  214. req.Header.Set("X-Randomized-Barename", "yes")
  215. goji.DefaultMux.ServeHTTP(w, req)
  216. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  217. if err != nil {
  218. t.Fatal(err)
  219. }
  220. if myjson.Filename == filename {
  221. t.Fatal("Filename was not random ")
  222. }
  223. }
  224. func TestPutExpireJSONUpload(t *testing.T) {
  225. type RespJSON struct {
  226. Filename string `json: filename`
  227. Url string `json: url`
  228. DeleteKey string `json: delete_key`
  229. Expiry string `json: expiry`
  230. Size string `json: size`
  231. }
  232. var myjson RespJSON
  233. w := httptest.NewRecorder()
  234. filename := randomString(10) + ".ext"
  235. req, err := http.NewRequest("PUT", "/upload/"+filename, strings.NewReader("File content"))
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. req.Header.Set("Accept", "application/json")
  240. req.Header.Set("X-File-Expiry", "600")
  241. goji.DefaultMux.ServeHTTP(w, req)
  242. err = json.Unmarshal([]byte(w.Body.String()), &myjson)
  243. if err != nil {
  244. t.Fatal(err)
  245. }
  246. expiry, err := strconv.Atoi(myjson.Expiry)
  247. if err != nil {
  248. t.Fatal("Expiry was not an integer")
  249. }
  250. if expiry < 1 {
  251. t.Fatal("Expiry was not set")
  252. }
  253. }
  254. func TestShutdown(t *testing.T) {
  255. os.RemoveAll(Config.filesDir)
  256. os.RemoveAll(Config.metaDir)
  257. }