transport_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package proxy
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "net/http"
  18. "net/http/httptest"
  19. "net/url"
  20. "strings"
  21. "testing"
  22. )
  23. func parseURLOrDie(inURL string) *url.URL {
  24. parsed, err := url.Parse(inURL)
  25. if err != nil {
  26. panic(err)
  27. }
  28. return parsed
  29. }
  30. func TestProxyTransport(t *testing.T) {
  31. testTransport := &Transport{
  32. Scheme: "http",
  33. Host: "foo.com",
  34. PathPrepend: "/proxy/minion/minion1:10250",
  35. }
  36. testTransport2 := &Transport{
  37. Scheme: "https",
  38. Host: "foo.com",
  39. PathPrepend: "/proxy/minion/minion1:8080",
  40. }
  41. emptyHostTransport := &Transport{
  42. Scheme: "https",
  43. PathPrepend: "/proxy/minion/minion1:10250",
  44. }
  45. emptySchemeTransport := &Transport{
  46. Host: "foo.com",
  47. PathPrepend: "/proxy/minion/minion1:10250",
  48. }
  49. type Item struct {
  50. input string
  51. sourceURL string
  52. transport *Transport
  53. output string
  54. contentType string
  55. forwardedURI string
  56. redirect string
  57. redirectWant string
  58. }
  59. table := map[string]Item{
  60. "normal": {
  61. input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
  62. sourceURL: "http://myminion.com/logs/log.log",
  63. transport: testTransport,
  64. output: `<pre><a href="kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
  65. contentType: "text/html",
  66. forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
  67. },
  68. "full document": {
  69. input: `<html><header></header><body><pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre></body></html>`,
  70. sourceURL: "http://myminion.com/logs/log.log",
  71. transport: testTransport,
  72. output: `<html><header></header><body><pre><a href="kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre></body></html>`,
  73. contentType: "text/html",
  74. forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
  75. },
  76. "trailing slash": {
  77. input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log/">google.log</a></pre>`,
  78. sourceURL: "http://myminion.com/logs/log.log",
  79. transport: testTransport,
  80. output: `<pre><a href="kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log/">google.log</a></pre>`,
  81. contentType: "text/html",
  82. forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
  83. },
  84. "content-type charset": {
  85. input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
  86. sourceURL: "http://myminion.com/logs/log.log",
  87. transport: testTransport,
  88. output: `<pre><a href="kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
  89. contentType: "text/html; charset=utf-8",
  90. forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
  91. },
  92. "content-type passthrough": {
  93. input: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
  94. sourceURL: "http://myminion.com/logs/log.log",
  95. transport: testTransport,
  96. output: `<pre><a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a></pre>`,
  97. contentType: "text/plain",
  98. forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
  99. },
  100. "subdir": {
  101. input: `<a href="kubelet.log">kubelet.log</a><a href="/google.log">google.log</a>`,
  102. sourceURL: "http://myminion.com/whatever/apt/somelog.log",
  103. transport: testTransport2,
  104. output: `<a href="kubelet.log">kubelet.log</a><a href="https://foo.com/proxy/minion/minion1:8080/google.log">google.log</a>`,
  105. contentType: "text/html",
  106. forwardedURI: "/proxy/minion/minion1:8080/whatever/apt/somelog.log",
  107. },
  108. "image": {
  109. input: `<pre><img src="kubernetes.jpg"/><img src="/kubernetes_abs.jpg"/></pre>`,
  110. sourceURL: "http://myminion.com/",
  111. transport: testTransport,
  112. output: `<pre><img src="kubernetes.jpg"/><img src="http://foo.com/proxy/minion/minion1:10250/kubernetes_abs.jpg"/></pre>`,
  113. contentType: "text/html",
  114. forwardedURI: "/proxy/minion/minion1:10250/",
  115. },
  116. "abs": {
  117. input: `<script src="http://google.com/kubernetes.js"/>`,
  118. sourceURL: "http://myminion.com/any/path/",
  119. transport: testTransport,
  120. output: `<script src="http://google.com/kubernetes.js"/>`,
  121. contentType: "text/html",
  122. forwardedURI: "/proxy/minion/minion1:10250/any/path/",
  123. },
  124. "abs but same host": {
  125. input: `<script src="http://myminion.com/kubernetes.js"/>`,
  126. sourceURL: "http://myminion.com/any/path/",
  127. transport: testTransport,
  128. output: `<script src="http://foo.com/proxy/minion/minion1:10250/kubernetes.js"/>`,
  129. contentType: "text/html",
  130. forwardedURI: "/proxy/minion/minion1:10250/any/path/",
  131. },
  132. "redirect rel": {
  133. sourceURL: "http://myminion.com/redirect",
  134. transport: testTransport,
  135. redirect: "/redirected/target/",
  136. redirectWant: "http://foo.com/proxy/minion/minion1:10250/redirected/target/",
  137. forwardedURI: "/proxy/minion/minion1:10250/redirect",
  138. },
  139. "redirect abs same host": {
  140. sourceURL: "http://myminion.com/redirect",
  141. transport: testTransport,
  142. redirect: "http://myminion.com/redirected/target/",
  143. redirectWant: "http://foo.com/proxy/minion/minion1:10250/redirected/target/",
  144. forwardedURI: "/proxy/minion/minion1:10250/redirect",
  145. },
  146. "redirect abs other host": {
  147. sourceURL: "http://myminion.com/redirect",
  148. transport: testTransport,
  149. redirect: "http://example.com/redirected/target/",
  150. redirectWant: "http://example.com/redirected/target/",
  151. forwardedURI: "/proxy/minion/minion1:10250/redirect",
  152. },
  153. "source contains the redirect already": {
  154. input: `<pre><a href="kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
  155. sourceURL: "http://foo.com/logs/log.log",
  156. transport: testTransport,
  157. output: `<pre><a href="kubelet.log">kubelet.log</a><a href="http://foo.com/proxy/minion/minion1:10250/google.log">google.log</a></pre>`,
  158. contentType: "text/html",
  159. forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
  160. },
  161. "no host": {
  162. input: "<html></html>",
  163. sourceURL: "http://myminion.com/logs/log.log",
  164. transport: emptyHostTransport,
  165. output: "<html></html>",
  166. contentType: "text/html",
  167. forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
  168. },
  169. "no scheme": {
  170. input: "<html></html>",
  171. sourceURL: "http://myminion.com/logs/log.log",
  172. transport: emptySchemeTransport,
  173. output: "<html></html>",
  174. contentType: "text/html",
  175. forwardedURI: "/proxy/minion/minion1:10250/logs/log.log",
  176. },
  177. }
  178. testItem := func(name string, item *Item) {
  179. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  180. // Check request headers.
  181. if got, want := r.Header.Get("X-Forwarded-Uri"), item.forwardedURI; got != want {
  182. t.Errorf("%v: X-Forwarded-Uri = %q, want %q", name, got, want)
  183. }
  184. if len(item.transport.Host) == 0 {
  185. _, present := r.Header["X-Forwarded-Host"]
  186. if present {
  187. t.Errorf("%v: X-Forwarded-Host header should not be present", name)
  188. }
  189. } else {
  190. if got, want := r.Header.Get("X-Forwarded-Host"), item.transport.Host; got != want {
  191. t.Errorf("%v: X-Forwarded-Host = %q, want %q", name, got, want)
  192. }
  193. }
  194. if len(item.transport.Scheme) == 0 {
  195. _, present := r.Header["X-Forwarded-Proto"]
  196. if present {
  197. t.Errorf("%v: X-Forwarded-Proto header should not be present", name)
  198. }
  199. } else {
  200. if got, want := r.Header.Get("X-Forwarded-Proto"), item.transport.Scheme; got != want {
  201. t.Errorf("%v: X-Forwarded-Proto = %q, want %q", name, got, want)
  202. }
  203. }
  204. // Send response.
  205. if item.redirect != "" {
  206. http.Redirect(w, r, item.redirect, http.StatusMovedPermanently)
  207. return
  208. }
  209. w.Header().Set("Content-Type", item.contentType)
  210. fmt.Fprint(w, item.input)
  211. }))
  212. defer server.Close()
  213. // Replace source URL with our test server address.
  214. sourceURL := parseURLOrDie(item.sourceURL)
  215. serverURL := parseURLOrDie(server.URL)
  216. item.input = strings.Replace(item.input, sourceURL.Host, serverURL.Host, -1)
  217. item.redirect = strings.Replace(item.redirect, sourceURL.Host, serverURL.Host, -1)
  218. sourceURL.Host = serverURL.Host
  219. req, err := http.NewRequest("GET", sourceURL.String(), nil)
  220. if err != nil {
  221. t.Errorf("%v: Unexpected error: %v", name, err)
  222. return
  223. }
  224. resp, err := item.transport.RoundTrip(req)
  225. if err != nil {
  226. t.Errorf("%v: Unexpected error: %v", name, err)
  227. return
  228. }
  229. if item.redirect != "" {
  230. // Check that redirect URLs get rewritten properly.
  231. if got, want := resp.Header.Get("Location"), item.redirectWant; got != want {
  232. t.Errorf("%v: Location header = %q, want %q", name, got, want)
  233. }
  234. return
  235. }
  236. body, err := ioutil.ReadAll(resp.Body)
  237. if err != nil {
  238. t.Errorf("%v: Unexpected error: %v", name, err)
  239. return
  240. }
  241. if e, a := item.output, string(body); e != a {
  242. t.Errorf("%v: expected %v, but got %v", name, e, a)
  243. }
  244. }
  245. for name, item := range table {
  246. testItem(name, &item)
  247. }
  248. }