|
| 1 | +/* |
| 2 | +Copyright 2017 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package transport |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "io/ioutil" |
| 22 | + "net/http" |
| 23 | + "net/url" |
| 24 | + "os" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +// copied from k8s.io/client-go/transport/round_trippers_test.go |
| 29 | +type testRoundTripper struct { |
| 30 | + Request *http.Request |
| 31 | + Response *http.Response |
| 32 | + Err error |
| 33 | +} |
| 34 | + |
| 35 | +func (rt *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { |
| 36 | + rt.Request = req |
| 37 | + return rt.Response, rt.Err |
| 38 | +} |
| 39 | + |
| 40 | +func TestCacheRoundTripper(t *testing.T) { |
| 41 | + rt := &testRoundTripper{} |
| 42 | + cacheDir, err := ioutil.TempDir("", "cache-rt") |
| 43 | + defer os.RemoveAll(cacheDir) |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + cache := NewCacheRoundTripper(cacheDir, rt) |
| 48 | + |
| 49 | + // First call, caches the response |
| 50 | + req := &http.Request{ |
| 51 | + Method: http.MethodGet, |
| 52 | + URL: &url.URL{Host: "localhost"}, |
| 53 | + } |
| 54 | + rt.Response = &http.Response{ |
| 55 | + Header: http.Header{"ETag": []string{`"123456"`}}, |
| 56 | + Body: ioutil.NopCloser(bytes.NewReader([]byte("Content"))), |
| 57 | + StatusCode: http.StatusOK, |
| 58 | + } |
| 59 | + resp, err := cache.RoundTrip(req) |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + content, err := ioutil.ReadAll(resp.Body) |
| 64 | + if err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + if string(content) != "Content" { |
| 68 | + t.Errorf(`Expected Body to be "Content", got %q`, string(content)) |
| 69 | + } |
| 70 | + |
| 71 | + // Second call, returns cached response |
| 72 | + req = &http.Request{ |
| 73 | + Method: http.MethodGet, |
| 74 | + URL: &url.URL{Host: "localhost"}, |
| 75 | + } |
| 76 | + rt.Response = &http.Response{ |
| 77 | + StatusCode: http.StatusNotModified, |
| 78 | + Body: ioutil.NopCloser(bytes.NewReader([]byte("Other Content"))), |
| 79 | + } |
| 80 | + |
| 81 | + resp, err = cache.RoundTrip(req) |
| 82 | + if err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + |
| 86 | + // Read body and make sure we have the initial content |
| 87 | + content, err = ioutil.ReadAll(resp.Body) |
| 88 | + resp.Body.Close() |
| 89 | + if err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + if string(content) != "Content" { |
| 93 | + t.Errorf("Invalid content read from cache %q", string(content)) |
| 94 | + } |
| 95 | +} |
0 commit comments