rule_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package ingress
  2. import (
  3. "io"
  4. "net/http/httptest"
  5. "net/url"
  6. "regexp"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func Test_rule_matches(t *testing.T) {
  11. type fields struct {
  12. Hostname string
  13. Path *regexp.Regexp
  14. Service originService
  15. }
  16. type args struct {
  17. requestURL *url.URL
  18. }
  19. tests := []struct {
  20. name string
  21. fields fields
  22. args args
  23. want bool
  24. }{
  25. {
  26. name: "Just hostname, pass",
  27. fields: fields{
  28. Hostname: "example.com",
  29. },
  30. args: args{
  31. requestURL: MustParseURL(t, "https://example.com"),
  32. },
  33. want: true,
  34. },
  35. {
  36. name: "Entire hostname is wildcard, should match everything",
  37. fields: fields{
  38. Hostname: "*",
  39. },
  40. args: args{
  41. requestURL: MustParseURL(t, "https://example.com"),
  42. },
  43. want: true,
  44. },
  45. {
  46. name: "Just hostname, fail",
  47. fields: fields{
  48. Hostname: "example.com",
  49. },
  50. args: args{
  51. requestURL: MustParseURL(t, "https://foo.bar"),
  52. },
  53. want: false,
  54. },
  55. {
  56. name: "Just wildcard hostname, pass",
  57. fields: fields{
  58. Hostname: "*.example.com",
  59. },
  60. args: args{
  61. requestURL: MustParseURL(t, "https://adam.example.com"),
  62. },
  63. want: true,
  64. },
  65. {
  66. name: "Just wildcard hostname, fail",
  67. fields: fields{
  68. Hostname: "*.example.com",
  69. },
  70. args: args{
  71. requestURL: MustParseURL(t, "https://tunnel.com"),
  72. },
  73. want: false,
  74. },
  75. {
  76. name: "Just wildcard outside of subdomain in hostname, fail",
  77. fields: fields{
  78. Hostname: "*example.com",
  79. },
  80. args: args{
  81. requestURL: MustParseURL(t, "https://www.example.com"),
  82. },
  83. want: false,
  84. },
  85. {
  86. name: "Wildcard over multiple subdomains",
  87. fields: fields{
  88. Hostname: "*.example.com",
  89. },
  90. args: args{
  91. requestURL: MustParseURL(t, "https://adam.chalmers.example.com"),
  92. },
  93. want: true,
  94. },
  95. {
  96. name: "Hostname and path",
  97. fields: fields{
  98. Hostname: "*.example.com",
  99. Path: regexp.MustCompile("/static/.*\\.html"),
  100. },
  101. args: args{
  102. requestURL: MustParseURL(t, "https://www.example.com/static/index.html"),
  103. },
  104. want: true,
  105. },
  106. }
  107. for _, tt := range tests {
  108. t.Run(tt.name, func(t *testing.T) {
  109. r := Rule{
  110. Hostname: tt.fields.Hostname,
  111. Path: tt.fields.Path,
  112. Service: tt.fields.Service,
  113. }
  114. u := tt.args.requestURL
  115. if got := r.Matches(u.Hostname(), u.Path); got != tt.want {
  116. t.Errorf("rule.matches() = %v, want %v", got, tt.want)
  117. }
  118. })
  119. }
  120. }
  121. func TestStaticHTTPStatus(t *testing.T) {
  122. o := newStatusCode(404)
  123. buf := make([]byte, 100)
  124. sendReq := func() {
  125. resp, err := o.RoundTrip(nil)
  126. require.NoError(t, err)
  127. _, err = resp.Body.Read(buf)
  128. require.Equal(t, io.EOF, err)
  129. require.NoError(t, resp.Body.Close())
  130. require.Equal(t, 404, resp.StatusCode)
  131. resp, err = o.RoundTrip(nil)
  132. require.NoError(t, err)
  133. w := httptest.NewRecorder()
  134. n, err := io.Copy(w, resp.Body)
  135. require.NoError(t, err)
  136. require.Equal(t, int64(0), n)
  137. }
  138. sendReq()
  139. sendReq()
  140. }