util_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package httputil
  10. import (
  11. "bytes"
  12. "fmt"
  13. "io/ioutil"
  14. "net/http"
  15. "os"
  16. "testing"
  17. )
  18. const InvalidFileName = "**" + string(0x0)
  19. /*
  20. dummyResponse is a dummy object for http response testing
  21. */
  22. type dummyResponse struct {
  23. out *bytes.Buffer
  24. header map[string][]string
  25. }
  26. func (dr *dummyResponse) Header() http.Header {
  27. return dr.header
  28. }
  29. func (dr *dummyResponse) Write(b []byte) (int, error) {
  30. return dr.out.Write(b)
  31. }
  32. func (dr *dummyResponse) WriteHeader(int) {
  33. }
  34. func TestCheckLocalRedirect(t *testing.T) {
  35. // Check local redirects
  36. if err := CheckLocalRedirect("/foo/bar"); err != nil {
  37. t.Error(err)
  38. return
  39. }
  40. if err := CheckLocalRedirect("foo/bar"); err != nil {
  41. t.Error(err)
  42. return
  43. }
  44. if err := CheckLocalRedirect("x"); err != nil {
  45. t.Error(err)
  46. return
  47. }
  48. // Check absolute redirects
  49. if err := CheckLocalRedirect("http://hans.foo/bla"); err == nil || err.Error() != "Redirection URL must not be an absolute URL" {
  50. t.Error(err)
  51. return
  52. }
  53. if err := CheckLocalRedirect("file://hans.foo/bla"); err == nil || err.Error() != "Redirection URL must not be an absolute URL" {
  54. t.Error(err)
  55. return
  56. }
  57. if err := CheckLocalRedirect("://hans.foo/bla"); err == nil || err.Error() != "parse ://hans.foo/bla: missing protocol scheme" {
  58. t.Error(err)
  59. return
  60. }
  61. if err := CheckLocalRedirect("https:www.foo.co.uk"); err == nil || err.Error() != "Redirection URL must not be an absolute URL" {
  62. t.Error(err)
  63. return
  64. }
  65. if err := CheckLocalRedirect("https:3627733859"); err == nil || err.Error() != "Redirection URL must not be an absolute URL" {
  66. t.Error(err)
  67. return
  68. }
  69. }
  70. func TestSingleFileServer(t *testing.T) {
  71. ioutil.WriteFile("foo.txt", []byte("foo test"), 0666)
  72. defer os.Remove("foo.txt")
  73. sfs := SingleFileServer("foo.txt", nil)
  74. dr := &dummyResponse{&bytes.Buffer{}, make(map[string][]string)}
  75. sfs.ServeHTTP(dr, nil)
  76. if res := fmt.Sprint(dr.header); res != "map[Content-Type:[text/plain; charset=utf-8]]" {
  77. t.Error("Unexpected result:", res)
  78. return
  79. }
  80. if res := fmt.Sprint(dr.out); res != "foo test" {
  81. t.Error("Unexpected result:", res)
  82. return
  83. }
  84. sfs = SingleFileServer(InvalidFileName, nil)
  85. dr = &dummyResponse{&bytes.Buffer{}, make(map[string][]string)}
  86. sfs.ServeHTTP(dr, nil)
  87. if res := fmt.Sprint(dr.header); res != "map[]" {
  88. t.Error("Unexpected result:", res)
  89. return
  90. }
  91. if res := fmt.Sprint(dr.out); res != "Unauthorized\n" {
  92. t.Error("Unexpected result:", res)
  93. return
  94. }
  95. }
  96. func TestRandomFileServer(t *testing.T) {
  97. ioutil.WriteFile("foo.txt", []byte("foo test"), 0666)
  98. defer os.Remove("foo.txt")
  99. rfs := RandomFileServer([]string{"foo.txt", "foo.txt", "foo.txt"}, nil)
  100. dr := &dummyResponse{&bytes.Buffer{}, make(map[string][]string)}
  101. rfs.ServeHTTP(dr, nil)
  102. if res := fmt.Sprint(dr.header); res != "map[Content-Type:[text/plain; charset=utf-8]]" {
  103. t.Error("Unexpected result:", res)
  104. return
  105. }
  106. if res := fmt.Sprint(dr.out); res != "foo test" {
  107. t.Error("Unexpected result:", res)
  108. return
  109. }
  110. }