origin_request_config_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package ingress
  2. import (
  3. "flag"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/require"
  7. "github.com/urfave/cli/v2"
  8. yaml "gopkg.in/yaml.v2"
  9. "github.com/cloudflare/cloudflared/config"
  10. )
  11. // Ensure that the nullable config from `config` package and the
  12. // non-nullable config from `ingress` package have the same number of
  13. // fields.
  14. // This test ensures that programmers didn't add a new field to
  15. // one struct and forget to add it to the other ;)
  16. func TestCorrespondingFields(t *testing.T) {
  17. require.Equal(
  18. t,
  19. CountFields(t, config.OriginRequestConfig{}),
  20. CountFields(t, OriginRequestConfig{}),
  21. )
  22. }
  23. func CountFields(t *testing.T, val interface{}) int {
  24. b, err := yaml.Marshal(val)
  25. require.NoError(t, err)
  26. m := make(map[string]interface{}, 0)
  27. err = yaml.Unmarshal(b, &m)
  28. require.NoError(t, err)
  29. return len(m)
  30. }
  31. func TestOriginRequestConfigOverrides(t *testing.T) {
  32. rulesYAML := `
  33. originRequest:
  34. connectTimeout: 1m
  35. tlsTimeout: 1s
  36. noHappyEyeballs: true
  37. tcpKeepAlive: 1s
  38. keepAliveConnections: 1
  39. keepAliveTimeout: 1s
  40. httpHostHeader: abc
  41. originServerName: a1
  42. caPool: /tmp/path0
  43. noTLSVerify: true
  44. disableChunkedEncoding: true
  45. bastionMode: True
  46. proxyAddress: 127.1.2.3
  47. proxyPort: 100
  48. proxyType: socks5
  49. ingress:
  50. - hostname: tun.example.com
  51. service: https://localhost:8000
  52. - hostname: "*"
  53. service: https://localhost:8001
  54. originRequest:
  55. connectTimeout: 2m
  56. tlsTimeout: 2s
  57. noHappyEyeballs: false
  58. tcpKeepAlive: 2s
  59. keepAliveConnections: 2
  60. keepAliveTimeout: 2s
  61. httpHostHeader: def
  62. originServerName: b2
  63. caPool: /tmp/path1
  64. noTLSVerify: false
  65. disableChunkedEncoding: false
  66. bastionMode: false
  67. proxyAddress: interface
  68. proxyPort: 200
  69. proxyType: ""
  70. `
  71. ing, err := ParseIngress(MustReadIngress(rulesYAML))
  72. if err != nil {
  73. t.Error(err)
  74. }
  75. // Rule 0 didn't override anything, so it inherits the user-specified
  76. // root-level configuration.
  77. actual0 := ing.Rules[0].Config
  78. expected0 := OriginRequestConfig{
  79. ConnectTimeout: 1 * time.Minute,
  80. TLSTimeout: 1 * time.Second,
  81. NoHappyEyeballs: true,
  82. TCPKeepAlive: 1 * time.Second,
  83. KeepAliveConnections: 1,
  84. KeepAliveTimeout: 1 * time.Second,
  85. HTTPHostHeader: "abc",
  86. OriginServerName: "a1",
  87. CAPool: "/tmp/path0",
  88. NoTLSVerify: true,
  89. DisableChunkedEncoding: true,
  90. BastionMode: true,
  91. ProxyAddress: "127.1.2.3",
  92. ProxyPort: uint(100),
  93. ProxyType: "socks5",
  94. }
  95. require.Equal(t, expected0, actual0)
  96. // Rule 1 overrode all the root-level config.
  97. actual1 := ing.Rules[1].Config
  98. expected1 := OriginRequestConfig{
  99. ConnectTimeout: 2 * time.Minute,
  100. TLSTimeout: 2 * time.Second,
  101. NoHappyEyeballs: false,
  102. TCPKeepAlive: 2 * time.Second,
  103. KeepAliveConnections: 2,
  104. KeepAliveTimeout: 2 * time.Second,
  105. HTTPHostHeader: "def",
  106. OriginServerName: "b2",
  107. CAPool: "/tmp/path1",
  108. NoTLSVerify: false,
  109. DisableChunkedEncoding: false,
  110. BastionMode: false,
  111. ProxyAddress: "interface",
  112. ProxyPort: uint(200),
  113. ProxyType: "",
  114. }
  115. require.Equal(t, expected1, actual1)
  116. }
  117. func TestOriginRequestConfigDefaults(t *testing.T) {
  118. rulesYAML := `
  119. ingress:
  120. - hostname: tun.example.com
  121. service: https://localhost:8000
  122. - hostname: "*"
  123. service: https://localhost:8001
  124. originRequest:
  125. connectTimeout: 2m
  126. tlsTimeout: 2s
  127. noHappyEyeballs: false
  128. tcpKeepAlive: 2s
  129. keepAliveConnections: 2
  130. keepAliveTimeout: 2s
  131. httpHostHeader: def
  132. originServerName: b2
  133. caPool: /tmp/path1
  134. noTLSVerify: false
  135. disableChunkedEncoding: false
  136. bastionMode: false
  137. proxyAddress: interface
  138. proxyPort: 200
  139. proxyType: ""
  140. `
  141. ing, err := ParseIngress(MustReadIngress(rulesYAML))
  142. if err != nil {
  143. t.Error(err)
  144. }
  145. // Rule 0 didn't override anything, so it inherits the cloudflared defaults
  146. actual0 := ing.Rules[0].Config
  147. expected0 := OriginRequestConfig{
  148. ConnectTimeout: defaultConnectTimeout,
  149. TLSTimeout: defaultTLSTimeout,
  150. TCPKeepAlive: defaultTCPKeepAlive,
  151. KeepAliveConnections: defaultKeepAliveConnections,
  152. KeepAliveTimeout: defaultKeepAliveTimeout,
  153. ProxyAddress: defaultProxyAddress,
  154. }
  155. require.Equal(t, expected0, actual0)
  156. // Rule 1 overrode all defaults.
  157. actual1 := ing.Rules[1].Config
  158. expected1 := OriginRequestConfig{
  159. ConnectTimeout: 2 * time.Minute,
  160. TLSTimeout: 2 * time.Second,
  161. NoHappyEyeballs: false,
  162. TCPKeepAlive: 2 * time.Second,
  163. KeepAliveConnections: 2,
  164. KeepAliveTimeout: 2 * time.Second,
  165. HTTPHostHeader: "def",
  166. OriginServerName: "b2",
  167. CAPool: "/tmp/path1",
  168. NoTLSVerify: false,
  169. DisableChunkedEncoding: false,
  170. BastionMode: false,
  171. ProxyAddress: "interface",
  172. ProxyPort: uint(200),
  173. ProxyType: "",
  174. }
  175. require.Equal(t, expected1, actual1)
  176. }
  177. func TestDefaultConfigFromCLI(t *testing.T) {
  178. set := flag.NewFlagSet("contrive", 0)
  179. c := cli.NewContext(nil, set, nil)
  180. expected := OriginRequestConfig{
  181. ConnectTimeout: defaultConnectTimeout,
  182. TLSTimeout: defaultTLSTimeout,
  183. TCPKeepAlive: defaultTCPKeepAlive,
  184. KeepAliveConnections: defaultKeepAliveConnections,
  185. KeepAliveTimeout: defaultKeepAliveTimeout,
  186. ProxyAddress: defaultProxyAddress,
  187. }
  188. actual := originRequestFromSingeRule(c)
  189. require.Equal(t, expected, actual)
  190. }