surf_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. package surf
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/headzoo/surf/jar"
  6. "github.com/headzoo/ut"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. )
  11. func TestGet(t *testing.T) {
  12. ut.Run(t)
  13. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  14. if req.URL.Path == "/page1" {
  15. fmt.Fprint(w, htmlPage1)
  16. } else if req.URL.Path == "/page2" {
  17. fmt.Fprint(w, htmlPage2)
  18. }
  19. }))
  20. defer ts.Close()
  21. bow := NewBrowser()
  22. err := bow.Open(ts.URL + "/page1")
  23. ut.AssertNil(err)
  24. ut.AssertEquals("Surf Page 1", bow.Title())
  25. ut.AssertContains("<p>Hello, Surf!</p>", bow.Body())
  26. err = bow.Open(ts.URL + "/page2")
  27. ut.AssertNil(err)
  28. ut.AssertEquals("Surf Page 2", bow.Title())
  29. ok := bow.Back()
  30. ut.AssertTrue(ok)
  31. ut.AssertEquals("Surf Page 1", bow.Title())
  32. ok = bow.Back()
  33. ut.AssertFalse(ok)
  34. ut.AssertEquals("Surf Page 1", bow.Title())
  35. }
  36. func TestPost(t *testing.T) {
  37. ut.Run(t)
  38. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  39. if req.Method == "POST" {
  40. w.WriteHeader(200)
  41. w.Write([]byte("OK"))
  42. } else {
  43. w.WriteHeader(500)
  44. w.Write([]byte("ERROR"))
  45. }
  46. }))
  47. defer ts.Close()
  48. bow := NewBrowser()
  49. bow.Post(ts.URL, "application/x-www-form-urlencoded", nil)
  50. ut.AssertEquals(200, bow.StatusCode())
  51. }
  52. func TestHead(t *testing.T) {
  53. ut.Run(t)
  54. var r *http.Request
  55. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  56. r = req
  57. }))
  58. defer ts.Close()
  59. bow := NewBrowser()
  60. err := bow.Head(ts.URL + "/page1")
  61. ut.AssertNil(err)
  62. ut.AssertNotNil(r)
  63. }
  64. func TestDownload(t *testing.T) {
  65. ut.Run(t)
  66. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  67. fmt.Fprint(w, req.UserAgent())
  68. }))
  69. defer ts.Close()
  70. bow := NewBrowser()
  71. bow.Open(ts.URL)
  72. buff := &bytes.Buffer{}
  73. l, err := bow.Download(buff)
  74. ut.AssertNil(err)
  75. ut.AssertGreaterThan(0, int(l))
  76. ut.AssertEquals(int(l), buff.Len())
  77. }
  78. func TestDownloadContentType(t *testing.T) {
  79. ut.Run(t)
  80. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  81. b := bytes.NewBufferString("Hello")
  82. fmt.Fprint(w, b)
  83. }))
  84. defer ts.Close()
  85. bow := NewBrowser()
  86. bow.Open(ts.URL)
  87. buff := &bytes.Buffer{}
  88. bow.Download(buff)
  89. ut.AssertEquals("Hello", buff.String())
  90. }
  91. func TestUserAgent(t *testing.T) {
  92. ut.Run(t)
  93. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  94. fmt.Fprint(w, req.UserAgent())
  95. }))
  96. defer ts.Close()
  97. bow := NewBrowser()
  98. bow.SetUserAgent("Testing/1.0")
  99. err := bow.Open(ts.URL)
  100. ut.AssertNil(err)
  101. ut.AssertEquals("Testing/1.0", bow.Body())
  102. }
  103. func TestHeaders(t *testing.T) {
  104. ut.Run(t)
  105. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  106. fmt.Fprint(w, req.Header.Get("X-Testing-1"))
  107. fmt.Fprint(w, req.Header.Get("X-Testing-2"))
  108. }))
  109. defer ts.Close()
  110. bow := NewBrowser()
  111. bow.AddRequestHeader("X-Testing-1", "Testing-1")
  112. bow.AddRequestHeader("X-Testing-2", "Testing-2")
  113. err := bow.Open(ts.URL)
  114. ut.AssertNil(err)
  115. ut.AssertContains("Testing-1", bow.Body())
  116. ut.AssertContains("Testing-2", bow.Body())
  117. }
  118. // TestHeadersSet
  119. // See: https://github.com/headzoo/surf/pull/19
  120. func TestHeadersBug19(t *testing.T) {
  121. ut.Run(t)
  122. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  123. fmt.Fprint(w, req.Header.Get("X-Testing"))
  124. }))
  125. defer ts.Close()
  126. bow := NewBrowser()
  127. bow.AddRequestHeader("X-Testing", "Testing-1")
  128. bow.AddRequestHeader("X-Testing", "Testing-2")
  129. err := bow.Open(ts.URL)
  130. ut.AssertNil(err)
  131. ut.AssertContains("Testing-2", bow.Body())
  132. }
  133. func TestBookmarks(t *testing.T) {
  134. ut.Run(t)
  135. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  136. fmt.Fprint(w, htmlPage1)
  137. }))
  138. defer ts.Close()
  139. bookmarks := jar.NewMemoryBookmarks()
  140. bow := NewBrowser()
  141. bow.SetBookmarksJar(bookmarks)
  142. bookmarks.Save("test1", ts.URL)
  143. bow.OpenBookmark("test1")
  144. ut.AssertEquals("Surf Page 1", bow.Title())
  145. ut.AssertContains("<p>Hello, Surf!</p>", bow.Body())
  146. err := bow.Bookmark("test2")
  147. ut.AssertNil(err)
  148. bow.OpenBookmark("test2")
  149. ut.AssertEquals("Surf Page 1", bow.Title())
  150. }
  151. func TestClick(t *testing.T) {
  152. ut.Run(t)
  153. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  154. if r.URL.Path == "/" {
  155. fmt.Fprint(w, htmlPage1)
  156. } else if r.URL.Path == "/page2" {
  157. fmt.Fprint(w, htmlPage1)
  158. }
  159. }))
  160. defer ts.Close()
  161. bow := NewBrowser()
  162. err := bow.Open(ts.URL)
  163. ut.AssertNil(err)
  164. err = bow.Click("a:contains('click')")
  165. ut.AssertNil(err)
  166. ut.AssertContains("<p>Hello, Surf!</p>", bow.Body())
  167. }
  168. func TestLinks(t *testing.T) {
  169. ut.Run(t)
  170. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  171. fmt.Fprint(w, htmlPage1)
  172. }))
  173. defer ts.Close()
  174. bow := NewBrowser()
  175. err := bow.Open(ts.URL)
  176. ut.AssertNil(err)
  177. links := bow.Links()
  178. ut.AssertEquals(2, len(links))
  179. ut.AssertEquals("", links[0].ID)
  180. ut.AssertEquals(ts.URL+"/page2", links[0].URL.String())
  181. ut.AssertEquals("click", links[0].Text)
  182. ut.AssertEquals("page3", links[1].ID)
  183. ut.AssertEquals(ts.URL+"/page3", links[1].URL.String())
  184. ut.AssertEquals("no clicking", links[1].Text)
  185. }
  186. func TestImages(t *testing.T) {
  187. ut.Run(t)
  188. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  189. fmt.Fprint(w, htmlPage1)
  190. }))
  191. defer ts.Close()
  192. bow := NewBrowser()
  193. err := bow.Open(ts.URL)
  194. ut.AssertNil(err)
  195. images := bow.Images()
  196. ut.AssertEquals(2, len(images))
  197. ut.AssertEquals("imgur-image", images[0].ID)
  198. ut.AssertEquals("http://i.imgur.com/HW4bJtY.jpg", images[0].URL.String())
  199. ut.AssertEquals("", images[0].Alt)
  200. ut.AssertEquals("It's a...", images[0].Title)
  201. ut.AssertEquals("", images[1].ID)
  202. ut.AssertEquals(ts.URL+"/Cxagv.jpg", images[1].URL.String())
  203. ut.AssertEquals("A picture", images[1].Alt)
  204. ut.AssertEquals("", images[1].Title)
  205. buff := &bytes.Buffer{}
  206. l, err := images[0].Download(buff)
  207. ut.AssertNil(err)
  208. ut.AssertGreaterThan(0, buff.Len())
  209. ut.AssertEquals(int(l), buff.Len())
  210. }
  211. func TestStylesheets(t *testing.T) {
  212. ut.Run(t)
  213. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  214. fmt.Fprint(w, htmlPage1)
  215. }))
  216. defer ts.Close()
  217. bow := NewBrowser()
  218. err := bow.Open(ts.URL)
  219. ut.AssertNil(err)
  220. stylesheets := bow.Stylesheets()
  221. ut.AssertEquals(2, len(stylesheets))
  222. ut.AssertEquals("http://godoc.org/-/site.css", stylesheets[0].URL.String())
  223. ut.AssertEquals("all", stylesheets[0].Media)
  224. ut.AssertEquals("text/css", stylesheets[0].Type)
  225. ut.AssertEquals(ts.URL+"/print.css", stylesheets[1].URL.String())
  226. ut.AssertEquals("print", stylesheets[1].Media)
  227. ut.AssertEquals("text/css", stylesheets[1].Type)
  228. buff := &bytes.Buffer{}
  229. l, err := stylesheets[0].Download(buff)
  230. ut.AssertNil(err)
  231. ut.AssertGreaterThan(0, buff.Len())
  232. ut.AssertEquals(int(l), buff.Len())
  233. }
  234. func TestScripts(t *testing.T) {
  235. ut.Run(t)
  236. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  237. fmt.Fprint(w, htmlPage1)
  238. }))
  239. defer ts.Close()
  240. bow := NewBrowser()
  241. err := bow.Open(ts.URL)
  242. ut.AssertNil(err)
  243. scripts := bow.Scripts()
  244. ut.AssertEquals(2, len(scripts))
  245. ut.AssertEquals("http://godoc.org/-/site.js", scripts[0].URL.String())
  246. ut.AssertEquals("text/javascript", scripts[0].Type)
  247. ut.AssertEquals(ts.URL+"/jquery.min.js", scripts[1].URL.String())
  248. ut.AssertEquals("text/javascript", scripts[1].Type)
  249. buff := &bytes.Buffer{}
  250. l, err := scripts[0].Download(buff)
  251. ut.AssertNil(err)
  252. ut.AssertGreaterThan(0, buff.Len())
  253. ut.AssertEquals(int(l), buff.Len())
  254. }
  255. var htmlPage1 = `<!doctype html>
  256. <html>
  257. <head>
  258. <title>Surf Page 1</title>
  259. <link href="/favicon.ico" rel="icon" type="image/x-icon">
  260. <link href="http://godoc.org/-/site.css" media="all" rel="stylesheet" type="text/css" />
  261. <link href="/print.css" rel="stylesheet" media="print" />
  262. </head>
  263. <body>
  264. <p>Hello, Surf!</p>
  265. <img src="http://i.imgur.com/HW4bJtY.jpg" id="imgur-image" title="It's a..." />
  266. <img src="/Cxagv.jpg" alt="A picture" />
  267. <p>Click the link below.</p>
  268. <a href="/page2">click</a>
  269. <a href="/page3" id="page3">no clicking</a>
  270. <script src="http://godoc.org/-/site.js" type="text/javascript"></script>
  271. <script src="/jquery.min.js" type="text/javascript"></script>
  272. <script type="text/javascript">
  273. var _gaq = _gaq || [];
  274. </script>
  275. </body>
  276. </html>
  277. `
  278. var htmlPage2 = `<!doctype html>
  279. <html>
  280. <head>
  281. <title>Surf Page 2</title>
  282. </head>
  283. <body>
  284. <p>Hello, Surf!</p>
  285. </body>
  286. </html>
  287. `