proxy_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package reverseproxy
  2. import (
  3. "net/url"
  4. "testing"
  5. )
  6. func TestMatchDomain(t *testing.T) {
  7. tests := []struct {
  8. pattern string
  9. host string
  10. matched bool
  11. }{
  12. {"example.com", "example.com", true},
  13. {"example.com", "example.com:80", true},
  14. {"example.com", "example.com:443", true},
  15. {"example.com", "sub.example.com", false},
  16. {"*.example.com", "sub.example.com", true},
  17. {"*.example.com", "api.sub.example.com", true},
  18. {"*.example.com", "example.com", true},
  19. {"*.example.com", "other.org", false},
  20. {"*", "anything.local", true},
  21. {"*", "192.168.1.1:8080", true},
  22. {"", "anything.local", true},
  23. {"192.168.1.100", "192.168.1.100", true},
  24. {"192.168.1.100", "192.168.1.100:80", true},
  25. {"192.168.1.100", "192.168.1.101", false},
  26. }
  27. for _, tt := range tests {
  28. got := MatchDomain(tt.pattern, tt.host)
  29. if got != tt.matched {
  30. t.Errorf("MatchDomain(%q, %q) = %v; want %v", tt.pattern, tt.host, got, tt.matched)
  31. }
  32. }
  33. }
  34. func TestMatchPath(t *testing.T) {
  35. tests := []struct {
  36. rulePath string
  37. reqPath string
  38. matched bool
  39. }{
  40. {"/", "/", true},
  41. {"/", "/app1", true},
  42. {"/", "/app1/module1", true},
  43. {"/app1", "/app1", true},
  44. {"/app1", "/app1/", true},
  45. {"/app1", "/app1/module1", true},
  46. {"/app1", "/app1/module1/attribute1", true},
  47. {"/app1", "/app100", false},
  48. {"/app1", "/other", false},
  49. {"app1", "/app1", true},
  50. {"/app1/", "/app1/module1", true},
  51. }
  52. for _, tt := range tests {
  53. got := MatchPath(tt.rulePath, tt.reqPath)
  54. if got != tt.matched {
  55. t.Errorf("MatchPath(%q, %q) = %v; want %v", tt.rulePath, tt.reqPath, got, tt.matched)
  56. }
  57. }
  58. }
  59. func TestBuildTargetURL(t *testing.T) {
  60. tests := []struct {
  61. name string
  62. targetRaw string
  63. rulePath string
  64. reqPath string
  65. rawQuery string
  66. stripPath bool
  67. expected string
  68. }{
  69. {
  70. name: "Root target preserving full path",
  71. targetRaw: "http://example.com",
  72. rulePath: "/",
  73. reqPath: "/test/item",
  74. rawQuery: "a=1",
  75. stripPath: false,
  76. expected: "http://example.com/test/item?a=1",
  77. },
  78. {
  79. name: "Target with path, forwarding subfolders",
  80. targetRaw: "http://example.com/app1",
  81. rulePath: "/app1",
  82. reqPath: "/app1/module1/attribute1",
  83. rawQuery: "filter=all",
  84. stripPath: false,
  85. expected: "http://example.com/app1/module1/attribute1?filter=all",
  86. },
  87. {
  88. name: "Target without path, stripPath true",
  89. targetRaw: "http://10.0.0.5:8080",
  90. rulePath: "/app1",
  91. reqPath: "/app1/module1/attribute1",
  92. rawQuery: "",
  93. stripPath: true,
  94. expected: "http://10.0.0.5:8080/module1/attribute1",
  95. },
  96. {
  97. name: "Target without path, stripPath false",
  98. targetRaw: "http://10.0.0.5:8080",
  99. rulePath: "/app1",
  100. reqPath: "/app1/module1/attribute1",
  101. rawQuery: "",
  102. stripPath: false,
  103. expected: "http://10.0.0.5:8080/app1/module1/attribute1",
  104. },
  105. {
  106. name: "Target with trailing slash, req root",
  107. targetRaw: "http://example.com/app1/",
  108. rulePath: "/app1",
  109. reqPath: "/app1",
  110. rawQuery: "",
  111. stripPath: false,
  112. expected: "http://example.com/app1/",
  113. },
  114. {
  115. name: "Merging backend query and incoming query",
  116. targetRaw: "http://example.com/api?v=2",
  117. rulePath: "/api",
  118. reqPath: "/api/users",
  119. rawQuery: "page=1",
  120. stripPath: false,
  121. expected: "http://example.com/api/users?v=2&page=1",
  122. },
  123. }
  124. for _, tt := range tests {
  125. t.Run(tt.name, func(t *testing.T) {
  126. targetBase, err := url.Parse(tt.targetRaw)
  127. if err != nil {
  128. t.Fatalf("failed to parse target URL: %v", err)
  129. }
  130. dest := BuildTargetURL(targetBase, tt.rulePath, tt.reqPath, tt.rawQuery, tt.stripPath)
  131. if dest.String() != tt.expected {
  132. t.Errorf("BuildTargetURL() = %q; want %q", dest.String(), tt.expected)
  133. }
  134. })
  135. }
  136. }