server_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package test
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "testing"
  6. . "github.com/onsi/ginkgo"
  7. . "github.com/onsi/gomega"
  8. "github.com/spf13/viper"
  9. )
  10. func TestHTTPServer(t *testing.T) {
  11. RegisterFailHandler(Fail)
  12. RunSpecs(t, "HTTP Server tests")
  13. }
  14. var _ = Describe("HTTP Server", func() {
  15. It("should return plain text", func() {
  16. response := getPath("/smorgasbord", "plain")
  17. Expect(response).To(ContainSubstring("multiple hot Smörgås"))
  18. Expect(response).To(ContainSubstring("A special Swedish type of smörgåsbord"))
  19. Expect(response).ToNot(ContainSubstring("<a href"))
  20. })
  21. It("should return HTML text", func() {
  22. response := getPath("/smorgasbord", "html")
  23. Expect(response).To(ContainSubstring(
  24. "<a href=\"/http://localhost:4444/smorgasbord/another.html\">Another page</a>"))
  25. })
  26. It("should return the DOM", func() {
  27. response := getPath("/smorgasbord", "dom")
  28. Expect(response).To(ContainSubstring(
  29. "<div class=\"big_middle\">"))
  30. })
  31. It("should return a background image", func() {
  32. response := getPath("/smorgasbord", "html")
  33. Expect(response).To(ContainSubstring("background-image: url(data:image/jpeg"))
  34. })
  35. It("should block specified domains", func() {
  36. viper.Set(
  37. "http-server.blocked-domains",
  38. []string{"[mail|accounts].google.com", "other"},
  39. )
  40. url := getBrowshServiceBase() + "/mail.google.com"
  41. client := &http.Client{}
  42. request, _ := http.NewRequest("GET", url, nil)
  43. response, _ := client.Do(request)
  44. contents, _ := ioutil.ReadAll(response.Body)
  45. Expect(string(contents)).To(ContainSubstring("Welcome to the Browsh HTML"))
  46. })
  47. It("should block specified user agents", func() {
  48. viper.Set(
  49. "http-server.blocked-user-agents",
  50. []string{"MJ12bot", "other"},
  51. )
  52. url := getBrowshServiceBase() + "/example.com"
  53. client := &http.Client{}
  54. request, _ := http.NewRequest("GET", url, nil)
  55. request.Header.Add("User-Agent", "Blah blah MJ12bot etc")
  56. response, _ := client.Do(request)
  57. Expect(response.StatusCode).To(Equal(403))
  58. })
  59. It("should allow a blocked user agent to see the home page", func() {
  60. viper.Set(
  61. "http-server.blocked-user-agents",
  62. []string{"MJ12bot", "other"},
  63. )
  64. url := getBrowshServiceBase()
  65. client := &http.Client{}
  66. request, _ := http.NewRequest("GET", url, nil)
  67. request.Header.Add("User-Agent", "Blah blah MJ12bot etc")
  68. response, _ := client.Do(request)
  69. Expect(response.StatusCode).To(Equal(200))
  70. })
  71. })