1
0

headervars_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package rewrite
  2. import (
  3. "net/http/httptest"
  4. "testing"
  5. )
  6. func TestGetHeaderVariableValuesFromRequest(t *testing.T) {
  7. // Create a sample request
  8. req := httptest.NewRequest("GET", "https://example.com/test?foo=bar", nil)
  9. req.Host = "example.com"
  10. req.RemoteAddr = "192.168.1.1:12345"
  11. req.Header.Set("Content-Type", "application/json")
  12. req.Header.Set("User-Agent", "TestAgent")
  13. req.Header.Set("Referer", "https://referer.com")
  14. // Call the function
  15. vars := GetHeaderVariableValuesFromRequest(req)
  16. // Expected results
  17. expected := map[string]string{
  18. "$host": "example.com",
  19. "$remote_addr": "192.168.1.1:12345",
  20. "$request_uri": "https://example.com/test?foo=bar",
  21. "$request_method": "GET",
  22. "$content_length": "0", // ContentLength is 0 because there's no body in the request
  23. "$content_type": "application/json",
  24. "$uri": "/test",
  25. "$args": "foo=bar",
  26. "$scheme": "https",
  27. "$query_string": "foo=bar",
  28. "$http_user_agent": "TestAgent",
  29. "$http_referer": "https://referer.com",
  30. }
  31. // Check each expected variable
  32. for key, expectedValue := range expected {
  33. if vars[key] != expectedValue {
  34. t.Errorf("Expected %s to be %s, but got %s", key, expectedValue, vars[key])
  35. }
  36. }
  37. }
  38. func TestCustomHeadersIncludeDynamicVariables(t *testing.T) {
  39. tests := []struct {
  40. name string
  41. headers []*UserDefinedHeader
  42. expectedHasVar bool
  43. }{
  44. {
  45. name: "No headers",
  46. headers: []*UserDefinedHeader{},
  47. expectedHasVar: false,
  48. },
  49. {
  50. name: "Headers without dynamic variables",
  51. headers: []*UserDefinedHeader{
  52. {
  53. Direction: HeaderDirection_ZoraxyToUpstream,
  54. Key: "X-Custom-Header",
  55. Value: "staticValue",
  56. IsRemove: false,
  57. },
  58. {
  59. Direction: HeaderDirection_ZoraxyToDownstream,
  60. Key: "X-Another-Header",
  61. Value: "staticValue",
  62. IsRemove: false,
  63. },
  64. },
  65. expectedHasVar: false,
  66. },
  67. {
  68. name: "Headers with one dynamic variable",
  69. headers: []*UserDefinedHeader{
  70. {
  71. Direction: HeaderDirection_ZoraxyToUpstream,
  72. Key: "X-Custom-Header",
  73. Value: "$dynamicValue",
  74. IsRemove: false,
  75. },
  76. },
  77. expectedHasVar: true,
  78. },
  79. {
  80. name: "Headers with multiple dynamic variables",
  81. headers: []*UserDefinedHeader{
  82. {
  83. Direction: HeaderDirection_ZoraxyToUpstream,
  84. Key: "X-Custom-Header",
  85. Value: "$dynamicValue1",
  86. IsRemove: false,
  87. },
  88. {
  89. Direction: HeaderDirection_ZoraxyToDownstream,
  90. Key: "X-Another-Header",
  91. Value: "$dynamicValue2",
  92. IsRemove: false,
  93. },
  94. },
  95. expectedHasVar: true,
  96. },
  97. }
  98. for _, tt := range tests {
  99. t.Run(tt.name, func(t *testing.T) {
  100. hasVar := CustomHeadersIncludeDynamicVariables(tt.headers)
  101. if hasVar != tt.expectedHasVar {
  102. t.Errorf("Expected %v, but got %v", tt.expectedHasVar, hasVar)
  103. }
  104. })
  105. }
  106. }
  107. func TestPopulateRequestHeaderVariables(t *testing.T) {
  108. // Create a sample request with specific values
  109. req := httptest.NewRequest("GET", "https://example.com/test?foo=bar", nil)
  110. req.Host = "example.com"
  111. req.RemoteAddr = "192.168.1.1:12345"
  112. req.Header.Set("User-Agent", "TestAgent")
  113. req.Header.Set("Referer", "https://referer.com")
  114. // Define user-defined headers with dynamic variables
  115. userDefinedHeaders := []*UserDefinedHeader{
  116. {
  117. Direction: HeaderDirection_ZoraxyToUpstream,
  118. Key: "X-Forwarded-Host",
  119. Value: "$host",
  120. },
  121. {
  122. Direction: HeaderDirection_ZoraxyToDownstream,
  123. Key: "X-Client-IP",
  124. Value: "$remote_addr",
  125. },
  126. {
  127. Direction: HeaderDirection_ZoraxyToDownstream,
  128. Key: "X-Custom-Header",
  129. Value: "$request_uri",
  130. },
  131. }
  132. // Call the function with the test data
  133. resultHeaders := PopulateRequestHeaderVariables(req, userDefinedHeaders)
  134. // Expected results after variable substitution
  135. expectedHeaders := []*UserDefinedHeader{
  136. {
  137. Direction: HeaderDirection_ZoraxyToUpstream,
  138. Key: "X-Forwarded-Host",
  139. Value: "example.com",
  140. },
  141. {
  142. Direction: HeaderDirection_ZoraxyToDownstream,
  143. Key: "X-Client-IP",
  144. Value: "192.168.1.1:12345",
  145. },
  146. {
  147. Direction: HeaderDirection_ZoraxyToDownstream,
  148. Key: "X-Custom-Header",
  149. Value: "https://example.com/test?foo=bar",
  150. },
  151. }
  152. // Validate results
  153. for i, expected := range expectedHeaders {
  154. if resultHeaders[i].Direction != expected.Direction ||
  155. resultHeaders[i].Key != expected.Key ||
  156. resultHeaders[i].Value != expected.Value {
  157. t.Errorf("Expected header %v, but got %v", expected, resultHeaders[i])
  158. }
  159. }
  160. }