|
| 1 | +// Copyright 2019 The Kubernetes Authors. |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package download |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "io" |
| 20 | + "net/http" |
| 21 | + "net/http/httptest" |
| 22 | + "reflect" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +func TestHTTPFetcher_Get(t *testing.T) { |
| 29 | + type args struct { |
| 30 | + uri string |
| 31 | + } |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + h HTTPFetcher |
| 35 | + args args |
| 36 | + want io.ReadCloser |
| 37 | + wantErr bool |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "200", |
| 41 | + h: HTTPFetcher{}, |
| 42 | + args: args{ |
| 43 | + uri: "content:foo", |
| 44 | + }, |
| 45 | + want: io.NopCloser(bytes.NewBufferString("foo")), |
| 46 | + wantErr: false, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "404", |
| 50 | + h: HTTPFetcher{}, |
| 51 | + args: args{ |
| 52 | + uri: "404", |
| 53 | + }, |
| 54 | + want: nil, |
| 55 | + wantErr: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "401", |
| 59 | + h: HTTPFetcher{}, |
| 60 | + args: args{ |
| 61 | + uri: "401", |
| 62 | + }, |
| 63 | + want: nil, |
| 64 | + wantErr: true, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 69 | + if r.Method != "GET" { |
| 70 | + t.Fatalf("test server download %v must always use GET, used %s instead", r.URL, r.Method) |
| 71 | + } |
| 72 | + // return everything after colon as content to the client, return 200 |
| 73 | + if strings.HasPrefix(r.URL.Path, "/content:") { |
| 74 | + if len(r.URL.Path) < 10 { |
| 75 | + w.WriteHeader(http.StatusBadRequest) |
| 76 | + t.Fatalf("test server received content directive with no content") |
| 77 | + return |
| 78 | + } |
| 79 | + content := r.URL.Path[9:] |
| 80 | + _, err := w.Write([]byte(content)) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("test server could not write content response: %s", err) |
| 83 | + } |
| 84 | + return |
| 85 | + } |
| 86 | + // assume that any other resource is just asking to return a specific return code |
| 87 | + i, err := strconv.Atoi(r.URL.Path[1:]) |
| 88 | + if err != nil { |
| 89 | + t.Fatalf("test server url received unknown directive: %v", r.URL.Path) |
| 90 | + } |
| 91 | + w.WriteHeader(i) |
| 92 | + })) |
| 93 | + defer ts.Close() |
| 94 | + |
| 95 | + for _, tt := range tests { |
| 96 | + t.Run(tt.name, func(t *testing.T) { |
| 97 | + h := HTTPFetcher{} |
| 98 | + got, err := h.Get(ts.URL + "/" + tt.args.uri) |
| 99 | + if got != nil { |
| 100 | + defer got.Close() |
| 101 | + } |
| 102 | + if (err != nil) != tt.wantErr { |
| 103 | + t.Errorf("HTTPFetcher.Get() error = %v, wantErr %v", err, tt.wantErr) |
| 104 | + return |
| 105 | + } |
| 106 | + if (tt.want != nil) && (got == nil) { |
| 107 | + t.Error("HTTPFetcher.Get() = nil, want not nil") |
| 108 | + return |
| 109 | + } |
| 110 | + if (tt.want == nil) && (got != nil) { |
| 111 | + t.Error("HTTPFetcher.Get() != nil, want nil") |
| 112 | + return |
| 113 | + } |
| 114 | + if got == nil && tt.want == nil { |
| 115 | + return |
| 116 | + } |
| 117 | + gotS, err := io.ReadAll(got) |
| 118 | + if err != nil { |
| 119 | + t.Errorf("HTTPFetcher.Get() could not read body: %s", err) |
| 120 | + return |
| 121 | + } |
| 122 | + // this is a local buffer, no error will be returned |
| 123 | + wantS, _ := io.ReadAll(tt.want) |
| 124 | + if !reflect.DeepEqual(gotS, wantS) { |
| 125 | + t.Errorf("HTTPFetcher.Get() = %v, want %v", gotS, wantS) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
0 commit comments