(t *testing.T)
| 109 | } |
| 110 | |
| 111 | func TestUploadAuthentication(t *testing.T) { |
| 112 | tests := []struct { |
| 113 | name string |
| 114 | configuredHost string |
| 115 | configuredToken string |
| 116 | wantRequestHost string |
| 117 | wantAuthorization string |
| 118 | }{ |
| 119 | { |
| 120 | name: "GitHub upload host uses github.com token", |
| 121 | configuredHost: "github.com", |
| 122 | configuredToken: "github-token", |
| 123 | wantRequestHost: "uploads.github.com", |
| 124 | wantAuthorization: "token github-token", |
| 125 | }, |
| 126 | { |
| 127 | name: "tenant upload host uses tenant token", |
| 128 | configuredHost: "acme.ghe.com", |
| 129 | configuredToken: "tenant-token", |
| 130 | wantRequestHost: "uploads.acme.ghe.com", |
| 131 | wantAuthorization: "token tenant-token", |
| 132 | }, |
| 133 | } |
| 134 | |
| 135 | for _, tt := range tests { |
| 136 | t.Run(tt.name, func(t *testing.T) { |
| 137 | a := testAsset(t, "shot.png", "image/png", "x") |
| 138 | |
| 139 | reg := &httpmock.Registry{} |
| 140 | defer reg.Verify(t) |
| 141 | reg.Register( |
| 142 | httpmock.REST("POST", "user-attachments/assets"), |
| 143 | httpmock.StatusStringResponse(201, `{"url":"https://github.com/user-attachments/assets/1"}`), |
| 144 | ) |
| 145 | |
| 146 | client := &http.Client{ |
| 147 | Transport: api.AddAuthTokenHeader( |
| 148 | reg, |
| 149 | staticTokenConfig{tt.configuredHost: tt.configuredToken}, |
| 150 | ), |
| 151 | } |
| 152 | uploader, err := NewUploader(client, gh.TokenTypeOAuth, tt.configuredHost, 1, "WRITE") |
| 153 | require.NoError(t, err) |
| 154 | |
| 155 | _, err = uploader.upload(context.Background(), a) |
| 156 | |
| 157 | require.NoError(t, err) |
| 158 | require.Len(t, reg.Requests, 1) |
| 159 | assert.Equal(t, tt.wantRequestHost, reg.Requests[0].URL.Host) |
| 160 | assert.Equal(t, tt.wantAuthorization, reg.Requests[0].Header.Get("Authorization")) |
| 161 | }) |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | func TestUploadErrors(t *testing.T) { |
| 166 | tests := []struct { |
nothing calls this directly
no test coverage detected