server_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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("smörgåsbord"))
  18. Expect(response).ToNot(ContainSubstring("<a href"))
  19. })
  20. It("should return HTML text", func() {
  21. response := getPath("/smorgasbord", "html")
  22. Expect(response).To(ContainSubstring(
  23. "<a href=\"/http://localhost:4444/smorgasbord/another.html\">"))
  24. })
  25. It("should return the DOM", func() {
  26. response := getPath("/smorgasbord", "dom")
  27. Expect(response).To(ContainSubstring(
  28. "<div class=\"big_middle\">"))
  29. })
  30. It("should return a background image", func() {
  31. response := getPath("/smorgasbord", "html")
  32. Expect(response).To(ContainSubstring("background-image: url(data:image/jpeg"))
  33. })
  34. It("should block specified domains", func() {
  35. viper.Set(
  36. "http-server.blocked-domains",
  37. []string{"[mail|accounts].google.com", "other"},
  38. )
  39. url := getBrowshServiceBase() + "/mail.google.com"
  40. client := &http.Client{}
  41. request, _ := http.NewRequest("GET", url, nil)
  42. response, _ := client.Do(request)
  43. contents, _ := ioutil.ReadAll(response.Body)
  44. Expect(string(contents)).To(ContainSubstring("Welcome to the Browsh HTML"))
  45. })
  46. It("should block specified user agents", func() {
  47. viper.Set(
  48. "http-server.blocked-user-agents",
  49. []string{"MJ12bot", "other"},
  50. )
  51. url := getBrowshServiceBase() + "/example.com"
  52. client := &http.Client{}
  53. request, _ := http.NewRequest("GET", url, nil)
  54. request.Header.Add("User-Agent", "Blah blah MJ12bot etc")
  55. response, _ := client.Do(request)
  56. Expect(response.StatusCode).To(Equal(403))
  57. })
  58. It("should allow a blocked user agent to see the home page", func() {
  59. viper.Set(
  60. "http-server.blocked-user-agents",
  61. []string{"MJ12bot", "other"},
  62. )
  63. url := getBrowshServiceBase()
  64. client := &http.Client{}
  65. request, _ := http.NewRequest("GET", url, nil)
  66. request.Header.Add("User-Agent", "Blah blah MJ12bot etc")
  67. response, _ := client.Do(request)
  68. Expect(response.StatusCode).To(Equal(200))
  69. })
  70. })