|
| 1 | +/* |
| 2 | +Copyright The ORAS 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 | +
|
| 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 | + |
| 16 | +package remote |
| 17 | + |
| 18 | +import ( |
| 19 | + "net/http" |
| 20 | + "net/http/httptest" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/oras-project/oras-mcp/internal/version" |
| 24 | + "oras.land/oras-go/v2/registry/remote/auth" |
| 25 | +) |
| 26 | + |
| 27 | +// TestIsPlainHttp tests the isPlainHttp function. |
| 28 | +func TestIsPlainHttp(t *testing.T) { |
| 29 | + tests := []struct { |
| 30 | + name string |
| 31 | + registry string |
| 32 | + want bool |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "localhost", |
| 36 | + registry: "localhost", |
| 37 | + want: true, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "localhost with port", |
| 41 | + registry: "localhost:5000", |
| 42 | + want: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "non-localhost registry", |
| 46 | + registry: "example.com", |
| 47 | + want: false, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "non-localhost registry with port", |
| 51 | + registry: "example.com:5000", |
| 52 | + want: false, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "IP with port", |
| 56 | + registry: "192.168.1.1:5000", |
| 57 | + want: false, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + got := isPlainHttp(tt.registry) |
| 64 | + if got != tt.want { |
| 65 | + t.Errorf("isPlainHttp() = %v, want %v", got, tt.want) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// TestAuthClient tests the authClient function. |
| 72 | +func TestAuthClient(t *testing.T) { |
| 73 | + // Save the original version values to restore after test. |
| 74 | + originalVersion := version.Version |
| 75 | + originalBuildMetadata := version.BuildMetadata |
| 76 | + defer func() { |
| 77 | + version.Version = originalVersion |
| 78 | + version.BuildMetadata = originalBuildMetadata |
| 79 | + }() |
| 80 | + |
| 81 | + // Set test values. |
| 82 | + version.Version = "1.0.0" |
| 83 | + version.BuildMetadata = "test" |
| 84 | + |
| 85 | + // Test creating the auth client. |
| 86 | + client, err := authClient() |
| 87 | + if err != nil { |
| 88 | + t.Fatalf("authClient() error = %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + // Check client properties. |
| 92 | + if client == nil { |
| 93 | + t.Fatal("authClient() returned nil client") |
| 94 | + } |
| 95 | + |
| 96 | + // Check if UserAgent is set correctly. |
| 97 | + expectedUserAgent := "oras-mcp/1.0.0+test" |
| 98 | + userAgentSet := false |
| 99 | + |
| 100 | + // Since we can't directly access the client's user-agent, let's make a test request. |
| 101 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 102 | + if r.Header.Get("User-Agent") == expectedUserAgent { |
| 103 | + userAgentSet = true |
| 104 | + } |
| 105 | + w.WriteHeader(http.StatusOK) |
| 106 | + })) |
| 107 | + defer ts.Close() |
| 108 | + |
| 109 | + req, err := http.NewRequest(http.MethodGet, ts.URL, nil) |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("failed to create request: %v", err) |
| 112 | + } |
| 113 | + _, err = client.Do(req) |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("Failed to make test request: %v", err) |
| 116 | + } |
| 117 | + |
| 118 | + if !userAgentSet { |
| 119 | + t.Errorf("Expected User-Agent header to be set to %s", expectedUserAgent) |
| 120 | + } |
| 121 | + |
| 122 | + // Check if cache is initialized. |
| 123 | + if client.Cache == nil { |
| 124 | + t.Error("Expected Cache to be initialized") |
| 125 | + } |
| 126 | + |
| 127 | + // Check if credential store is set. |
| 128 | + if client.Credential == nil { |
| 129 | + t.Error("Expected Credential function to be set") |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// TestDefaultClient checks that the DefaultClient is properly initialized. |
| 134 | +func TestDefaultClient(t *testing.T) { |
| 135 | + if DefaultClient == nil { |
| 136 | + t.Fatal("DefaultClient should not be nil") |
| 137 | + } |
| 138 | + |
| 139 | + // Check that it's an auth.Client. |
| 140 | + if _, ok := interface{}(DefaultClient).(*auth.Client); !ok { |
| 141 | + t.Errorf("DefaultClient is not of type *auth.Client") |
| 142 | + } |
| 143 | + |
| 144 | + // Test basic functionality of the client. |
| 145 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 146 | + w.WriteHeader(http.StatusOK) |
| 147 | + })) |
| 148 | + defer ts.Close() |
| 149 | + |
| 150 | + req, err := http.NewRequest(http.MethodGet, ts.URL, nil) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("failed to create request: %v", err) |
| 153 | + } |
| 154 | + resp, err := DefaultClient.Do(req) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("Failed to use DefaultClient: %v", err) |
| 157 | + } |
| 158 | + defer resp.Body.Close() |
| 159 | + |
| 160 | + if resp.StatusCode != http.StatusOK { |
| 161 | + t.Errorf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode) |
| 162 | + } |
| 163 | +} |
0 commit comments