|
|
@ -12,6 +12,8 @@ import ( |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/json" |
|
|
|
"code.gitea.io/gitea/modules/json" |
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting" |
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/test" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
) |
|
|
|
) |
|
|
@ -183,93 +185,84 @@ func TestHTTPClientDownload(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
cases := []struct { |
|
|
|
cases := []struct { |
|
|
|
endpoint string |
|
|
|
endpoint string |
|
|
|
expectederror string |
|
|
|
expectedError string |
|
|
|
}{ |
|
|
|
}{ |
|
|
|
// case 0
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://status-not-ok.io", |
|
|
|
endpoint: "https://status-not-ok.io", |
|
|
|
expectederror: io.ErrUnexpectedEOF.Error(), |
|
|
|
expectedError: io.ErrUnexpectedEOF.Error(), |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 1
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://invalid-json-response.io", |
|
|
|
endpoint: "https://invalid-json-response.io", |
|
|
|
expectederror: "invalid json", |
|
|
|
expectedError: "invalid json", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 2
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://valid-batch-request-download.io", |
|
|
|
endpoint: "https://valid-batch-request-download.io", |
|
|
|
expectederror: "", |
|
|
|
expectedError: "", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 3
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://response-no-objects.io", |
|
|
|
endpoint: "https://response-no-objects.io", |
|
|
|
expectederror: "", |
|
|
|
expectedError: "", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 4
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://unknown-transfer-adapter.io", |
|
|
|
endpoint: "https://unknown-transfer-adapter.io", |
|
|
|
expectederror: "TransferAdapter not found: ", |
|
|
|
expectedError: "TransferAdapter not found: ", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 5
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://error-in-response-objects.io", |
|
|
|
endpoint: "https://error-in-response-objects.io", |
|
|
|
expectederror: "Object not found", |
|
|
|
expectedError: "Object not found", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 6
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://empty-actions-map.io", |
|
|
|
endpoint: "https://empty-actions-map.io", |
|
|
|
expectederror: "missing action 'download'", |
|
|
|
expectedError: "missing action 'download'", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 7
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://download-actions-map.io", |
|
|
|
endpoint: "https://download-actions-map.io", |
|
|
|
expectederror: "", |
|
|
|
expectedError: "", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 8
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://upload-actions-map.io", |
|
|
|
endpoint: "https://upload-actions-map.io", |
|
|
|
expectederror: "missing action 'download'", |
|
|
|
expectedError: "missing action 'download'", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 9
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://verify-actions-map.io", |
|
|
|
endpoint: "https://verify-actions-map.io", |
|
|
|
expectederror: "missing action 'download'", |
|
|
|
expectedError: "missing action 'download'", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 10
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://unknown-actions-map.io", |
|
|
|
endpoint: "https://unknown-actions-map.io", |
|
|
|
expectederror: "missing action 'download'", |
|
|
|
expectedError: "missing action 'download'", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 11
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://legacy-batch-request-download.io", |
|
|
|
endpoint: "https://legacy-batch-request-download.io", |
|
|
|
expectederror: "", |
|
|
|
expectedError: "", |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for n, c := range cases { |
|
|
|
defer test.MockVariableValue(&setting.LFSClient.BatchOperationConcurrency, 3)() |
|
|
|
client := &HTTPClient{ |
|
|
|
for _, c := range cases { |
|
|
|
client: hc, |
|
|
|
t.Run(c.endpoint, func(t *testing.T) { |
|
|
|
endpoint: c.endpoint, |
|
|
|
client := &HTTPClient{ |
|
|
|
transfers: map[string]TransferAdapter{ |
|
|
|
client: hc, |
|
|
|
"dummy": dummy, |
|
|
|
endpoint: c.endpoint, |
|
|
|
}, |
|
|
|
transfers: map[string]TransferAdapter{ |
|
|
|
} |
|
|
|
"dummy": dummy, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err := client.Download(context.Background(), []Pointer{p}, func(p Pointer, content io.ReadCloser, objectError error) error { |
|
|
|
err := client.Download(context.Background(), []Pointer{p}, func(p Pointer, content io.ReadCloser, objectError error) error { |
|
|
|
if objectError != nil { |
|
|
|
if objectError != nil { |
|
|
|
return objectError |
|
|
|
return objectError |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
b, err := io.ReadAll(content) |
|
|
|
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
assert.Equal(t, []byte("dummy"), b) |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
if c.expectedError != "" { |
|
|
|
|
|
|
|
assert.ErrorContains(t, err, c.expectedError) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
assert.NoError(t, err) |
|
|
|
} |
|
|
|
} |
|
|
|
b, err := io.ReadAll(content) |
|
|
|
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
assert.Equal(t, []byte("dummy"), b) |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
if len(c.expectederror) > 0 { |
|
|
|
|
|
|
|
assert.True(t, strings.Contains(err.Error(), c.expectederror), "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
assert.NoError(t, err, "case %d", n) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -296,81 +289,73 @@ func TestHTTPClientUpload(t *testing.T) { |
|
|
|
|
|
|
|
|
|
|
|
cases := []struct { |
|
|
|
cases := []struct { |
|
|
|
endpoint string |
|
|
|
endpoint string |
|
|
|
expectederror string |
|
|
|
expectedError string |
|
|
|
}{ |
|
|
|
}{ |
|
|
|
// case 0
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://status-not-ok.io", |
|
|
|
endpoint: "https://status-not-ok.io", |
|
|
|
expectederror: io.ErrUnexpectedEOF.Error(), |
|
|
|
expectedError: io.ErrUnexpectedEOF.Error(), |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 1
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://invalid-json-response.io", |
|
|
|
endpoint: "https://invalid-json-response.io", |
|
|
|
expectederror: "invalid json", |
|
|
|
expectedError: "invalid json", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 2
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://valid-batch-request-upload.io", |
|
|
|
endpoint: "https://valid-batch-request-upload.io", |
|
|
|
expectederror: "", |
|
|
|
expectedError: "", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 3
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://response-no-objects.io", |
|
|
|
endpoint: "https://response-no-objects.io", |
|
|
|
expectederror: "", |
|
|
|
expectedError: "", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 4
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://unknown-transfer-adapter.io", |
|
|
|
endpoint: "https://unknown-transfer-adapter.io", |
|
|
|
expectederror: "TransferAdapter not found: ", |
|
|
|
expectedError: "TransferAdapter not found: ", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 5
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://error-in-response-objects.io", |
|
|
|
endpoint: "https://error-in-response-objects.io", |
|
|
|
expectederror: "Object not found", |
|
|
|
expectedError: "Object not found", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 6
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://empty-actions-map.io", |
|
|
|
endpoint: "https://empty-actions-map.io", |
|
|
|
expectederror: "", |
|
|
|
expectedError: "", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 7
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://download-actions-map.io", |
|
|
|
endpoint: "https://download-actions-map.io", |
|
|
|
expectederror: "missing action 'upload'", |
|
|
|
expectedError: "missing action 'upload'", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 8
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://upload-actions-map.io", |
|
|
|
endpoint: "https://upload-actions-map.io", |
|
|
|
expectederror: "", |
|
|
|
expectedError: "", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 9
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://verify-actions-map.io", |
|
|
|
endpoint: "https://verify-actions-map.io", |
|
|
|
expectederror: "missing action 'upload'", |
|
|
|
expectedError: "missing action 'upload'", |
|
|
|
}, |
|
|
|
}, |
|
|
|
// case 10
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
endpoint: "https://unknown-actions-map.io", |
|
|
|
endpoint: "https://unknown-actions-map.io", |
|
|
|
expectederror: "missing action 'upload'", |
|
|
|
expectedError: "missing action 'upload'", |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for n, c := range cases { |
|
|
|
defer test.MockVariableValue(&setting.LFSClient.BatchOperationConcurrency, 3)() |
|
|
|
client := &HTTPClient{ |
|
|
|
for _, c := range cases { |
|
|
|
client: hc, |
|
|
|
t.Run(c.endpoint, func(t *testing.T) { |
|
|
|
endpoint: c.endpoint, |
|
|
|
client := &HTTPClient{ |
|
|
|
transfers: map[string]TransferAdapter{ |
|
|
|
client: hc, |
|
|
|
"dummy": dummy, |
|
|
|
endpoint: c.endpoint, |
|
|
|
}, |
|
|
|
transfers: map[string]TransferAdapter{ |
|
|
|
} |
|
|
|
"dummy": dummy, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err := client.Upload(context.Background(), []Pointer{p}, func(p Pointer, objectError error) (io.ReadCloser, error) { |
|
|
|
err := client.Upload(context.Background(), []Pointer{p}, func(p Pointer, objectError error) (io.ReadCloser, error) { |
|
|
|
return io.NopCloser(new(bytes.Buffer)), objectError |
|
|
|
return io.NopCloser(new(bytes.Buffer)), objectError |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
if c.expectedError != "" { |
|
|
|
|
|
|
|
assert.ErrorContains(t, err, c.expectedError) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
assert.NoError(t, err) |
|
|
|
|
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
if len(c.expectederror) > 0 { |
|
|
|
|
|
|
|
assert.True(t, strings.Contains(err.Error(), c.expectederror), "case %d: '%s' should contain '%s'", n, err.Error(), c.expectederror) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
assert.NoError(t, err, "case %d", n) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|