Micro & pluggable web framework for Go
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.

476 lines
9.3KB

  1. // Copyright 2015 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "bytes"
  7. "fmt"
  8. "net/http"
  9. "net/http/httptest"
  10. "strings"
  11. "testing"
  12. )
  13. type CtxAction struct {
  14. Ctx
  15. }
  16. func (p *CtxAction) Get() {
  17. p.Ctx.Write([]byte("context"))
  18. }
  19. func TestContext1(t *testing.T) {
  20. buff := bytes.NewBufferString("")
  21. recorder := httptest.NewRecorder()
  22. recorder.Body = buff
  23. o := Classic()
  24. o.Get("/", new(CtxAction))
  25. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  26. if err != nil {
  27. t.Error(err)
  28. }
  29. o.ServeHTTP(recorder, req)
  30. expect(t, recorder.Code, http.StatusOK)
  31. refute(t, len(buff.String()), 0)
  32. expect(t, buff.String(), "context")
  33. }
  34. type CtxJSONAction struct {
  35. Ctx
  36. }
  37. func (p *CtxJSONAction) Get() error {
  38. return p.Ctx.ServeJson(map[string]string{
  39. "get": "ctx",
  40. })
  41. }
  42. func TestContext2(t *testing.T) {
  43. buff := bytes.NewBufferString("")
  44. recorder := httptest.NewRecorder()
  45. recorder.Body = buff
  46. o := Classic()
  47. o.Get("/", new(CtxJSONAction))
  48. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  49. if err != nil {
  50. t.Error(err)
  51. }
  52. o.ServeHTTP(recorder, req)
  53. expect(t, recorder.Code, http.StatusOK)
  54. refute(t, len(buff.String()), 0)
  55. expect(t, recorder.Header().Get("Content-Type"), "application/json; charset=UTF-8")
  56. expect(t, strings.TrimSpace(buff.String()), `{"get":"ctx"}`)
  57. }
  58. type CtxXMLAction struct {
  59. Ctx
  60. }
  61. type XMLStruct struct {
  62. Content string
  63. }
  64. func (p *CtxXMLAction) Get() error {
  65. return p.Ctx.ServeXml(XMLStruct{"content"})
  66. }
  67. func TestContext3(t *testing.T) {
  68. buff := bytes.NewBufferString("")
  69. recorder := httptest.NewRecorder()
  70. recorder.Body = buff
  71. o := Classic()
  72. o.Get("/", new(CtxXMLAction))
  73. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  74. if err != nil {
  75. t.Error(err)
  76. }
  77. o.ServeHTTP(recorder, req)
  78. expect(t, recorder.Code, http.StatusOK)
  79. expect(t, recorder.Header().Get("Content-Type"), "application/xml; charset=UTF-8")
  80. refute(t, len(buff.String()), 0)
  81. expect(t, strings.TrimSpace(buff.String()), `<XMLStruct><Content>content</Content></XMLStruct>`)
  82. }
  83. type CtxFileAction struct {
  84. Ctx
  85. }
  86. func (p *CtxFileAction) Get() error {
  87. return p.Ctx.ServeFile("./public/index.html")
  88. }
  89. func TestContext4(t *testing.T) {
  90. buff := bytes.NewBufferString("")
  91. recorder := httptest.NewRecorder()
  92. recorder.Body = buff
  93. o := Classic()
  94. o.Any("/", new(CtxFileAction))
  95. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  96. if err != nil {
  97. t.Error(err)
  98. }
  99. o.ServeHTTP(recorder, req)
  100. expect(t, recorder.Code, http.StatusOK)
  101. refute(t, len(buff.String()), 0)
  102. expect(t, strings.TrimSpace(buff.String()), `this is index.html`)
  103. }
  104. func TestContext5(t *testing.T) {
  105. buff := bytes.NewBufferString("")
  106. recorder := httptest.NewRecorder()
  107. recorder.Body = buff
  108. o := Classic()
  109. o.Any("/2", func() string {
  110. return "2"
  111. })
  112. o.Any("/", func(ctx *Context) {
  113. ctx.Redirect("/2")
  114. })
  115. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  116. if err != nil {
  117. t.Error(err)
  118. }
  119. o.ServeHTTP(recorder, req)
  120. expect(t, recorder.Code, http.StatusFound)
  121. refute(t, len(buff.String()), 0)
  122. expect(t, strings.TrimSpace(buff.String()), `<a href="/2">Found</a>.`)
  123. }
  124. type NotFoundAction struct {
  125. Ctx
  126. }
  127. func (n *NotFoundAction) Get() {
  128. n.NotFound("not found")
  129. }
  130. func TestContext6(t *testing.T) {
  131. buff := bytes.NewBufferString("")
  132. recorder := httptest.NewRecorder()
  133. recorder.Body = buff
  134. o := Classic()
  135. o.Any("/", new(NotFoundAction))
  136. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  137. if err != nil {
  138. t.Error(err)
  139. }
  140. o.ServeHTTP(recorder, req)
  141. expect(t, recorder.Code, http.StatusNotFound)
  142. refute(t, len(buff.String()), 0)
  143. expect(t, strings.TrimSpace(buff.String()), "not found")
  144. }
  145. type NotModifidAction struct {
  146. Ctx
  147. }
  148. func (n *NotModifidAction) Get() {
  149. n.NotModified()
  150. }
  151. func TestContext7(t *testing.T) {
  152. buff := bytes.NewBufferString("")
  153. recorder := httptest.NewRecorder()
  154. recorder.Body = buff
  155. o := Classic()
  156. o.Any("/", new(NotModifidAction))
  157. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  158. if err != nil {
  159. t.Error(err)
  160. }
  161. o.ServeHTTP(recorder, req)
  162. expect(t, recorder.Code, http.StatusNotModified)
  163. expect(t, len(buff.String()), 0)
  164. }
  165. type UnauthorizedAction struct {
  166. Ctx
  167. }
  168. func (n *UnauthorizedAction) Get() {
  169. n.Unauthorized()
  170. }
  171. func TestContext8(t *testing.T) {
  172. buff := bytes.NewBufferString("")
  173. recorder := httptest.NewRecorder()
  174. recorder.Body = buff
  175. o := Classic()
  176. o.Any("/", new(UnauthorizedAction))
  177. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  178. if err != nil {
  179. t.Error(err)
  180. }
  181. o.ServeHTTP(recorder, req)
  182. expect(t, recorder.Code, http.StatusUnauthorized)
  183. expect(t, buff.String(), http.StatusText(http.StatusUnauthorized))
  184. }
  185. type DownloadAction struct {
  186. Ctx
  187. }
  188. func (n *DownloadAction) Get() {
  189. n.Download("./public/index.html")
  190. }
  191. func TestContext9(t *testing.T) {
  192. buff := bytes.NewBufferString("")
  193. recorder := httptest.NewRecorder()
  194. recorder.Body = buff
  195. o := Classic()
  196. o.Any("/", new(DownloadAction))
  197. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  198. if err != nil {
  199. t.Error(err)
  200. }
  201. o.ServeHTTP(recorder, req)
  202. expect(t, recorder.Header().Get("Content-Disposition"), `attachment; filename="index.html"`)
  203. expect(t, recorder.Code, http.StatusOK)
  204. expect(t, buff.String(), "this is index.html")
  205. }
  206. // check unsupported function will panic
  207. func TestContext10(t *testing.T) {
  208. buff := bytes.NewBufferString("")
  209. recorder := httptest.NewRecorder()
  210. recorder.Body = buff
  211. o := Classic()
  212. var ifPanic bool
  213. defer func() {
  214. if err := recover(); err != nil {
  215. ifPanic = true
  216. }
  217. expect(t, ifPanic, true)
  218. }()
  219. o.Any("/", func(i int) {
  220. fmt.Println(i)
  221. })
  222. }
  223. type DownloadAction2 struct {
  224. Ctx
  225. }
  226. func (n *DownloadAction2) Get() {
  227. n.ServeFile("./public/a.html")
  228. }
  229. func TestContext11(t *testing.T) {
  230. buff := bytes.NewBufferString("")
  231. recorder := httptest.NewRecorder()
  232. recorder.Body = buff
  233. o := Classic()
  234. o.Any("/", new(DownloadAction2))
  235. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  236. if err != nil {
  237. t.Error(err)
  238. }
  239. o.ServeHTTP(recorder, req)
  240. expect(t, recorder.Code, http.StatusNotFound)
  241. }
  242. func TestContext12(t *testing.T) {
  243. buff := bytes.NewBufferString("")
  244. recorder := httptest.NewRecorder()
  245. recorder.Body = buff
  246. o := Classic()
  247. o.Any("/", func() string {
  248. return "text"
  249. })
  250. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  251. if err != nil {
  252. t.Error(err)
  253. }
  254. o.ServeHTTP(recorder, req)
  255. expect(t, recorder.Code, http.StatusOK)
  256. expect(t, buff.String(), "text")
  257. }
  258. func TestContext13(t *testing.T) {
  259. buff := bytes.NewBufferString("")
  260. recorder := httptest.NewRecorder()
  261. recorder.Body = buff
  262. o := Classic()
  263. o.Get("/", func(responseWriter http.ResponseWriter, req *http.Request) {
  264. responseWriter.Write([]byte("text"))
  265. })
  266. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  267. if err != nil {
  268. t.Error(err)
  269. }
  270. o.ServeHTTP(recorder, req)
  271. expect(t, recorder.Code, http.StatusOK)
  272. expect(t, buff.String(), "text")
  273. }
  274. func TestContext14(t *testing.T) {
  275. buff := bytes.NewBufferString("")
  276. recorder := httptest.NewRecorder()
  277. recorder.Body = buff
  278. o := Classic()
  279. o.Get("/", func(req *http.Request) string {
  280. return "text"
  281. })
  282. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  283. if err != nil {
  284. t.Error(err)
  285. }
  286. o.ServeHTTP(recorder, req)
  287. expect(t, recorder.Code, http.StatusOK)
  288. expect(t, buff.String(), "text")
  289. }
  290. func TestContextDecodeJSON(t *testing.T) {
  291. buff := bytes.NewBufferString("")
  292. recorder := httptest.NewRecorder()
  293. recorder.Body = buff
  294. type Data struct {
  295. Name string
  296. }
  297. o := Classic()
  298. o.Post("/", func(ctx *Context) string {
  299. var data Data
  300. err := ctx.DecodeJSON(&data)
  301. if err != nil {
  302. return err.Error()
  303. }
  304. return data.Name
  305. })
  306. req, err := http.NewRequest("POST", "http://localhost:8000/", bytes.NewBufferString(`
  307. {
  308. "Name": "lunny"
  309. }
  310. `))
  311. if err != nil {
  312. t.Error(err)
  313. }
  314. o.ServeHTTP(recorder, req)
  315. expect(t, recorder.Code, http.StatusOK)
  316. expect(t, buff.String(), "lunny")
  317. }
  318. func TestContextDecodeXML(t *testing.T) {
  319. buff := bytes.NewBufferString("")
  320. recorder := httptest.NewRecorder()
  321. recorder.Body = buff
  322. type Data struct {
  323. Name string `xml:"name"`
  324. }
  325. o := Classic()
  326. o.Post("/", func(ctx *Context) string {
  327. var data Data
  328. err := ctx.DecodeXML(&data)
  329. if err != nil {
  330. return err.Error()
  331. }
  332. return data.Name
  333. })
  334. req, err := http.NewRequest("POST", "http://localhost:8000/", bytes.NewBufferString(`
  335. <Data><name>lunny</name></Data>
  336. `))
  337. if err != nil {
  338. t.Error(err)
  339. }
  340. o.ServeHTTP(recorder, req)
  341. expect(t, recorder.Code, http.StatusOK)
  342. expect(t, buff.String(), "lunny")
  343. }
  344. type ActionTag struct {
  345. Name string `tag:"name"`
  346. }
  347. func (a *ActionTag) Get() interface{} {
  348. return "lunny"
  349. }
  350. func TestContextActionTag(t *testing.T) {
  351. buff := bytes.NewBufferString("")
  352. recorder := httptest.NewRecorder()
  353. recorder.Body = buff
  354. o := Classic()
  355. o.Use(HandlerFunc(func(ctx *Context) {
  356. ctx.Next()
  357. v := ctx.ActionValue()
  358. if a, ok := v.Interface().(*ActionTag); ok {
  359. fmt.Println(a.Get())
  360. }
  361. tagName := ctx.ActionTag("Name")
  362. expect(t, `tag:"name"`, tagName)
  363. }))
  364. o.Get("/", new(ActionTag))
  365. req, err := http.NewRequest("GET", "http://localhost:8000/", nil)
  366. if err != nil {
  367. t.Error(err)
  368. }
  369. o.ServeHTTP(recorder, req)
  370. expect(t, recorder.Code, http.StatusOK)
  371. expect(t, buff.String(), "lunny")
  372. }