|
1 | 1 | package main |
2 | 2 |
|
3 | | -import "testing" |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/go-resty/resty/v2" |
| 10 | + "github.com/jarcoal/httpmock" |
| 11 | +) |
4 | 12 |
|
5 | 13 | func Test_validateRemotes(t *testing.T) { |
6 | 14 |
|
@@ -35,3 +43,78 @@ func Test_validateRemotes(t *testing.T) { |
35 | 43 | }) |
36 | 44 | } |
37 | 45 | } |
| 46 | + |
| 47 | +func Test_getURL(t *testing.T) { |
| 48 | + tests := []struct { |
| 49 | + name string |
| 50 | + requestLocation string |
| 51 | + responseStatusCode int |
| 52 | + redirectLocation string |
| 53 | + want string |
| 54 | + }{ |
| 55 | + { |
| 56 | + name: "valid location", |
| 57 | + requestLocation: "https://service.us-east-1.example.com", |
| 58 | + responseStatusCode: http.StatusOK, |
| 59 | + redirectLocation: "", |
| 60 | + want: "https://service.us-east-1.example.com", |
| 61 | + }, |
| 62 | + |
| 63 | + { |
| 64 | + name: "valid location - no scheme", |
| 65 | + requestLocation: "service.us-east-1.example.com", |
| 66 | + responseStatusCode: http.StatusOK, |
| 67 | + redirectLocation: "", |
| 68 | + want: "http://service.us-east-1.example.com", |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "302 redirect", |
| 72 | + requestLocation: "https://service.us-east-1.example.com", |
| 73 | + responseStatusCode: http.StatusFound, |
| 74 | + redirectLocation: "https://redirected.example.com", |
| 75 | + want: "https://redirected.example.com", |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "302 redirect - no scheme", |
| 79 | + requestLocation: "service.us-east-1.example.com", |
| 80 | + responseStatusCode: http.StatusFound, |
| 81 | + redirectLocation: "https://redirected.example.com", |
| 82 | + want: "https://redirected.example.com", |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + client := resty.New() |
| 89 | + |
| 90 | + // Activate httpmock for this client |
| 91 | + httpmock.ActivateNonDefault(client.GetClient()) |
| 92 | + defer httpmock.DeactivateAndReset() |
| 93 | + mockRequestLocation := tt.requestLocation |
| 94 | + |
| 95 | + if !strings.HasPrefix(tt.requestLocation, "http") { |
| 96 | + mockRequestLocation = "http://" + mockRequestLocation |
| 97 | + } |
| 98 | + |
| 99 | + // Register a mocked response |
| 100 | + httpmock.RegisterResponder("GET", mockRequestLocation, |
| 101 | + func(req *http.Request) (*http.Response, error) { |
| 102 | + resp := httpmock.NewStringResponse(tt.responseStatusCode, "") |
| 103 | + resp.Header.Set("Location", tt.redirectLocation) |
| 104 | + return resp, nil |
| 105 | + }, |
| 106 | + ) |
| 107 | + |
| 108 | + got := fetchURL(client, tt.requestLocation) |
| 109 | + if got != tt.want { |
| 110 | + t.Errorf("getURL() = %v, want %v", got, tt.want) |
| 111 | + } |
| 112 | + if tt.responseStatusCode == http.StatusFound { |
| 113 | + // Check if the redirect location was set correctly |
| 114 | + httpmock.RegisterResponder("GET", mockRequestLocation, |
| 115 | + httpmock.NewStringResponder(tt.responseStatusCode, "Location: "+tt.redirectLocation), |
| 116 | + ) |
| 117 | + } |
| 118 | + }) |
| 119 | + } |
| 120 | +} |
0 commit comments